aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/util/sort.h
blob: 5c9ed37161b6155a9743c467b3f442030d3011c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma once

#include <google/protobuf/message.h>

#include <util/generic/vector.h>
#include <util/generic/algorithm.h>

namespace NProtoBuf {
    // TComparePtr is something like: 
    // typedef bool (*TComparePtr)(const Message* msg1, const Message* msg2); 
    // typedef bool (*TComparePtr)(const TProto* msg1, const TProto* msg2); 

    template <typename TProto, typename TComparePtr> 
    void SortMessages(RepeatedPtrField<TProto>& msgs, TComparePtr cmp) { 
        TVector<TProto*> ptrs; 
        ptrs.reserve(msgs.size()); 
        while (msgs.size()) { 
            ptrs.push_back(msgs.ReleaseLast()); 
        } 

        ::StableSort(ptrs.begin(), ptrs.end(), cmp); 
 
        for (size_t i = 0; i < ptrs.size(); ++i) { 
            msgs.AddAllocated(ptrs[i]); 
        } 
    }

}