LRDE Tiger Compiler  1.34a $Id: 7fef12e1f5fa43449d667a0eec1d837c40fc1202 $
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
set.hxx
Go to the documentation of this file.
1 
6 #ifndef MISC_SET_HXX
7 # define MISC_SET_HXX
8 
9 # include <algorithm>
10 # include <ostream>
11 # include <misc/contract.hh>
12 # include <misc/set.hh>
13 
14 namespace misc
15 {
16 
17  template <class K, class C, class A>
18  inline
20  {}
21 
22  template <class K, class C, class A>
23  template <typename Iter>
24  inline
25  set<K, C, A>::set(Iter first, Iter last)
26  {
27  this->insert(first, last);
28  }
29 
30  /* Useful to convert a std::list in set::set. */
31  template <class K, class C, class A>
32  inline
33  set<K, C, A>::set(const std::list<K> l)
34  {
35  for (const K& x : l)
36  this->insert(x);
37  }
38 
39  template <class K, class C, class A>
40  inline
42  {}
43 
44  template <class K, class C, class A>
45  inline
46  bool
47  set<K, C, A>::has(const K& k) const
48  {
49  return set_type::find(k) != this->end();
50  }
51 
52  /* \brief Return the addition of \a data into \a this.
53  \a data must not yet be part of \a this. */
54  template <class K, class C, class A>
55  inline
57  set<K, C, A>::operator+(const K& data) const
58  {
59  precondition(! (data % *this));
60  set<K, C, A> res(*this);
61  res.insert(data);
62  return res;
63  }
64 
65  /* \brief Insert \a data in \a this.
66  \a data must not yet be part of \a this. */
67  template <class K, class C, class A>
68  inline
70  set<K, C, A>::operator+=(const K& data)
71  {
72  precondition(! (data % *this));
73  this->insert(data);
74  return *this;
75  }
76 
77  /* \brief Return the removal of \a data into \a this.
78  \a data must be part of \a this. */
79  template <class K, class C, class A>
80  inline
82  set<K, C, A>::operator-(const K& data) const
83  {
84  precondition(data % *this);
85  set<K, C, A> res(*this);
86  res.erase(data);
87  return res;
88  }
89 
90  /* \brief Remove \a data from \a this.
91  \a data must be part of \a this. */
92  template <class K, class C, class A>
93  inline
95  set<K, C, A>::operator-=(const K& data)
96  {
97  precondition(data % *this);
98  this->erase(data);
99  return *this;
100  }
101 
102 
103  // Union with another set \a s.
104  // FIXME: Deprecate this use, it ought to be direct sum.
105  template <class K, class C, class A>
106  inline
109  {
110  return set_union(*this, s);
111  }
112 
113  // In place union with another set \a s.
114  template <class K, class C, class A>
115  inline
116  set<K, C, A>&
118  {
119  return *this = *this + s;
120  }
121 
122  // Subtraction with another set \a s.
123  template <class K, class C, class A>
124  inline
127  {
128  return set_difference(*this, s);
129  }
130 
131  // In place subtraction with another set \a s.
132  template <class K, class C, class A>
133  inline
134  set<K, C, A>&
136  {
137  *this = *this - s;
138  return *this;
139  }
140 
141  // Union with another set \a s.
142  template <class K, class C, class A>
143  inline
146  {
147  return *this + s;
148  }
149 
150  // In place union with another set \a s.
151  template <class K, class C, class A>
152  inline
153  set<K, C, A>&
155  {
156  return *this += s;
157  }
158 
159  // Intersection with another set \a s.
160  template <class K, class C, class A>
161  inline
164  {
165  return set_intersection(*this, s);
166  }
167 
168  // In place intersection with another set \a s.
169  template <class K, class C, class A>
170  inline
171  set<K, C, A>&
173  {
174  return *this = *this & s;
175  }
176 
177 
178  template <class K, class C, class A>
179  inline
182  {
183  set<K, C, A> res;
184  // Specifying the key_comp to use is required: without it, the
185  // first set is properly ordered using its own key_comp, but
186  // neither the second set nor the resulting set are ordering
187  // using it. Even if s1, s2, and res do use the same Compare!
188  std::set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(),
189  inserter(res, res.begin()), s1.key_comp());
190  return res;
191  }
192 
193  template <class K, class C, class A>
194  inline
195  set<K, C, A>
197  {
198  set<K, C, A> res;
199  std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
200  inserter(res, res.begin()), s1.key_comp());
201  return res;
202  }
203 
204  template <class K, class C, class A>
205  inline
206  set<K, C, A>
207  set_union(const set<K, C, A>& s1, const set<K, C, A>& s2)
208  {
209  set<K, C, A> res;
210  std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
211  inserter(res, res.begin()), s1.key_comp());
212  return res;
213  }
214 
215 
216  template <class K, class C, class A>
217  inline
218  std::ostream&
219  operator<<(std::ostream& ostr, const set<K, C, A>& s)
220  {
221  for (typename set<K, C, A>::const_iterator i = s.begin(); i != s.end();)
222  {
223  ostr << *i;
224  if (++i != s.end())
225  ostr << ", ";
226  }
227  return ostr;
228  }
229 
230  template <class K, class C, class A>
231  inline
232  bool
233  operator%(const K& k, const set<K, C, A>& s)
234  {
235  return s.has(k);
236  }
237 
238 } // namespace misc
239 
240 #endif // !MISC_SET_HXX