as_symboltable.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2012-2021 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. //
  24. // as_symboltable.h
  25. //
  26. // Created on: Jun 19, 2012
  27. // Author: Markus Lenger, a.k.a. mlengerx
  28. //
  29. // This class is used for fast symbol lookups while parsing or loading bytecode
  30. //
  31. #ifndef AS_SYMBOLTABLE_H
  32. #define AS_SYMBOLTABLE_H
  33. #include "as_config.h"
  34. #include "as_memory.h"
  35. #include "as_string.h"
  36. #include "as_map.h"
  37. #include "as_datatype.h"
  38. #include "as_namespace.h"
  39. BEGIN_AS_NAMESPACE
  40. // Interface to avoid nested templates which is not well supported by older compilers, e.g. MSVC6
  41. struct asIFilter
  42. {
  43. virtual bool operator()(const void*) const = 0;
  44. virtual ~asIFilter() {};
  45. };
  46. // forward declaration
  47. template<class T>
  48. class asCSymbolTable;
  49. // Iterator that allows iterating in index order
  50. template<class T, class T2 = T>
  51. class asCSymbolTableIterator
  52. {
  53. public:
  54. T2* operator*() const;
  55. T2* operator->() const;
  56. asCSymbolTableIterator<T, T2>& operator++(int);
  57. asCSymbolTableIterator<T, T2>& operator--(int);
  58. operator bool() const;
  59. int GetIndex() const { return m_idx; }
  60. private:
  61. friend class asCSymbolTable<T>;
  62. asCSymbolTableIterator(asCSymbolTable<T> *table);
  63. void Next();
  64. void Previous();
  65. asCSymbolTable<T>* m_table;
  66. unsigned int m_idx;
  67. };
  68. // Symbol table mapping namespace + name to symbols
  69. // The structure keeps the entries indexed in an array so the indices will not change
  70. // There is also a map for a quick lookup. The map supports multiple entries with the same name
  71. template<class T>
  72. class asCSymbolTable
  73. {
  74. public:
  75. typedef asCSymbolTableIterator<T, T> iterator;
  76. typedef asCSymbolTableIterator<T, const T> const_iterator;
  77. asCSymbolTable(asUINT initialCapacity = 0);
  78. int GetFirstIndex(const asSNameSpace *ns, const asCString &name, const asIFilter &comparator) const;
  79. int GetFirstIndex(const asSNameSpace *ns, const asCString &name) const;
  80. int GetLastIndex() const;
  81. int GetIndex(const T*) const;
  82. T* GetFirst(const asSNameSpace *ns, const asCString &name, const asIFilter &comparator) const;
  83. T* GetFirst(const asSNameSpace *ns, const asCString &name);
  84. const T* GetFirst(const asSNameSpace *ns, const asCString &name) const;
  85. T* Get(asUINT index);
  86. const T* Get(asUINT index) const;
  87. T* GetLast();
  88. const T* GetLast() const;
  89. const asCArray<asUINT> &GetIndexes(const asSNameSpace *ns, const asCString &name) const;
  90. asUINT Put(T* entry);
  91. asUINT GetSize() const;
  92. void SwapWith(asCSymbolTable<T> &other);
  93. void Clear();
  94. bool Erase(asUINT idx);
  95. void Allocate(asUINT elem_cnt, bool keep_data);
  96. iterator List();
  97. const_iterator List() const;
  98. private:
  99. // Don't allow assignment
  100. asCSymbolTable<T>& operator=(const asCSymbolTable<T> &other) { return *this; }
  101. friend class asCSymbolTableIterator<T, T>;
  102. friend class asCSymbolTableIterator<T, const T>;
  103. void GetKey(const T *entry, asSNameSpaceNamePair &key) const;
  104. bool CheckIdx(asUINT idx) const;
  105. asCMap<asSNameSpaceNamePair, asCArray<asUINT> > m_map;
  106. asCArray<T*> m_entries;
  107. unsigned int m_size;
  108. };
  109. template<class T>
  110. void asCSymbolTable<T>::SwapWith(asCSymbolTable<T> &other)
  111. {
  112. m_map.SwapWith(other.m_map);
  113. m_entries.SwapWith(other.m_entries);
  114. asUINT tmp = m_size;
  115. m_size = other.m_size;
  116. other.m_size = tmp;
  117. }
  118. // Constructor
  119. // initialCapacity gives the number of entries to allocate in advance
  120. template<class T>
  121. asCSymbolTable<T>::asCSymbolTable(asUINT initialCapacity) : m_entries(initialCapacity)
  122. {
  123. m_size = 0;
  124. }
  125. template<class T>
  126. int asCSymbolTable<T>::GetFirstIndex(
  127. const asSNameSpace *ns,
  128. const asCString &name,
  129. const asIFilter &filter) const
  130. {
  131. asSNameSpaceNamePair key(ns, name);
  132. asSMapNode<asSNameSpaceNamePair, asCArray<asUINT> > *cursor;
  133. if( m_map.MoveTo(&cursor, key) )
  134. {
  135. const asCArray<asUINT> &arr = m_map.GetValue(cursor);
  136. for( asUINT n = 0; n < arr.GetLength(); n++ )
  137. {
  138. T *entry = m_entries[arr[n]];
  139. if( entry && filter(entry) )
  140. return arr[n];
  141. }
  142. }
  143. return -1;
  144. }
  145. template<class T>
  146. const asCArray<asUINT> &asCSymbolTable<T>::GetIndexes(const asSNameSpace *ns, const asCString &name) const
  147. {
  148. asSNameSpaceNamePair key(ns, name);
  149. asSMapNode<asSNameSpaceNamePair, asCArray<asUINT> > *cursor;
  150. if( m_map.MoveTo(&cursor, key) )
  151. return m_map.GetValue(cursor);
  152. static asCArray<asUINT> dummy;
  153. return dummy;
  154. }
  155. template<class T>
  156. T* asCSymbolTable<T>::GetFirst(const asSNameSpace *ns, const asCString &name, const asIFilter &comp) const
  157. {
  158. int idx = GetFirstIndex(ns, name, comp);
  159. if (idx != -1) return m_entries[idx];
  160. return 0;
  161. }
  162. template<class T>
  163. int asCSymbolTable<T>::GetFirstIndex(const asSNameSpace *ns, const asCString &name) const
  164. {
  165. asSNameSpaceNamePair key(ns, name);
  166. asSMapNode<asSNameSpaceNamePair, asCArray<asUINT> > *cursor;
  167. if( m_map.MoveTo(&cursor, key) )
  168. return m_map.GetValue(cursor)[0];
  169. return -1;
  170. }
  171. // Find the index of a certain symbol
  172. // ATTENTION: this function has linear runtime complexity O(n)!!
  173. template<class T>
  174. int asCSymbolTable<T>::GetIndex(const T* entry) const
  175. {
  176. for( asUINT n = 0; n < m_entries.GetLength(); n++ )
  177. if( m_entries[n] == entry )
  178. return n;
  179. return -1;
  180. }
  181. template<class T>
  182. T* asCSymbolTable<T>::Get(asUINT idx)
  183. {
  184. if( !CheckIdx(idx) )
  185. return 0;
  186. return m_entries[idx];
  187. }
  188. template<class T>
  189. const T* asCSymbolTable<T>::Get(asUINT idx) const
  190. {
  191. return const_cast< asCSymbolTable<T>* >(this)->Get(idx);
  192. }
  193. template<class T>
  194. T* asCSymbolTable<T>::GetFirst(const asSNameSpace *ns, const asCString &name)
  195. {
  196. int idx = GetFirstIndex(ns, name);
  197. return Get(idx);
  198. }
  199. template<class T>
  200. const T* asCSymbolTable<T>::GetFirst(const asSNameSpace *ns, const asCString &name) const
  201. {
  202. return const_cast< asCSymbolTable<T>* >(this)->GetFirst(ns, name);
  203. }
  204. template<class T>
  205. T* asCSymbolTable<T>::GetLast()
  206. {
  207. return Get(GetLastIndex());
  208. }
  209. template<class T>
  210. const T* asCSymbolTable<T>::GetLast() const
  211. {
  212. return const_cast< asCSymbolTable<T>* >(this)->GetLast();
  213. }
  214. // Clear the symbol table
  215. // ATTENTION: The contained symbols are not rleased. This is up to the client
  216. template<class T>
  217. void asCSymbolTable<T>::Clear()
  218. {
  219. m_entries.SetLength(0);
  220. m_map.EraseAll();
  221. m_size = 0;
  222. }
  223. // Pre-allocate slots for elemCnt entries
  224. template<class T>
  225. void asCSymbolTable<T>::Allocate(asUINT elemCnt, bool keepData)
  226. {
  227. asASSERT( elemCnt >= m_entries.GetLength() );
  228. m_entries.Allocate(elemCnt, keepData);
  229. if( !keepData )
  230. m_map.EraseAll();
  231. }
  232. template<class T>
  233. bool asCSymbolTable<T>::Erase(asUINT idx)
  234. {
  235. if( !CheckIdx(idx) )
  236. {
  237. asASSERT(false);
  238. return false;
  239. }
  240. T *entry = m_entries[idx];
  241. asASSERT(entry);
  242. if( !entry )
  243. return false;
  244. // Remove the symbol from the lookup map
  245. asSNameSpaceNamePair key;
  246. GetKey(entry, key);
  247. asSMapNode<asSNameSpaceNamePair, asCArray<asUINT> > *cursor;
  248. if( m_map.MoveTo(&cursor, key) )
  249. {
  250. asCArray<asUINT> &arr = m_map.GetValue(cursor);
  251. arr.RemoveValue(idx);
  252. if( arr.GetLength() == 0 )
  253. m_map.Erase(cursor);
  254. }
  255. else
  256. asASSERT(false);
  257. // Remove the symbol from the indexed array
  258. if( idx == m_entries.GetLength() - 1 )
  259. m_entries.PopLast();
  260. else
  261. {
  262. // Must keep the array packed
  263. int prevIdx = int(m_entries.GetLength()-1);
  264. m_entries[idx] = m_entries.PopLast();
  265. // Update the index in the lookup map
  266. entry = m_entries[idx];
  267. GetKey(entry, key);
  268. if( m_map.MoveTo(&cursor, key) )
  269. {
  270. asCArray<asUINT> &arr = m_map.GetValue(cursor);
  271. arr[arr.IndexOf(prevIdx)] = idx;
  272. }
  273. else
  274. asASSERT(false);
  275. }
  276. m_size--;
  277. return true;
  278. }
  279. template<class T>
  280. asUINT asCSymbolTable<T>::Put(T *entry)
  281. {
  282. asUINT idx = m_entries.GetLength();
  283. asSNameSpaceNamePair key;
  284. GetKey(entry, key);
  285. asSMapNode<asSNameSpaceNamePair, asCArray<asUINT> > *cursor;
  286. if( m_map.MoveTo(&cursor, key) )
  287. m_map.GetValue(cursor).PushLast(idx);
  288. else
  289. {
  290. asCArray<asUINT> arr(1);
  291. arr.PushLast(idx);
  292. m_map.Insert(key, arr);
  293. }
  294. m_entries.PushLast(entry);
  295. m_size++;
  296. return idx;
  297. }
  298. // Return key for specified symbol (namespace and name are used to generate the key)
  299. template<class T>
  300. void asCSymbolTable<T>::GetKey(const T *entry, asSNameSpaceNamePair &key) const
  301. {
  302. key = asSNameSpaceNamePair(entry->nameSpace, entry->name);
  303. }
  304. template<class T>
  305. asUINT asCSymbolTable<T>::GetSize() const
  306. {
  307. return m_size;
  308. }
  309. template<class T>
  310. bool asCSymbolTable<T>::CheckIdx(asUINT idx) const
  311. {
  312. return idx < m_entries.GetLength();
  313. }
  314. template<class T>
  315. int asCSymbolTable<T>::GetLastIndex() const
  316. {
  317. int idx = int(m_entries.GetLength()) - 1;
  318. asASSERT( idx == -1 || m_entries[idx] );
  319. return idx;
  320. }
  321. template<class T>
  322. asCSymbolTableIterator<T, T> asCSymbolTable<T>::List()
  323. {
  324. return asCSymbolTableIterator<T, T>(this);
  325. }
  326. template<class T>
  327. typename asCSymbolTable<T>::const_iterator asCSymbolTable<T>::List() const
  328. {
  329. return asCSymbolTableIterator<T, const T>(const_cast< asCSymbolTable<T> *>(this));
  330. }
  331. /////////////////////////////////////////////////////////////////////////////////////////////////
  332. // Iterator
  333. template<class T, class T2>
  334. asCSymbolTableIterator<T, T2>::asCSymbolTableIterator(asCSymbolTable<T> *table) : m_table(table), m_idx(0)
  335. {
  336. asUINT sz = m_table->m_entries.GetLength();
  337. while( m_idx < sz && m_table->m_entries[m_idx] == 0 )
  338. m_idx++;
  339. }
  340. template<class T, class T2>
  341. T2* asCSymbolTableIterator<T, T2>::operator*() const
  342. {
  343. asASSERT(m_table->CheckIdx(m_idx));
  344. return m_table->m_entries[m_idx];
  345. }
  346. template<class T, class T2>
  347. T2* asCSymbolTableIterator<T, T2>::operator->() const
  348. {
  349. asASSERT(m_table->CheckIdx(m_idx));
  350. return m_table->m_entries[m_idx];
  351. }
  352. template<class T, class T2>
  353. asCSymbolTableIterator<T, T2>& asCSymbolTableIterator<T, T2>::operator++(int)
  354. {
  355. Next();
  356. return *this;
  357. }
  358. // Return true if more elements are following
  359. // ATTENTION: When deleting the object currently pointed to by this iterator this
  360. // method returns false even though there might be more elements in the list
  361. template<class T, class T2>
  362. asCSymbolTableIterator<T, T2>::operator bool() const
  363. {
  364. return m_idx < m_table->m_entries.GetLength() && m_table->m_entries[m_idx] != 0;
  365. }
  366. template<class T, class T2>
  367. void asCSymbolTableIterator<T, T2>::Next()
  368. {
  369. asUINT sz = m_table->m_entries.GetLength();
  370. m_idx++;
  371. while( m_idx < sz && m_table->m_entries[m_idx] == 0 )
  372. m_idx++;
  373. }
  374. template<class T, class T2>
  375. void asCSymbolTableIterator<T, T2>::Previous()
  376. {
  377. // overflow on stepping over first element
  378. asUINT sz = m_table->m_entries.GetLength();
  379. m_idx--;
  380. while( m_idx < sz && m_table->m_entries[m_idx] == 0 )
  381. m_idx--;
  382. }
  383. template<class T, class T2>
  384. asCSymbolTableIterator<T, T2>& asCSymbolTableIterator<T, T2>::operator--(int)
  385. {
  386. Previous();
  387. return *this;
  388. }
  389. END_AS_NAMESPACE
  390. #endif // AS_SYMBOLTABLE_H