wibble 1.1
operators.h
Go to the documentation of this file.
1// -*- C++ -*-
2
3#include <set>
4#include <wibble/empty.h>
5#include <wibble/singleton.h>
6#include <algorithm>
7
8#ifndef WIBBLE_OPERATORS_H
9#define WIBBLE_OPERATORS_H
10
11namespace wibble {
12namespace operators {
13
14/*
15template< typename S, typename VT > struct IsContainer {
16 typedef S T;
17};
18
19template< typename S >
20typename 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
28template< typename T >
29T operator+( const T &i, typename T::difference_type o ) {
30 T r = i;
31 std::advance( r, o );
32 return r;
33}
34
35template< typename T >
36std::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
43template< typename T >
44std::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
54template< typename T >
55std::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
61template< typename T >
62std::set< T > operator |( const std::set< T > &a, const wibble::Empty<T>& ) {
63 return a;
64}
65
66template< typename T >
67std::set< T > operator |( const std::set< T > &a, const wibble::Singleton<T>& item ) {
68 std::set< T > ret = a;
69 ret.insert(*item.begin());
70 return ret;
71}
72
73template< typename T >
74std::set< T > operator |( const std::set< T > &a, const std::set< T > &b ) {
75 std::set< T > ret;
76 std::set_union( a.begin(), a.end(), b.begin(), b.end(),
77 std::inserter( ret, ret.begin() ) );
78 return ret;
79}
80
81template< typename T >
82std::set< T > operator -( const std::set< T > &a, const std::set< T > &b ) {
83 std::set< T > ret;
84 std::set_difference( a.begin(), a.end(), b.begin(), b.end(),
85 std::inserter(ret, ret.begin() ) );
86 return ret;
87}
88
89template< typename T >
90std::set< T > operator -( const std::set< T > &a, const T& item ) {
91 std::set< T > ret = a;
92 ret.erase(item);
93 return ret;
94}
95
96template< typename T >
97std::set< T > operator -( const std::set< T > &a, const wibble::Singleton<T>& item ) {
98 std::set< T > ret = a;
99 ret.erase(*item.begin());
100 return ret;
101}
102
103template< typename T >
104std::set< T > operator -( const std::set< T > &a, const wibble::Empty<T>& ) {
105 return a;
106}
107
108template< typename T >
109std::set< T > &operator|=( std::set< T > &a, const wibble::Empty<T>& )
110{
111 return a;
112}
113
114template< typename T >
115std::set< T > &operator|=( std::set< T > &a, const T& item )
116{
117 a.insert(item);
118 return a;
119}
120
121// General case
122template< typename T, typename SEQ >
123std::set< T > &operator|=( std::set< T > &a, const SEQ& items )
124{
125 for (typename SEQ::const_iterator i = items.begin();
126 i != items.end(); ++i)
127 a.insert(*i);
128 return a;
129}
130
131// Little optimization in case a is empty
132template< typename T >
133std::set< T > &operator |=( std::set< T > &a, const std::set< T > &b ) {
134 if (a.empty())
135 return a = b;
136
137 for (typename std::set<T>::const_iterator i = b.begin();
138 i != b.end(); ++i)
139 a.insert(*i);
140 return a;
141}
142
143// General case, but assumes that b is sorted
144template< typename T, typename SEQ >
145std::set< T > &operator &=( std::set< T > &a, const SEQ& b ) {
146 // Little optimization: if b is empty, we avoid a run through a
147 if (b.empty())
148 {
149 a.clear();
150 return a;
151 }
152
153 typename std::set<T>::iterator ia = a.begin();
154 typename SEQ::const_iterator ib = b.begin();
155 while (ia != a.end())
156 {
157 if (ib != b.end() && *ib < *ia)
158 {
159 ++ib;
160 }
161 else if (ib == b.end() || *ia != *ib)
162 {
163 typename std::set<T>::iterator tmp = ia;
164 ++ia;
165 a.erase(tmp);
166 }
167 else
168 {
169 ++ia;
170 ++ib;
171 }
172 }
173 return a;
174}
175
176template< typename T >
177std::set< T > &operator-=( std::set< T > &a, const wibble::Empty<T>& )
178{
179 return a;
180}
181
182template< typename T >
183std::set< T > &operator-=( std::set< T > &a, const T& item )
184{
185 a.erase(item);
186 return a;
187}
188
189template< typename T >
190std::set< T > &operator-=( std::set< T > &a, const wibble::Singleton<T>& item )
191{
192 a.erase(*item.begin());
193 return a;
194}
195
196// General case, but works only if b is sorted
197template< typename T, typename SEQ >
198std::set< T > &operator -=( std::set< T > &a, const SEQ& b )
199{
200 typename std::set<T>::iterator ia = a.begin();
201 typename SEQ::const_iterator ib = b.begin();
202 while (ia != a.end() && ib != b.end())
203 {
204 if (*ia == *ib)
205 {
206 typename std::set<T>::iterator tmp = ia;
207 ++ia;
208 ++ib;
209 a.erase(tmp);
210 }
211 else if (*ia < *ib)
212 ++ia;
213 else
214 ++ib;
215 }
216 return a;
217}
218
219template< typename T >
220bool operator<=( const T &a, const std::set< T > &b ) {
221 return b.find( a ) != b.end();
222}
223
224template< typename T >
225bool operator<=( const std::set< T > &a, const std::set< T > &b ) {
226 typename std::set<T>::const_iterator x = a.begin();
227
228 for ( typename std::set<T>::const_iterator y = b.begin(); y != b.end(); ++y )
229 if ( x == a.end() )
230 return true;
231 else if (*x == *y)
232 ++x;
233 else if (*x < *y)
234 return false;
235
236 return x == a.end();
237}
238
239}
240}
241
242#endif
T operator+(const T &i, typename T::difference_type o)
Definition operators.h:29
std::set< T > & operator|=(std::set< T > &a, const wibble::Empty< T > &)
Definition operators.h:109
std::set< T > operator&(const std::set< T > &a, const std::set< T > &b)
Definition operators.h:36
std::set< T > & operator-=(std::set< T > &a, const wibble::Empty< T > &)
Definition operators.h:177
std::set< T > operator|(const std::set< T > &a, const T &item)
Definition operators.h:55
std::set< T > operator-(const std::set< T > &a, const std::set< T > &b)
Definition operators.h:82
std::set< T > & operator&=(std::set< T > &a, const SEQ &b)
Definition operators.h:145
bool operator<=(const T &a, const std::set< T > &b)
Definition operators.h:220
Definition amorph.h:17
Definition amorph.h:30