summaryrefslogtreecommitdiff
path: root/cpp/src/Glacier2/FilterT.h
blob: 078d174f192e1db72d50be71125091b02909ff83 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef FILTER_I_H
#define FILTER_I_H

#include <Glacier2/Session.h>

#include <Ice/Identity.h>

#include <list>
#include <mutex>
#include <string>
#include <vector>

namespace Glacier2
{

template <typename T, class P>
class FilterT : public P
{
public:

    FilterT(const std::vector<T>&);

    //
    // Slice to C++ mapping.
    //
    virtual void add(std::vector<T>, const Ice::Current&) override;
    virtual void remove(std::vector<T>, const Ice::Current&) override;
    virtual std::vector<T> get(const Ice::Current&) override;

    //
    // Internal functions.
    //
    bool
    match(const T& candidate) const
    {
        std::lock_guard<std::mutex> lg(_mutex);
        //
        // Empty vectors mean no filtering, so all matches will succeed.
        //
        if(_items.size() == 0)
        {
            return true;
        }

        return binary_search(_items.begin(), _items.end(), candidate);
    }

    bool
    empty() const
    {
        std::lock_guard<std::mutex> lg(_mutex);
        return _items.size() == 0;
    }

private:

    std::vector<T> _items;

    mutable std::mutex _mutex;
};

template<class T, class P>
FilterT<T, P>::FilterT(const std::vector<T>& accept):
    _items(accept)
{
    sort(_items.begin(), _items.end());
    _items.erase(unique(_items.begin(), _items.end()), _items.end());
}

template<class T, class P> void
FilterT<T, P>::add(std::vector<T> additions, const Ice::Current&)
{
    //
    // Sort the filter elements first, erasing duplicates. Then we can
    // simply use the STL merge algorithm to add to our list of filters.
    //
    std::vector<T> newItems(additions);
    sort(newItems.begin(), newItems.end());
    newItems.erase(unique(newItems.begin(), newItems.end()), newItems.end());

    std::lock_guard<std::mutex> lg(_mutex);
    std::vector<T> merged(_items.size() + newItems.size());
    merge(newItems.begin(), newItems.end(), _items.begin(), _items.end(), merged.begin());
    merged.erase(unique(merged.begin(), merged.end()), merged.end());
    swap(_items, merged);
}

template<class T, class P> void
FilterT<T, P>::remove(std::vector<T> deletions, const Ice::Current&)
{
    //
    // Our removal algorithm depends on the filter elements to be
    // removed to be sorted in the same order as our current elements.
    //
    std::vector<T> toRemove(deletions);
    sort(toRemove.begin(), toRemove.end());
    toRemove.erase(unique(toRemove.begin(), toRemove.end()), toRemove.end());

    std::lock_guard<std::mutex> lg(_mutex);

    //
    // Our vectors are both sorted, so if we keep track of our first
    // match between the current set and the set of items to be removed,
    // we do not need to traverse the whole of the current set each
    // time. We also use a list of deletions instead of erasing things
    // itemwise.
    //

    typename std::vector<T>::const_iterator r = toRemove.begin();
    typename std::vector<T>::iterator mark = _items.begin();
    std::list<typename std::vector<T>::iterator> deleteList;

    while(r != toRemove.end())
    {
        typename std::vector<T>::iterator i = mark;
        while(i != _items.end() && r != toRemove.end())
        {
            if(*r == *i)
            {
                //
                // We want this list to be in LIFO order because we are
                // going to erase things from the tail forward.
                //
                deleteList.push_front(i);
                ++i;
                ++r;
                mark = i;
            }
            else
            {
                ++i;
            }
        }

        if(r == toRemove.end())
        {
            break;
        }
        ++r;
    }

    for(const auto& item : deleteList)
    {
        _items.erase(item);
    }
}

template<class T, class P> std::vector<T>
FilterT<T, P>::get(const Ice::Current&)
{
    std::lock_guard<std::mutex> lg(_mutex);
    return _items;
}

using IdentitySetI = FilterT<Ice::Identity, Glacier2::IdentitySet>;
using StringSetI = FilterT<std::string, Glacier2::StringSet>;

}

#endif