forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceIdSet.h
More file actions
135 lines (101 loc) · 3.18 KB
/
SourceIdSet.h
File metadata and controls
135 lines (101 loc) · 3.18 KB
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
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_COMMON_SOURCEIDSET_H_INCLUDED
#define REACT_COMMON_SOURCEIDSET_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <algorithm>
#include <vector>
#include "tbb/queuing_mutex.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// SourceIdSet
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename T>
class SourceIdSet
{
private:
using MutexT = tbb::queuing_mutex;
using DataT = std::vector<T>;
public:
void Insert(const T& e)
{
MutexT::scoped_lock lock(mutex_);
data_.push_back(e);
isSorted_ = false;
}
void Insert(SourceIdSet<T>& other)
{
MutexT::scoped_lock myLock(mutex_);
MutexT::scoped_lock otherLock(other.mutex_);
sort();
other.sort();
auto l = data_.begin();
auto r = data_.end();
auto offset = std::distance(l,r);
// For each element in other, check if it's already contained in this
// if not, add it
for (const auto& e : other.data_)
{
l = std::lower_bound(l, r, e);
// Already in the set?
if (l < r && *l == e)
continue;
auto d = std::distance(data_.begin(), l);
data_.push_back(e);
// push_back invalidates iterators
l = data_.begin() + d;
r = data_.begin() + offset;
}
std::inplace_merge(data_.begin(), data_.begin() + offset, data_.end());
}
void Erase(const T& e)
{
MutexT::scoped_lock lock(mutex_);
data_.erase(std::find(data_.begin(), data_.end(), e));
}
void Clear()
{
MutexT::scoped_lock lock(mutex_);
data_.clear();
isSorted_ = true;
}
bool IntersectsWith(SourceIdSet<T>& other)
{
MutexT::scoped_lock myLock(mutex_);
MutexT::scoped_lock otherLock(other.mutex_);
sort();
other.sort();
auto l1 = data_.begin();
const auto r1 = data_.end();
auto l2 = other.data_.begin();
const auto r2 = other.data_.end();
// Is intersection of turn sourceIds and node sourceIds non-empty?
while (l1 != r1 && l2 != r2)
{
if (*l1 < *l2)
l1++;
else if (*l2 < *l1)
l2++;
// Equals => Intersect
else
return true;
}
return false;
}
private:
MutexT mutex_;
DataT data_;
bool isSorted_ = false;
void sort()
{
if (isSorted_)
return;
std::sort(data_.begin(), data_.end());
isSorted_ = true;
}
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_COMMON_SOURCEIDSET_H_INCLUDED