Evo C++ Library v0.5.1
map.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_map_h
9 #define INCL_evo_map_h
10 
11 #include "type.h"
12 #include "pair.h"
13 #include "strtok.h"
14 
15 namespace evo {
18 
20 
128 template<class TKey, class TValue, class TSize=SizeT>
129 class Map {
130 public:
134  typedef TSize Size;
135  typedef TKey Key;
136  typedef TValue Value;
138 
139  // Iterator support types
141  struct IterKey {
142  Size a, b;
143 
144  IterKey(Size a=0, Size b=0) : a(a), b(b)
145  { }
146  IterKey(const IterKey& src)
147  { memcpy(this, &src, sizeof(src)); }
148  IterKey& operator=(const IterKey& src)
149  { memcpy(this, &src, sizeof(src)); return *this; }
150  };
151  typedef Pair<const Key,Value> IterItem;
156 
157 #if defined(EVO_CPP11)
160 
162  struct InitPair {
165 
170  InitPair(KeyPass key, ValuePass value) : key(key), value(value) {
171  }
172 
173  InitPair() = delete;
174  InitPair(const InitPair&) = delete;
175  InitPair& operator=(const InitPair&) = delete;
176  };
177 #endif
178 
179 protected:
180  Size size_;
181  bool ordered_;
182 
184  Map() : size_(0), ordered_(true)
185  { }
186 
190  Map(bool ordered) : size_(0), ordered_(ordered)
191  { }
192 
194  Map(const Map&) EVO_ONCPP11(= delete);
195 
201  virtual const Item* getiter(IterKey& iterkey, const Key& key) const = 0;
202 
203 public:
205  virtual ~Map()
206  { }
207 
209  const MapBaseType& asconst() const {
210  return *this;
211  }
212 
213  // SET
214 
219  MapBaseType& operator=(const MapBaseType& src) {
220  set(src);
221  return *this;
222  }
223 
227  virtual MapBaseType& set() = 0;
228 
235  virtual MapBaseType& set(const MapBaseType& src) = 0;
236 
240  virtual MapBaseType& setempty() = 0;
241 
247  virtual MapBaseType& clear() = 0;
248 
249  // INFO
250 
255  bool ordered() const
256  { return ordered_; }
257 
261  virtual bool null() const = 0;
262 
266  bool empty() const
267  { return (size_ == 0); }
268 
272  Size size() const
273  { return size_; }
274 
280  virtual bool shared() const = 0;
281 
285  virtual Size capacity() const = 0;
286 
287  // COMPARE
288 
293  bool operator==(const MapBaseType& map) const {
294  bool result = false;
295  if (this == &map)
296  result = true;
297  else if (this->null())
298  result = map.null();
299  else if (map.null())
300  result = false;
301  else if (size_ == map.size_) {
302  result = true;
303  if (size_ > 0) {
304  const Value* val;
305  for (Iter iter(*this); iter; ++iter) {
306  val = map.find(iter->first);
307  if (val == NULL || !(*val == iter->second)) {
308  result = false;
309  break;
310  }
311  }
312  }
313  }
314  return result;
315  }
316 
321  bool operator!=(const MapBaseType& map) const
322  { return !this->operator==(map); }
323 
324  // FIND
325 
327  Iter cbegin() const
328  { return Iter(*this); }
329 
331  Iter cend() const
332  { return Iter(); }
333 
335  IterM begin()
336  { return IterM(*this); }
337 
339  Iter begin() const
340  { return Iter(*this); }
341 
343  IterM end()
344  { return IterM(); }
345 
347  Iter end() const
348  { return Iter(); }
349 
356  virtual bool contains(const Key& key) const = 0;
357 
362  virtual const Value* find(const Key& key) const = 0;
363 
368  virtual Value* findM(const Key& key) = 0;
369 
374  Iter iter(const Key& key) const {
375  IterKey iterkey;
376  const Item* item = this->getiter(iterkey, key);
377  return (item != NULL ? Iter(*this, iterkey, (IterItem*)item) : Iter(*this, iterEND));
378  }
379 
384  IterM iterM(const Key& key) {
385  IterKey iterkey;
386  Item* item = (Item*)this->getiter(iterkey, key);
387  return (item != NULL ? IterM(*this, iterkey, (IterItem*)item) : IterM(*this, iterEND));
388  }
389 
397  virtual Item& getitem(const Key& key, bool* created=NULL) = 0;
398 
406  Value& get(const Key& key, bool* created=NULL)
407  { return this->getitem(key, created).second; }
408 
409  // INFO_SET
410 
418  Value& operator[](const Key& key)
419  { return this->getitem(key, NULL).second; }
420 
429  virtual MapBaseType& unshare() = 0;
430 
437  virtual MapBaseType& capacity(Size size) = 0;
438 
445  virtual MapBaseType& capacitymin(Size min) = 0;
446 
453  virtual MapBaseType& compact()
454  { return *this; }
455 
464  MapBaseType& reserve(Size size)
465  { this->capacitymin(size_ + size); return *this; }
466 
467  // ADD
468 
475  virtual Item& add(const Key& key, const Value& value, bool update=true) = 0;
476 
482  virtual Item& add(const Item& item, bool update=true) = 0;
483 
489  virtual MapBaseType& add(const MapBaseType& map, bool update=true) = 0;
490 
502  template<class T>
503  Size addsplit(const T& str, char delim=',', char kvdelim='=') {
504  char delims_buf[2];
505  delims_buf[0] = delim;
506  delims_buf[1] = kvdelim;
507  const SubString delims(delims_buf, 2);
508  const SubString EMPTY;
509 
510  Size count = 0;
511  bool created;
512  StrTok tok(str);
513  for (; tok.nextany(delims); ++count) {
514  Value& val = get(tok.value().convert<Key>(), &created);
515  if (tok.delim().null() || *tok.delim() == delim) {
516  if (!created)
518  } else {
519  tok.next(delim);
520  val = tok.value().convert<Value>();
521  }
522  }
523  return count;
524  }
525 
526  // REMOVE
527 
534  virtual bool remove(const Key& key) = 0;
535 
544  virtual bool remove(IterM& iter, IteratorDir dir=iterNONE) = 0;
545 
546  // INTERNAL
547 
548  // Iterator support methods (used internally)
550  virtual void iterInitMutable() = 0;
551  virtual const IterItem* iterFirst(IterKey& key) const = 0;
552  virtual const IterItem* iterNext(IterKey& key) const = 0;
553  virtual const IterItem* iterLast(IterKey& key) const = 0;
554  virtual const IterItem* iterPrev(IterKey& key) const = 0;
556 };
557 
559 
569 template<class T> inline bool map_contains(const T& map, const typename T::Key& key, const typename T::Value& value) {
570  const typename T::Value* p = map.find(key);
571  return (p != NULL && *p == value);
572 }
573 
588 template<class TMap>
589 inline SubString lookupsub(const TMap& map, const typename TMap::Key& key) {
590  const typename TMap::Value* val = map.find(key);
591  return (val != NULL ? SubString(*val) : SubString());
592 }
593 
594 // STD/STL container helpers
595 
611 template<class TMap>
612 inline SubString stdlookupsub(const TMap& map, const typename TMap::key_type& key) {
613  typename TMap::const_iterator i = map.find(key);
614  return (i != map.end() ? SubString(i->second) : SubString());
615 }
616 
626 template<class TMap>
627 inline const typename TMap::mapped_type* stdlookup(const TMap& map, const typename TMap::key_type& key) {
628  typename TMap::const_iterator i = map.find(key);
629  return (i != map.end() ? &i->second : NULL);
630 }
631 
641 template<class TMap>
642 inline typename TMap::mapped_type* stdlookupM(TMap& map, const typename TMap::key_type& key) {
643  typename TMap::iterator i = map.find(key);
644  return (i != map.end() ? &i->second : NULL);
645 }
646 
648 
649 }
650 #endif
typename DataCopy< SigNumType >::PassType KeyPass
Key type for passing through InitPair (C++11)
Definition: map.h:158
C convert() const
Convert string to value of given type.
Definition: substring.h:1427
Iter iter(const Key &key) const
Find (lookup) iterator for given key (const).
Definition: map.h:374
virtual bool null() const =0
Get whether map is null.
Pair< Key, Value > Item
Item type (key/value pair)
Definition: map.h:137
Iter cend() const
Get iterator at end (const).
Definition: map.h:331
Map(bool ordered)
Constructor.
Definition: map.h:190
Map< TKey, TValue, TSize > ThisType
This type.
Definition: map.h:132
Map< TKey, TValue, TSize > MapBaseType
Map base type
Definition: map.h:133
SubString lookupsub(const TMap &map, const typename TMap::Key &key)
Lookup (find) map value as SubString for given key.
Definition: map.h:589
bool null() const
Get whether null.
Definition: type.h:318
Size size_
Map size (number of items, automatically updated by concrete set members)
Definition: map.h:180
Iter cbegin() const
Get iterator at first item (const).
Definition: map.h:327
Evo string tokenizers.
virtual Item & getitem(const Key &key, bool *created=NULL)=0
Get map item for key (mutable).
#define EVO_CONTAINER_TYPE
Identify current class/struct as an EvoContainer.
Definition: meta.h:482
Size addsplit(const T &str, char delim=',', char kvdelim='=')
Split delimited string into map key/value items.
Definition: map.h:503
#define EVO_ONCPP11(EXPR)
Compile EXPR only if C++11 support is detected, otherwise this is a no-op.
Definition: sys.h:259
IterM end()
Get iterator at end.
Definition: map.h:343
Optimized data copy helpers.
Definition: container.h:543
virtual bool contains(const Key &key) const =0
Get whether map contains the given key.
bool operator==(const MapBaseType &map) const
Equality operator.
Definition: map.h:293
virtual const Item * getiter(IterKey &iterkey, const Key &key) const =0
Used by base class to get data to initialize iterator.
KeyPass key
Map key value
Definition: map.h:163
bool first()
Go to first item (used internally).
Definition: iter.h:465
virtual MapBaseType & clear()=0
Clear by removing all items.
Iter begin() const
Get iterator at first item (const).
Definition: map.h:339
MapBaseType & reserve(Size size)
Reserve space for new items.
Definition: map.h:464
IteratorBi< ThisType > IterM
Iterator (mutable) - IteratorBi.
Definition: map.h:155
TKey Key
Key type.
Definition: map.h:135
virtual Size capacity() const =0
Get map capacity.
virtual ~Map()
Destructor.
Definition: map.h:205
typename DataCopy< Handler >::PassType ValuePass
Value type for passing through InitPair (C++11)
Definition: map.h:159
virtual MapBaseType & setempty()=0
Set as empty but not null.
Bidirectional iterator.
Definition: iter.h:611
Key find(char ch) const
Find first occurrence of character with forward search.
Definition: substring.h:681
TValue Value
Value type.
Definition: map.h:136
const SubString & value() const
Get current token value from last call to next().
Definition: strtok.h:42
ValuePass value
Map value
Definition: map.h:164
MapBaseType & operator=(const MapBaseType &src)
Assignment operator.
Definition: map.h:219
SubString stdlookupsub(const TMap &map, const typename TMap::key_type &key)
Lookup (find) STL map value as SubString for given key.
Definition: map.h:612
IteratorDir
Iterator direction value.
Definition: iter.h:27
static void set_default(T &val)
Set value to default.
Definition: container.h:559
bool ordered() const
Get whether map is ordered.
Definition: map.h:255
const TMap::mapped_type * stdlookup(const TMap &map, const typename TMap::key_type &key)
Lookup (find) STL map value for given key (const).
Definition: map.h:627
Evo C++ Library namespace.
Definition: alg.h:11
No iterator direction.
Definition: iter.h:28
IteratorBi< ThisType >::Const Iter
Iterator (const) - IteratorBi.
Definition: map.h:154
bool operator!=(const MapBaseType &map) const
Inequality operator.
Definition: map.h:321
const MapBaseType & asconst() const
Explicitly use a const reference to this.
Definition: map.h:209
TSize Size
Size type for size values (must be unsigned integer) – default: SizeT.
Definition: map.h:134
Evo Pair class.
virtual MapBaseType & capacitymin(Size min)=0
Set map capacity to at least given minimum.
bool nextany(const StringBase &delims)
Find next token using any of given delimiters.
Definition: strtok.h:321
Char delim() const
Get current delimiter before next token.
Definition: strtok.h:36
Size size() const
Get map size (number of items).
Definition: map.h:272
bool next(char delim)
Find next token using delimiter.
Definition: strtok.h:184
Iter end() const
Get iterator at end (const).
Definition: map.h:347
virtual const Value * find(const Key &key) const =0
Find (lookup) value for given key (const).
End iterator position.
Definition: iter.h:23
virtual Value * findM(const Key &key)=0
Find (lookup) value for given key (mutable).
virtual MapBaseType & unshare()=0
Make data unique by allocating new buffer, if needed (modifier).
String forward tokenizer.
Definition: strtok.h:112
virtual MapBaseType & compact()
Reduce capacity to fit current size (modifier).
Definition: map.h:453
virtual Item & add(const Key &key, const Value &value, bool update=true)=0
Add or update using given key and value.
bool ordered_
Whether map is ordered (items are kept in order by key)
Definition: map.h:181
bool empty() const
Get whether map is empty (size is 0).
Definition: map.h:266
IterM iterM(const Key &key)
Find (lookup) iterator for given key (mutable).
Definition: map.h:384
TMap::mapped_type * stdlookupM(TMap &map, const typename TMap::key_type &key)
Lookup (find) STL map value for given key (mutable).
Definition: map.h:642
Stores a key/value pair of independent objects or values.
Definition: pair.h:32
Reference and access existing string data.
Definition: substring.h:229
Value & operator[](const Key &key)
Get item value for key (mutable).
Definition: map.h:418
Associative container holding key/value pairs for fast lookup.
Definition: map.h:129
T & min(T &a, T &b)
Returns lowest of given values.
Definition: alg.h:26
TB second
Second value (same as b() and value())
Definition: pair.h:43
Initializer key/value pair, used with initializer (C++11).
Definition: map.h:162
virtual bool shared() const =0
Get whether shared.
bool map_contains(const T &map, const typename T::Key &key, const typename T::Value &value)
Check whether map contains key with matching value.
Definition: map.h:569
IterM begin()
Get iterator at first item (mutable).
Definition: map.h:335
InitPair(KeyPass key, ValuePass value)
Constructor with key and value.
Definition: map.h:170
Evo basic types and traits.
Map()
Constructor.
Definition: map.h:184