strusBase  0.17
symbolTable.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_BASE_SYMBOL_TABLE_HPP_INCLUDED
10 #define _STRUS_BASE_SYMBOL_TABLE_HPP_INCLUDED
11 #include "strus/base/crc32.hpp"
12 #include <list>
13 #include <vector>
14 #include <string>
15 #include <cstring>
16 #include <stdexcept>
17 #include <new>
18 
19 namespace strus
20 {
22 class ErrorBufferInterface;
24 class StringMapKeyBlockList;
26 class InternalMap;
27 
29 {
30 public:
31  explicit BlockAllocator( ErrorBufferInterface* errorhnd_)
32  :m_errorhnd(errorhnd_),m_blocks(createBlocks())
33  {
34  if (!m_blocks)
35  {
36  deleteBlocks( m_blocks);
37  throw std::bad_alloc();
38  }
39  }
41 
42  const char* allocStringCopy( const char* str, std::size_t size);
43  const char* allocStringCopy( const std::string& str);
44 
45 private:
46  static StringMapKeyBlockList* createBlocks();
47  static void deleteBlocks( StringMapKeyBlockList* ptr);
48 
49 private:
50  ErrorBufferInterface* m_errorhnd;
51  StringMapKeyBlockList* m_blocks;
52 };
53 
54 
58 {
59 public:
61  explicit SymbolTable( ErrorBufferInterface* errorhnd_)
62  :m_errorhnd(errorhnd_),m_map(createInternalMap()),m_keystring_blocks(createKeystringBlocks()),m_isnew(false)
63  {
64  if (!m_keystring_blocks || !m_map)
65  {
66  if (!m_keystring_blocks) deleteKeystringBlocks( m_keystring_blocks);
67  if (!m_map) deleteInternalMap( m_map);
68  throw std::bad_alloc();
69  }
70  }
71 
73  struct Key
74  {
75  const char* str;
76  std::size_t len;
77 
78  Key()
79  :str(0),len(0){}
80  Key( const char* str_, std::size_t len_)
81  :str(str_),len(len_){}
82  Key( const Key& o)
83  :str(o.str),len(o.len){}
84  };
85  struct MapKeyLess
86  {
87  bool operator()( const Key& a, const Key& b) const
88  {
89  if (a.len < b.len) return true;
90  if (a.len > b.len) return false;
91  return std::memcmp( a.str, b.str, a.len) < 0;
92  }
93  };
94  struct MapKeyEqual
95  {
96  bool operator()( const Key& a, const Key& b) const
97  {
98  return a.len == b.len && std::memcmp( a.str, b.str, a.len) == 0;
99  }
100  };
101  struct HashFunc
102  {
103  int operator()( const Key& key)const
104  {
105  return utils::Crc32::calc( key.str, key.len);
106  }
107  };
108 
109 public:
111  ~SymbolTable();
112 
116  uint32_t getOrCreate( const std::string& key);
121  uint32_t getOrCreate( const char* key, std::size_t keysize);
122 
126  uint32_t get( const std::string& key) const;
131  uint32_t get( const char* key, std::size_t keysize) const;
132 
136  const char* key( const uint32_t& id) const;
137 
140  std::size_t size() const
141  {
142  return m_invmap.size();
143  }
144 
147  bool empty() const
148  {
149  return m_invmap.empty();
150  }
151 
152  typedef std::vector<const char*>::const_iterator const_inv_iterator;
153 
156  {
157  return m_invmap.begin();
158  }
161  {
162  return m_invmap.end();
163  }
164 
167  bool isNew() const
168  {
169  return m_isnew;
170  }
171 
177  void* allocBlock( unsigned int blocksize, unsigned int elemsize);
178 
180  void clear();
181 
182 private:
183 #if __cplusplus >= 201103L
184  SymbolTable( const SymbolTable&) = delete;
185  void operator=( const SymbolTable&) = delete;
186 #else
187  SymbolTable( const SymbolTable&){}
188  void operator=( const SymbolTable&){}
189 #endif
190  static StringMapKeyBlockList* createKeystringBlocks();
191  static void deleteKeystringBlocks( StringMapKeyBlockList* ptr);
192  static InternalMap* createInternalMap();
193  static void deleteInternalMap( InternalMap* ptr);
194 
195 private:
196  ErrorBufferInterface* m_errorhnd;
197  InternalMap* m_map;
198  std::vector<const char*> m_invmap;
199  StringMapKeyBlockList* m_keystring_blocks;
200  bool m_isnew;
201 };
202 
203 }//namespace
204 #endif
205 
206 
bool operator()(const Key &a, const Key &b) const
Definition: symbolTable.hpp:87
void * allocBlock(unsigned int blocksize, unsigned int elemsize)
Allocate a block in the context of the symbol table.
bool empty() const
Evaluate if the symbol table is empty, without any definitions.
Definition: symbolTable.hpp:147
static uint32_t calc(const char *blk, std::size_t blksize)
const_inv_iterator inv_end() const
Get end iterator of inv.
Definition: symbolTable.hpp:160
Key()
Definition: symbolTable.hpp:78
std::size_t len
Definition: symbolTable.hpp:76
Definition: symbolTable.hpp:101
std::size_t size() const
Get number of elements defined.
Definition: symbolTable.hpp:140
const char * str
Definition: symbolTable.hpp:75
int operator()(const Key &key) const
Definition: symbolTable.hpp:103
void clear()
Free all keys allocated.
const char * key(const uint32_t &id) const
Inverse lookup, get key of handle.
Interface for reporting and catching errors in modules.
Definition: errorBufferInterface.hpp:24
Definition: symbolTable.hpp:94
Key of symbol table.
Definition: symbolTable.hpp:73
Definition: symbolTable.hpp:85
Key(const char *str_, std::size_t len_)
Definition: symbolTable.hpp:80
Key(const Key &o)
Definition: symbolTable.hpp:82
SymbolTable(ErrorBufferInterface *errorhnd_)
Default constructor.
Definition: symbolTable.hpp:61
Map of strings to indices not freed till end of table life time.
Definition: symbolTable.hpp:57
const_inv_iterator inv_begin() const
Get start iterator of inv.
Definition: symbolTable.hpp:155
BlockAllocator(ErrorBufferInterface *errorhnd_)
Definition: symbolTable.hpp:31
bool isNew() const
Evaluate if the last symbol retrieved with getOrCreate was new.
Definition: symbolTable.hpp:167
~SymbolTable()
Destructor.
bool operator()(const Key &a, const Key &b) const
Definition: symbolTable.hpp:96
const char * allocStringCopy(const char *str, std::size_t size)
Definition: symbolTable.hpp:28
uint32_t getOrCreate(const std::string &key)
Get handle ( >= 1) associated with key, create one if not defined.
std::vector< const char * >::const_iterator const_inv_iterator
Definition: symbolTable.hpp:152