TeoCCl library  0.1.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
map.hpp
Go to the documentation of this file.
1 /*
2  * The MIT License
3  *
4  * Copyright 2016-2018 Kirill Scherba <kirill@scherba.ru>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  * \file map
25  * \Author max <mpano91@gmail.com>
26  *
27  * Teo ccl Map C++ wrapper
28  *
29  * Created on March 25, 2018, 2:25 PM
30  */
31 
32 #ifndef MAP_HXX
33 #define MAP_HXX
34 
35 #include <string>
36 #include <stdlib.h>
37 
38 #include "teoccl/map.h"
39 
40 namespace teo {
41 
42  class Map {
43 
44  private:
45 
46  teoMap *map;
47 
48  public:
49 
50  Map() { map = teoMapNew(HASH_TABLE_SIZE, 1); }
51  Map(size_t size, int auto_resize_f)
52  {
53  map = teoMapNew(size, auto_resize_f);
54  }
55 
56  virtual ~Map() { teoMapDestroy(map); }
57 
58 
59  // size
60  inline size_t size() {
61  return teoMapSize(map);
62  }
63 
64  inline void mapClear() {
65  teoMapClear(map);
66  }
67 
68  inline void *getFirst(size_t *data_length) {
69  return teoMapGetFirst(map, data_length);
70  }
71 
72 
73  // add
74  inline void *add(const uint8_t *key, size_t key_length,
75  const uint8_t *data, size_t data_length) {
76  return teoMapAdd(map, key, key_length, data, data_length);
77  }
78 
79  inline void *add(const std::string& key, const std::string& data) {
80  return add((const uint8_t *)key.c_str(), key.size() + 1,
81  (const uint8_t *)data.c_str(), data.size() + 1);
82  }
83 
84  template<typename D>
85  void *add(const std::string& key, const D& data) {
86  return add((const uint8_t*)key.c_str(), key.size() + 1,
87  (const uint8_t *)&data, sizeof(D));
88  }
89 
90  template<typename K, typename D>
91  void *add(const K& key, const D& data) {
92  return add((const uint8_t*)&key, sizeof(K),
93  (const uint8_t *)&data, sizeof(D));
94  }
95 
96 
97  // get
98  inline void *get(const uint8_t *key, size_t key_length, size_t *data_length) {
99  return teoMapGet(map, key, key_length, data_length);
100  }
101 
102  inline void *get(const std::string& key, size_t *data_length) {
103  return get((const uint8_t *)key.c_str(), key.size() + 1, data_length);
104  }
105 
106  template<typename K>
107  void *get(const K& key, size_t *data_length) {
108  return get((const uint8_t *)&key, sizeof(K), data_length);
109  }
110 
111 
112  // delete
113  inline int del(const uint8_t *key, size_t key_length) {
114  return teoMapDelete(map, key, key_length);
115  }
116 
117  inline int del(const std::string& key) {
118  return del((const uint8_t *)key.c_str(), key.size() + 1);
119  }
120 
121  template<typename K>
122  int del(const K& key) {
123  return del((const uint8_t *)&key, sizeof(K));
124  }
125 
126 
127  // get iterator new(normal)
129  return teoMapIteratorNew(map);
130  }
131 
132  // get iterator new(reverse)
134  return teoMapIteratorReverseNew(map);
135  }
136 
137  // iterator next
139  return teoMapIteratorNext(it);
140  }
141 
142  // iterator previous
144  return teoMapIteratorPrev(it);
145  }
146 
147  // iterator free
148  inline int iteratorFree(teoMapIterator *it) {
149  return teoMapIteratorFree(it);
150  }
151 
152  // get iterator element
154  return teoMapIteratorElement(it);
155  }
156 
157  // get iterator element key
158  template<typename D>
159  inline D *mapItElKey(teoMapElementData *el, size_t *key_length = NULL) {
160  return (D *)teoMapIteratorElementKey(el, key_length);
161  }
162 
163  // get iterator element data
164  template<typename D>
165  inline D *mapItElData(teoMapElementData *el, size_t *data_length = NULL) {
166  return (D *)teoMapIteratorElementData(el, data_length);
167  }
168 
169  // foreach
170  inline int foreach(teoMapForeachFunction callback, void *user_data = NULL) {
171  return teoMapForeach(map, callback, user_data);
172  }
173 
174  };
175 
176 }
177 #endif /* MAP_HXX */
uint8_t * teoMapGet(teoMap *map, const uint8_t *key, size_t key_length, size_t *data_length)
Get key data from hash table.
Definition: map.c:345
teoMapIterator * iteratorReverse()
Definition: map.hpp:133
int(* teoMapForeachFunction)(teoMap *m, int idx, teoMapElementData *d, void *user_data)
Definition: map.h:228
teoMap * teoMapNew(size_t size, int auto_resize_f)
Create new map.
Definition: map.c:65
int teoMapDelete(teoMap *map, const uint8_t *key, size_t key_length)
Delete keys element from map.
Definition: map.c:373
teoMapIterator * teoMapIteratorReverseNew(teoMap *map)
Create new map reverse iterator.
Definition: map.c:432
Definition: map.hpp:42
int iteratorFree(teoMapIterator *it)
Definition: map.hpp:148
teoMapIterator * teoMapIteratorNew(teoMap *map)
Create new map iterator.
Definition: map.c:402
virtual ~Map()
Definition: map.hpp:56
Map()
Definition: map.hpp:50
teoMapElementData * teoMapIteratorNext(teoMapIterator *map_it)
Get next maps element.
Definition: map.c:477
int teoMapForeach(teoMap *m, teoMapForeachFunction callback, void *user_data)
Loop through map and call callback function with index and data in parameters.
Definition: map.c:537
void * getFirst(size_t *data_length)
Definition: map.hpp:68
size_t size()
Definition: map.hpp:60
D * mapItElKey(teoMapElementData *el, size_t *key_length=NULL)
Definition: map.hpp:159
teoMapIterator * iterator()
Definition: map.hpp:128
size_t teoMapSize(teoMap *map)
Get number of elements in TR-UPD map.
Definition: map.c:54
uint8_t * teoMapAdd(teoMap *map, const uint8_t *key, size_t key_length, const uint8_t *data, size_t data_length)
Add (or update) key data to the map.
Definition: map.c:282
Definition: map.h:48
teoMapElementData * iteratorNext(teoMapIterator *it)
Definition: map.hpp:138
void * add(const uint8_t *key, size_t key_length, const uint8_t *data, size_t data_length)
Definition: map.hpp:74
void teoMapClear(teoMap *map)
Definition: map.c:171
D * mapItElData(teoMapElementData *el, size_t *data_length=NULL)
Definition: map.hpp:165
void teoMapDestroy(teoMap *map)
Destroy map.
Definition: map.c:161
int teoMapIteratorFree(teoMapIterator *map_it)
Destroy map iterator.
Definition: map.c:462
Definition: map.h:58
Definition: map.h:67
uint8_t * teoMapGetFirst(teoMap *map, size_t *data_length)
Get first available element from hash table.
Definition: map.c:246
void * add(const K &key, const D &data)
Definition: map.hpp:91
Map module.
teoMapElementData * mapItEl(teoMapIterator *it)
Definition: map.hpp:153
int del(const K &key)
Definition: map.hpp:122
void mapClear()
Definition: map.hpp:64
int del(const uint8_t *key, size_t key_length)
Definition: map.hpp:113
void * add(const std::string &key, const std::string &data)
Definition: map.hpp:79
void * add(const std::string &key, const D &data)
Definition: map.hpp:85
teoMapElementData * teoMapIteratorPrev(teoMapIterator *map_it)
Get previous maps element.
Definition: map.c:506
Map(size_t size, int auto_resize_f)
Definition: map.hpp:51
int del(const std::string &key)
Definition: map.hpp:117
#define HASH_TABLE_SIZE
Definition: map.h:45
teoMapElementData * iterator_prev(teoMapIterator *it)
Definition: map.hpp:143