libept
operators.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 #ifndef EPT_DEBTAGS_COLL_OPERATORS_H
4 #define EPT_DEBTAGS_COLL_OPERATORS_H
5 
6 #include <set>
7 #include <algorithm>
8 
9 namespace ept {
10 namespace debtags {
11 namespace coll {
12 namespace operators {
13 
14 /*
15 template< typename S, typename VT > struct IsContainer {
16  typedef S T;
17 };
18 
19 template< typename S >
20 typename IsContainer< S, typename S::value_type >::T operator &&( const S &a, const S &b ) {
21  S ret;
22  std::set_intersection( a.begin(), a.end(), b.begin(), b.end(),
23  std::inserter( ret, ret.begin() ) );
24  return ret;
25 }
26 */
27 
28 template< typename T >
29 T operator+( const T &i, typename T::difference_type o ) {
30  T r = i;
31  std::advance( r, o );
32  return r;
33 }
34 
35 template< typename T >
36 std::set< T > operator &( const std::set< T > &a, const std::set< T > &b ) {
37  std::set< T > ret;
38  std::set_intersection( a.begin(), a.end(), b.begin(), b.end(),
39  std::inserter( ret, ret.begin() ) );
40  return ret;
41 }
42 
43 template< typename T >
44 std::set< T > operator &( const std::set< T > &a, const T &b ) {
45  std::set< T > ret;
46  if ( a.find( b ) != a.end() ) {
47  std::set< T > r;
48  r.insert( b );
49  return r;
50  }
51  return std::set< T >();
52 }
53 
54 template< typename T >
55 std::set< T > operator |( const std::set< T > &a, const T& item ) {
56  std::set< T > ret = a;
57  ret.insert(item);
58  return ret;
59 }
60 
61 template< typename T >
62 std::set< T > operator |( const std::set< T > &a, const std::set< T > &b ) {
63  std::set< T > ret;
64  std::set_union( a.begin(), a.end(), b.begin(), b.end(),
65  std::inserter( ret, ret.begin() ) );
66  return ret;
67 }
68 
69 template< typename T >
70 std::set< T > operator -( const std::set< T > &a, const std::set< T > &b ) {
71  std::set< T > ret;
72  std::set_difference( a.begin(), a.end(), b.begin(), b.end(),
73  std::inserter(ret, ret.begin() ) );
74  return ret;
75 }
76 
77 template< typename T >
78 std::set< T > operator -( const std::set< T > &a, const T& item ) {
79  std::set< T > ret = a;
80  ret.erase(item);
81  return ret;
82 }
83 
84 template< typename T >
85 std::set< T > &operator|=( std::set< T > &a, const T& item )
86 {
87  a.insert(item);
88  return a;
89 }
90 
91 // General case
92 template< typename T, typename SEQ >
93 std::set< T > &operator|=( std::set< T > &a, const SEQ& items )
94 {
95  for (typename SEQ::const_iterator i = items.begin();
96  i != items.end(); ++i)
97  a.insert(*i);
98  return a;
99 }
100 
101 // Little optimization in case a is empty
102 template< typename T >
103 std::set< T > &operator |=( std::set< T > &a, const std::set< T > &b ) {
104  if (a.empty())
105  return a = b;
106 
107  for (typename std::set<T>::const_iterator i = b.begin();
108  i != b.end(); ++i)
109  a.insert(*i);
110  return a;
111 }
112 
113 // General case, but assumes that b is sorted
114 template< typename T, typename SEQ >
115 std::set< T > &operator &=( std::set< T > &a, const SEQ& b ) {
116  // Little optimization: if b is empty, we avoid a run through a
117  if (b.empty())
118  {
119  a.clear();
120  return a;
121  }
122 
123  typename std::set<T>::iterator ia = a.begin();
124  typename SEQ::const_iterator ib = b.begin();
125  while (ia != a.end())
126  {
127  if (ib != b.end() && *ib < *ia)
128  {
129  ++ib;
130  }
131  else if (ib == b.end() || *ia != *ib)
132  {
133  typename std::set<T>::iterator tmp = ia;
134  ++ia;
135  a.erase(tmp);
136  }
137  else
138  {
139  ++ia;
140  ++ib;
141  }
142  }
143  return a;
144 }
145 
146 template< typename T >
147 std::set< T > &operator-=( std::set< T > &a, const T& item )
148 {
149  a.erase(item);
150  return a;
151 }
152 
153 // General case, but works only if b is sorted
154 template< typename T, typename SEQ >
155 std::set< T > &operator -=( std::set< T > &a, const SEQ& b )
156 {
157  typename std::set<T>::iterator ia = a.begin();
158  typename SEQ::const_iterator ib = b.begin();
159  while (ia != a.end() && ib != b.end())
160  {
161  if (*ia == *ib)
162  {
163  typename std::set<T>::iterator tmp = ia;
164  ++ia;
165  ++ib;
166  a.erase(tmp);
167  }
168  else if (*ia < *ib)
169  ++ia;
170  else
171  ++ib;
172  }
173  return a;
174 }
175 
176 template< typename T >
177 bool operator<=( const T &a, const std::set< T > &b ) {
178  return b.find( a ) != b.end();
179 }
180 
181 template< typename T >
182 bool operator<=( const std::set< T > &a, const std::set< T > &b ) {
183  typename std::set<T>::const_iterator x = a.begin();
184 
185  for ( typename std::set<T>::const_iterator y = b.begin(); y != b.end(); ++y )
186  if ( x == a.end() )
187  return true;
188  else if (*x == *y)
189  ++x;
190  else if (*x < *y)
191  return false;
192 
193  return x == a.end();
194 }
195 
196 }
197 }
198 }
199 }
200 
201 #endif
std::set< T > & operator&=(std::set< T > &a, const SEQ &b)
Definition: operators.h:115
std::set< T > operator|(const std::set< T > &a, const T &item)
Definition: operators.h:55
std::set< T > & operator|=(std::set< T > &a, const T &item)
Definition: operators.h:85
String functions.
Definition: apt.cc:38
T operator+(const T &i, typename T::difference_type o)
Definition: operators.h:29
std::set< T > & operator-=(std::set< T > &a, const T &item)
Definition: operators.h:147
std::set< T > operator-(const std::set< T > &a, const std::set< T > &b)
Definition: operators.h:70
std::set< T > operator&(const std::set< T > &a, const std::set< T > &b)
Definition: operators.h:36