feisty meow concerns codebase  2.140
set.h
Go to the documentation of this file.
1 #ifndef SET_CLASS
2 #define SET_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : set *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1996-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include "object_packers.h"
19 #include "string_array.h"
20 
21 #include <basis/astring.h>
22 #include <basis/contracts.h>
23 #include <basis/definitions.h>
24 #include <basis/guards.h>
25 
26 namespace structures {
27 
29 
34 template <class contents>
35 class set : public basis::array<contents>
36 {
37 public:
39 
41  set(int num = 0, const contents *init = NULL_POINTER,
43  : basis::array<contents>(num, init, flags) {}
44 
45  ~set() {}
46 
47  int elements() const { return this->length(); }
49 
50  bool empty() const { return elements() == 0; }
52  bool non_empty() const { return elements() != 0; }
54 
55  void clear() { this->reset(); }
56 
57  bool member(const contents &to_test) const;
59 
60  bool add(const contents &to_add);
62 
65  set &operator += (const contents &to_add)
66  { add(to_add); return *this; }
68  set &operator += (const set &to_add)
69  { unionize(to_add); return *this; }
71 
72  bool remove(const contents &to_remove);
74 
76  set &operator -= (const contents &to_zap)
77  { remove(to_zap); return *this; }
79  set &operator -= (const set &to_zap)
80  { differentiate(to_zap); return *this; }
82 
83  set set_union(const set &union_with) const;
85 
89  void unionize(const set &union_with);
91 
92  set operator + (const set &uw) const { return set_union(uw); }
94 
95  set intersection(const set &intersect_with) const;
97 
98  set operator * (const set &iw) const { return intersection(iw); }
100 
101  set difference(const set &differ_with) const;
103 
106  void differentiate(const set &differ_with);
108 
111  set operator - (const set &dw) const { return difference(dw); }
113 
114  int find(const contents &to_find) const;
116 
121 
123  bool remove_index(int index)
124  { return this->zap(index, index) == basis::common::OKAY; }
125 };
126 
128 
130 template <class contents>
131 void pack(basis::byte_array &packed_form, const set<contents> &to_pack)
132 {
133  obscure_attach(packed_form, to_pack.elements());
134  for (int i = 0; i < to_pack.elements(); i++) to_pack[i].pack(packed_form);
135 }
136 
138 template <class contents>
139 bool unpack(basis::byte_array &packed_form, set<contents> &to_unpack)
140 {
141  to_unpack.clear();
142  basis::un_int len;
143  if (!obscure_detach(packed_form, len)) return false;
144  contents to_fill;
145  for (int i = 0; i < (int)len; i++) {
146  if (!to_fill.unpack(packed_form)) return false;
147  to_unpack.add(to_fill);
148  }
149  return true;
150 }
151 
153 
155 class int_set : public set<int>, public virtual basis::root_object
156 {
157 public:
158  int_set() {}
160  int_set(const set<int> &to_copy) : set<int>(to_copy) {}
162 
163  DEFINE_CLASS_NAME("int_set");
164 };
165 
167 class string_set : public set<basis::astring>, public virtual basis::packable
168 {
169 public:
173  : set<basis::astring>(to_copy) {}
175  string_set(const string_array &to_copy) {
176  for (int i = 0; i < to_copy.length(); i++)
177  add(to_copy[i]);
178  }
179 
180  DEFINE_CLASS_NAME("string_set");
181 
182  bool operator == (const string_set &compare) const {
183  for (int i = 0; i < elements(); i++)
184  if (get(i) != compare.get(i)) return false;
185  return true;
186  }
187 
188  operator string_array() const {
189  string_array to_return;
190  for (int i = 0; i < length(); i++)
191  to_return += get(i);
192  return to_return;
193  }
194 
195  virtual void pack(basis::byte_array &packed_form) const
196  { structures::pack(packed_form, *this); }
197  virtual bool unpack(basis::byte_array &packed_form)
198  { return structures::unpack(packed_form, *this); }
199  virtual int packed_size() const {
200  int to_return = sizeof(int) * 2; // length packed in, using obscure.
201  for (int i = 0; i < length(); i++)
202  to_return += get(i).length() + 1;
203  return to_return;
204  }
205 };
206 
208 class pointer_set : public set<void *>
209 {
210 public:
213  pointer_set(const set<void *> &to_copy) : set<void *>(to_copy)
214  {}
216 };
217 
219 
220 // implementation for longer functions is below...
221 
222 template <class contents>
223 bool set<contents>::member(const contents &to_test) const
224 {
225  for (int i = 0; i < elements(); i++)
226  if (to_test == this->get(i))
227  return true;
228  return false;
229 }
230 
231 template <class contents>
232 bool set<contents>::add(const contents &to_add)
233 {
234  if (member(to_add)) return false;
235  this->concatenate(to_add);
236  return true;
237 }
238 
239 template <class contents>
240 int set<contents>::find(const contents &to_find) const
241 {
242  for (int i = 0; i < elements(); i++)
243  if (to_find == this->get(i))
244  return i;
245  return basis::common::NOT_FOUND;
246 }
247 
248 template <class contents>
249 bool set<contents>::remove(const contents &to_remove)
250 {
251  for (int i = 0; i < elements(); i++)
252  if (to_remove == this->get(i)) {
253  this->zap(i, i);
254  return true;
255  }
256  return false;
257 }
258 
259 template <class contents>
260 set<contents> set<contents>::intersection(const set &intersect_with) const
261 {
262  set<contents> created(0, NULL_POINTER, this->flags());
263  const set *smaller = this;
264  const set *larger = &intersect_with;
265  if (elements() > intersect_with.elements()) {
266  // switch the smaller one into place.
267  smaller = &intersect_with;
268  larger = this;
269  }
270  for (int i = 0; i < smaller->length(); i++)
271  if (larger->member(smaller->get(i)))
272  created.concatenate(smaller->get(i));
273  return created;
274 }
275 
276 template <class contents>
278 {
279  set<contents> created = *this;
280  for (int i = 0; i < union_with.elements(); i++)
281  created.add(union_with.get(i));
282  return created;
283 }
284 
285 template <class contents>
286 void set<contents>::unionize(const set &union_with)
287 {
288  for (int i = 0; i < union_with.elements(); i++)
289  add(union_with.get(i));
290 }
291 
292 template <class contents>
293 set<contents> set<contents>::difference(const set &differ_with) const
294 {
295  set<contents> created = *this;
296  for (int i = 0; i < differ_with.elements(); i++) {
297  if (created.member(differ_with[i]))
298  created.remove(differ_with[i]);
299  }
300  return created;
301 }
302 
303 template <class contents>
304 void set<contents>::differentiate(const set &differ_with)
305 {
306  for (int i = 0; i < differ_with.elements(); i++) {
307  if (member(differ_with[i]))
308  remove(differ_with[i]);
309  }
310 }
311 
312 } // namespace.
313 
314 #endif
315 
Represents a sequential, ordered, contiguous collection of objects.
Definition: array.h:54
int flags() const
Provides the raw flags value, without interpreting what it means.
Definition: array.h:121
void reset(int number=0, const contents *initial_contents=NULL_POINTER)
Resizes this array and sets the contents from an array of contents.
Definition: array.h:349
array & concatenate(const array &to_concatenate)
Appends the array "to_concatenate" onto "this" and returns "this".
Definition: array.h:379
const basis::astring & get(int index) const
Accesses individual objects stored in "this" at the "index" position.
Definition: array.h:372
int length() const
Returns the current reported length of the allocated C array.
Definition: array.h:115
outcome zap(int start, int end)
Deletes from "this" the objects inclusively between "start" and "end".
Definition: array.h:769
array(int number=0, const contents *init=NULL_POINTER, int flags=EXPONENTIAL_GROWTH|FLUSH_INVISIBLE)
Constructs an array with room for "number" objects.
Definition: array.h:315
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
A base class for objects that can pack into an array of bytes.
Definition: byte_array.h:87
A simple object that wraps a templated set of ints.
Definition: set.h:156
DEFINE_CLASS_NAME("int_set")
int_set()
Constructs an empty set of ints.
Definition: set.h:158
int_set(const set< int > &to_copy)
Constructs a copy of the "to_copy" array.
Definition: set.h:160
A set of pointers that hides the platform's pointer size.
Definition: set.h:209
pointer_set()
Constructs an empty set of void pointers.
Definition: set.h:211
pointer_set(const set< void * > &to_copy)
Constructs a copy of the "to_copy" array.
Definition: set.h:213
Emulates a mathematical set, providing several standard set operations.
Definition: set.h:36
~set()
Destroys any storage held for the set.
Definition: set.h:45
int elements() const
Returns the number of elements in this set.
Definition: set.h:47
set(int num=0, const contents *init=NULL_POINTER, basis::un_short flags=basis::array< contents >::EXPONE)
Constructs a set with "num" elements, copying them from "init".
Definition: set.h:41
set difference(const set &differ_with) const
Returns the difference of this with "differ_with".
Definition: set.h:293
set set_union(const set &union_with) const
Implements the set union of "this" with "union_with".
Definition: set.h:277
set intersection(const set &intersect_with) const
Returns the intersection of "this" with the set in "intersect_with".
Definition: set.h:260
set & operator+=(const contents &to_add)
An algebraic operator synonym for add() that operates on the contents.
Definition: set.h:65
bool member(const contents &to_test) const
Returns true if the item "to_test" is a member of this set.
Definition: set.h:223
set operator*(const set &iw) const
A synonym for intersection.
Definition: set.h:98
bool remove_index(int index)
Zaps the entry at the specified "index".
Definition: set.h:123
set & operator-=(const contents &to_zap)
An algebraic operator synonym for remove that operates on the contents.
Definition: set.h:76
bool add(const contents &to_add)
Adds a new element "to_add" to the set.
Definition: set.h:232
bool remove(const contents &to_remove)
Removes the item "to_remove" from the set.
Definition: set.h:249
void differentiate(const set &differ_with)
Makes "this" set equal to the difference of "this" and "differ_with".
Definition: set.h:304
bool non_empty() const
Returns true if the set has some elements.
Definition: set.h:52
void unionize(const set &union_with)
Makes "this" set a union of "this" and "union_with".
Definition: set.h:286
set operator-(const set &dw) const
A synonym for difference.
Definition: set.h:111
int find(const contents &to_find) const
Returns the integer index of the item "to_find" in this set.
Definition: set.h:240
bool empty() const
Returns true if the set has no elements.
Definition: set.h:50
set operator+(const set &uw) const
A synonym for set_union.
Definition: set.h:92
void clear()
Empties out this set.
Definition: set.h:55
An array of strings with some additional helpful methods.
Definition: string_array.h:32
A simple object that wraps a templated set of strings.
Definition: set.h:168
virtual bool unpack(basis::byte_array &packed_form)
Restores the packable from the "packed_form".
Definition: set.h:197
virtual int packed_size() const
Estimates the space needed for the packed structure.
Definition: set.h:199
DEFINE_CLASS_NAME("string_set")
virtual void pack(basis::byte_array &packed_form) const
Creates a packed form of the packable object in "packed_form".
Definition: set.h:195
string_set()
Constructs an empty set of strings.
Definition: set.h:170
bool operator==(const string_set &compare) const
Definition: set.h:182
string_set(const set< basis::astring > &to_copy)
Constructs a copy of the "to_copy" array.
Definition: set.h:172
string_set(const string_array &to_copy)
Definition: set.h:175
Constants and objects used throughout HOOPLE.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned int un_int
Abbreviated name for unsigned integers.
Definition: definitions.h:62
unsigned short un_short
Abbreviated name for unsigned short integers.
Definition: definitions.h:64
A dynamic container class that holds any kind of object via pointers.
Definition: amorph.h:55
bool unpack(basis::byte_array &packed_form, set< contents > &to_unpack)
provides a way to unpack any set that stores packable objects.
Definition: set.h:139
bool obscure_detach(byte_array &packed_form, un_int &to_detach)
shifts the number back and checks validity, false returned if corrupted.
void obscure_attach(byte_array &packed_form, un_int to_attach)
like the normal attach but shifts in some recognizable sentinel data.
void pack(basis::byte_array &packed_form, const set< contents > &to_pack)
provides a way to pack any set that stores packable objects.
Definition: set.h:131
string_array(1, math_list))) const char *addr_list[]