Claw  1.7.3
ordered_set.tpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #include <list>
31 
32 /*----------------------------------------------------------------------------*/
33 template<class K, class Comp>
35 
36 /*----------------------------------------------------------------------------*/
41 template<class K, class Comp>
44 {
45  return intersection( that );
46 } // ordered_set::operator*=()
47 
48 /*----------------------------------------------------------------------------*/
53 template<class K, class Comp>
56 {
57  return join( that );
58 } // ordered_set::operator+=()
59 
60 /*----------------------------------------------------------------------------*/
65 template<class K, class Comp>
68 {
69  return difference( that );
70 } // ordered_set::operator-=()
71 
72 /*----------------------------------------------------------------------------*/
77 template<class K, class Comp>
80 {
81  return symetric_difference( that );
82 } // ordered_set::operator/=()
83 
84 /*----------------------------------------------------------------------------*/
90 template<class K, class Comp>
91 bool
93 {
94  return strictly_contains( that );
95 } // ordered_set::operator>()
96 
97 /*----------------------------------------------------------------------------*/
103 template<class K, class Comp>
104 bool
106 {
107  return contains( that );
108 } // ordered_set::operator>=()
109 
110 /*----------------------------------------------------------------------------*/
116 template<class K, class Comp>
117 bool
119 {
120  return that.strictly_contains( *this );
121 } // ordered_set::operator<()
122 
123 /*----------------------------------------------------------------------------*/
129 template<class K, class Comp>
130 bool
132 {
133  return that.contains( *this );
134 } // ordered_set::operator<=()
135 
136 /*----------------------------------------------------------------------------*/
141 template<class K, class Comp>
144 {
145  std::list<K> remove_us;
146  const_iterator it;
147 
148  for (it=super::begin(); it!=super::end(); ++it)
149  if ( that.find( *it ) == that.end() )
150  remove_us.push_front( *it );
151 
152  typename std::list<K>::const_iterator remove_it;
153 
154  for (remove_it=remove_us.begin(); remove_it!=remove_us.end(); ++remove_it)
155  super::erase( *remove_it );
156 
157  return *this;
158 } // ordered_set::intersection()
159 
160 /*----------------------------------------------------------------------------*/
165 template<class K, class Comp>
168 {
169  const_iterator it;
170 
171  for (it=that.begin(); it!=that.end(); ++it)
172  super::insert( *it );
173 
174  return *this;
175 } // ordered_set::join()
176 
177 /*----------------------------------------------------------------------------*/
182 template<class K, class Comp>
185 {
186  std::list<K> remove_us;
187  const_iterator it;
188 
189  for (it=super::begin(); it!=super::end(); ++it)
190  if ( that.find( *it ) != that.end() )
191  remove_us.push_front( *it );
192 
193  typename std::list<K>::const_iterator remove_it;
194 
195  for (remove_it=remove_us.begin(); remove_it!=remove_us.end(); ++remove_it)
196  super::erase( *remove_it );
197 
198  return *this;
199 } // ordered_set::difference()
200 
201 /*----------------------------------------------------------------------------*/
206 template<class K, class Comp>
209 {
210  ordered_set<K, Comp> my_copy(*this), his_copy(that);
211 
212  return difference( that ).join( his_copy.difference(my_copy) );
213 } // ordered_set::symetric_difference()
214 
215 /*----------------------------------------------------------------------------*/
221 template<class K, class Comp>
222 bool
224 {
225  bool ok = super::size() >= that.size();
226  const_iterator it_this( super::begin() );
227  const_iterator it_that( that.begin() );
228 
229  while ( ok && (it_that != that.end()) && (it_this != super::end()) )
230  if ( s_key_comp( *it_this, *it_that ) )
231  ++it_this;
232  else if ( s_key_comp( *it_that, *it_this ) )
233  ok = false;
234  else
235  {
236  ++it_this;
237  ++it_that;
238  }
239 
240  return it_that == that.end();
241 } // ordered_set::contains()
242 
243 /*----------------------------------------------------------------------------*/
249 template<class K, class Comp>
250 bool
252 ( const ordered_set& that ) const
253 {
254  return contains(that) && ( super::size() > that.size() );
255 } // ordered_set::strictly_contains()
256