Evo C++ Library v0.5.1
set.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_set_h
9 #define INCL_evo_set_h
10 
11 #include "type.h"
12 #include "strtok.h"
13 
14 namespace evo {
17 
19 
118 template<class TKey, class TSize=SizeT>
119 class Set {
120 public:
124  typedef TSize Size;
125  typedef TKey Key;
126  typedef TKey Value;
127  typedef TKey Item;
128 
129  // Iterator support types
131  struct IterKey {
132  Size a, b;
133 
134  IterKey(Size a=0, Size b=0) : a(a), b(b)
135  { }
136  IterKey(const IterKey& src)
137  { memcpy(this, &src, sizeof(src)); }
138  };
139  typedef Item IterItem;
144 
145 protected:
146  Size size_;
147 
149  Set() : size_(0)
150  { }
151 
153  Set(const ThisType&) EVO_ONCPP11(= delete);
154 
160  virtual const Value* getiter(IterKey& iterkey, const Value& value) const = 0;
161 
162 public:
164  virtual ~Set()
165  { }
166 
168  const SetBaseType& asconst() const {
169  return *this;
170  }
171 
172  // SET
173 
178  SetBaseType& operator=(const SetBaseType& src) {
179  set(src);
180  return *this;
181  }
182 
186  virtual SetBaseType& set() = 0;
187 
194  virtual SetBaseType& set(const SetBaseType& src) = 0;
195 
199  virtual SetBaseType& setempty() = 0;
200 
206  virtual SetBaseType& clear() = 0;
207 
208  // INFO
209 
214  virtual bool ordered() const = 0;
215 
219  virtual bool null() const = 0;
220 
224  bool empty() const
225  { return (size_ == 0); }
226 
230  Size size() const
231  { return size_; }
232 
238  virtual bool shared() const = 0;
239 
243  virtual Size capacity() const = 0;
244 
245  // COMPARE
246 
251  bool operator==(const SetBaseType& set) const {
252  bool result = false;
253  if (this == &set)
254  result = true;
255  else if (this->null())
256  result = set.null();
257  else if (set.null())
258  result = false;
259  else if (size_ == set.size_) {
260  result = true;
261  for (Iter iter(*this); iter; ++iter) {
262  if (!set.contains(*iter)) { // lookup rather than rely on ordering
263  result = false;
264  break;
265  }
266  }
267  }
268  return result;
269  }
270 
275  bool operator!=(const SetBaseType& set) const
276  { return !this->operator==(set); }
277 
278  // CONTAINS
279 
284  virtual bool contains(const Value& value) const = 0;
285 
286  // FIND
287 
289  Iter cbegin() const
290  { return Iter(*this); }
291 
293  Iter cend() const
294  { return Iter(); }
295 
297  IterM begin()
298  { return IterM(*this); }
299 
301  Iter begin() const
302  { return Iter(*this); }
303 
305  IterM end()
306  { return IterM(); }
307 
309  Iter end() const
310  { return Iter(); }
311 
316  Iter iter(const Value& value) const {
317  IterKey iterkey;
318  const Item* item = this->getiter(iterkey, value);
319  return (item != NULL ? Iter(*this, iterkey, (IterItem*)item) : Iter(*this, iterEND));
320  }
321 
328  IterM iterM(const Value& value) {
329  IterKey iterkey;
330  Item* item = (Item*)this->getiter(iterkey, value);
331  return (item != NULL ? IterM(*this, iterkey, (IterItem*)item) : IterM(*this, iterEND));
332  }
333 
342  virtual Value& get(const Value& value, bool* created=NULL) = 0;
343 
344  // INFO_SET
345 
354  virtual SetBaseType& unshare() = 0;
355 
362  virtual SetBaseType& capacity(Size size) = 0;
363 
370  virtual SetBaseType& capacitymin(Size min) = 0;
371 
378  virtual SetBaseType& compact()
379  { return *this; }
380 
389  SetBaseType& reserve(Size size) {
390  this->capacitymin(size_ + size);
391  return *this;
392  }
393 
394  // ADD
395 
401  virtual Value& add(const Value& value, bool update=false) = 0;
402 
409  template<class T>
410  Size addfrom(const T& items, bool update=false) {
411  if (this != &items) {
412  const Size start_size = size_;
413  reserve(items.size());
414  for (typename T::Iter iter(items); iter; ++iter)
415  this->add(*iter, update);
416  return (size_ - start_size);
417  }
418  return 0;
419  }
420 
431  template<class T>
432  Size addsplit(const T& str, char delim=',') {
433  Size count = 0;
434  StrTok tok(str);
435  for (; tok.next(delim); ++count)
436  this->add(tok.value().convert<Value>());
437  return count;
438  }
439 
440  // REMOVE
441 
448  virtual bool remove(const Value& value) = 0;
449 
458  virtual bool remove(IterM& iter, IteratorDir dir=iterNONE) = 0;
459 
460  // INTERNAL
461 
462  // Iterator support methods (used internally)
464  virtual void iterInitMutable() = 0;
465  virtual const IterItem* iterFirst(IterKey& key) const = 0;
466  virtual const IterItem* iterNext(IterKey& key) const = 0;
467  virtual const IterItem* iterLast(IterKey& key) const = 0;
468  virtual const IterItem* iterPrev(IterKey& key) const = 0;
470 };
471 
473 
474 }
475 #endif
C convert() const
Convert string to value of given type.
Definition: substring.h:1427
Associative container with unique values for fast lookup.
Definition: set.h:119
virtual bool shared() const =0
Get whether shared.
IteratorBi< ThisType >::Const Iter
Iterator (const) - IteratorBi.
Definition: set.h:142
bool empty() const
Get whether set is empty (size is 0).
Definition: set.h:224
Size addfrom(const T &items, bool update=false)
Add items from given list or set.
Definition: set.h:410
Evo string tokenizers.
#define EVO_CONTAINER_TYPE
Identify current class/struct as an EvoContainer.
Definition: meta.h:482
virtual SetBaseType & clear()=0
Clear by removing all items.
IterM iterM(const Value &value)
Find (lookup) iterator for given value (mutable).
Definition: set.h:328
TKey Item
Item type (same as Value)
Definition: set.h:127
Set< TKey, TSize > SetBaseType
Set base type
Definition: set.h:123
Iter end() const
Get iterator at end (const).
Definition: set.h:309
#define EVO_ONCPP11(EXPR)
Compile EXPR only if C++11 support is detected, otherwise this is a no-op.
Definition: sys.h:259
Size size() const
Get set size (number of items).
Definition: set.h:230
virtual bool contains(const Value &value) const =0
Get whether the set contains the given value.
virtual bool null() const =0
Get whether set is null.
SetBaseType & reserve(Size size)
Reserve space for new items.
Definition: set.h:389
IterM begin()
Get iterator at first item (mutable).
Definition: set.h:297
Size size_
Set size (number of items, automatically updated by concrete set members)
Definition: set.h:146
virtual SetBaseType & compact()
Reduce capacity to fit current size (modifier).
Definition: set.h:378
Iter cend() const
Get iterator at end (const).
Definition: set.h:293
virtual const Value * getiter(IterKey &iterkey, const Value &value) const =0
Used by base class to get data to initialize iterator.
IterM end()
Get iterator at end.
Definition: set.h:305
IteratorBi< ThisType > IterM
Iterator (mutable) - IteratorBi.
Definition: set.h:143
virtual bool ordered() const =0
Get whether set is ordered.
Bidirectional iterator.
Definition: iter.h:611
Iter iter(const Value &value) const
Find (lookup) iterator for given value (const).
Definition: set.h:316
const SetBaseType & asconst() const
Explicitly use a const reference to this.
Definition: set.h:168
const SubString & value() const
Get current token value from last call to next().
Definition: strtok.h:42
IteratorDir
Iterator direction value.
Definition: iter.h:27
Set< TKey, TSize > ThisType
This type.
Definition: set.h:122
virtual SetBaseType & unshare()=0
Make data unique by allocating new buffer, if needed (modifier).
Size addsplit(const T &str, char delim=',')
Split delimited string into set items.
Definition: set.h:432
virtual Size capacity() const =0
Get set capacity.
Evo C++ Library namespace.
Definition: alg.h:11
No iterator direction.
Definition: iter.h:28
Iter cbegin() const
Get iterator at first item (const).
Definition: set.h:289
TKey Key
Key type (same as Value)
Definition: set.h:125
TSize Size
Size type for size values (must be unsigned integer) – default: SizeT.
Definition: set.h:124
bool next(char delim)
Find next token using delimiter.
Definition: strtok.h:184
bool operator!=(const SetBaseType &set) const
Inequality operator.
Definition: set.h:275
End iterator position.
Definition: iter.h:23
virtual SetBaseType & setempty()=0
Set as empty but not null.
String forward tokenizer.
Definition: strtok.h:112
TKey Value
Value type.
Definition: set.h:126
virtual ~Set()
Destructor.
Definition: set.h:164
SetBaseType & operator=(const SetBaseType &src)
Assignment operator.
Definition: set.h:178
bool operator==(const SetBaseType &set) const
Equality operator.
Definition: set.h:251
Iter begin() const
Get iterator at first item (const).
Definition: set.h:301
T & min(T &a, T &b)
Returns lowest of given values.
Definition: alg.h:26
Set()
Constructor.
Definition: set.h:149
virtual Value & add(const Value &value, bool update=false)=0
Add or update using given item.
Evo basic types and traits.
virtual SetBaseType & capacitymin(Size min)=0
Set capacity to at least given minimum for set (modifier).