Map.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /******************************************************************************
  2. Use 'Map' to quickly access custom data by creating it from specified key.
  3. Once 'Map' creates a resource, it will keep it in the memory for faster access.
  4. Objects in 'Map' containers are stored using 'Memx' container,
  5. which means that the memory address of the elements remains constant as long as the elements exist.
  6. /******************************************************************************/
  7. enum MAP_MODE : Byte // Map Mode
  8. {
  9. MAP_EXIT , // load data, Exit on fail
  10. MAP_NULL , // load data, null on fail
  11. MAP_DUMMY , // load data, dummy on fail (pointer to empty data, initialized with constructor but without the 'load' method)
  12. MAP_ALL_NULL , // don't load data, always return null
  13. MAP_ALL_DUMMY, // don't load data, always return dummy (pointer to empty data, initialized with constructor but without the 'load' method)
  14. };
  15. #if EE_PRIVATE
  16. enum MAP_ELM_FLAG // Map Element Flag
  17. {
  18. MAP_ELM_DUMMY =0x1,
  19. MAP_ELM_LOADING=0x2,
  20. };
  21. #endif
  22. /******************************************************************************/
  23. T2(KEY, DATA) struct Map : _Map // Map - container for dynamically created elements, consisting of unique keys and their corresponding data, Map is multi-threaded safe
  24. {
  25. struct Elm : _Map::Elm
  26. {
  27. DATA data;
  28. KEY key ;
  29. Desc desc;
  30. };
  31. // manage
  32. Map& clear(); // remove all elements
  33. Map& del (); // remove all elements and free helper memory
  34. // get
  35. Int elms ()C; // get number of elements in container
  36. Int dataSize()C; // get size of DATA element
  37. DATA* find (C KEY &key); // find element, don't create if not found, null on fail
  38. DATA* get (C KEY &key); // get element, create if not found, null on fail
  39. DATA* operator()(C KEY &key); // require element, create if not found, Exit on fail (unless different MAP_MODE selected)
  40. Int findAbsIndex(C KEY &key)C; // find element absolute index, don't create if not found, -1 on fail
  41. Int getAbsIndex(C KEY &key) ; // get element absolute index, create if not found, -1 on fail
  42. Int requireAbsIndex(C KEY &key) ; // require element absolute index, create if not found, Exit on fail (unless different MAP_MODE selected)
  43. Bool containsKey (C KEY &key )C; // check if map contains an element with specified key
  44. Bool containsData (C DATA *data)C; // check if map contains an element, testing is done by comparing elements memory address only
  45. C KEY* dataToKey (C DATA *data)C; // get element key, this will return pointer to element's key if that element is stored in this Map, null on fail
  46. C KEY* dataInMapToKey(C DATA *data)C; // get element key, this will return pointer to element's key, that element must be stored in this Map or be null, this method is faster than 'key' because it does not check if element is stored in this Map
  47. C KEY& dataInMapToKey(C DATA &data)C; // get element key, this will return element's key, that element must be stored in this Map , this method is faster than 'key' because it does not check if element is stored in this Map
  48. Int dataToIndex (C DATA *data)C; // get element index in map, -1 on fail (if not stored in this Map)
  49. // operations
  50. C KEY & key (Int i)C; // access i-th element key from container
  51. DATA& operator[](Int i) ; // access i-th element data from container
  52. C DATA& operator[](Int i)C; // access i-th element data from container
  53. C KEY & absKey (Int abs_i)C; // access i-th absolute element key from container, 'abs_i'=absolute index of the element
  54. DATA& absData(Int abs_i) ; // access i-th absolute element data from container, 'abs_i'=absolute index of the element
  55. C DATA& absData(Int abs_i)C; // access i-th absolute element data from container, 'abs_i'=absolute index of the element
  56. MAP_MODE mode(MAP_MODE mode); // set map mode, returns previous mode
  57. void remove ( Int i ); // remove i-th element from container
  58. void removeKey (C KEY &key ); // remove element from container
  59. void removeData(C DATA *data); // remove element from container
  60. Bool replaceKey(C KEY &src, C KEY &dest); // replace existing element 'src' key with 'dest', false on fail
  61. T1(EXTENDED) Map& replaceClass(); // replace the type of class stored in the container, all elements are automatically removed before changing the type of the class, the new type must be extended from the base 'DATA' (if you're receiving a compilation error pointing to this method this means that the new class isn't extended from the base class)
  62. Map& operator=(C Map &src); // create from 'src'
  63. explicit Map(Int compare(C KEY &a, C KEY &b)=Compare, Bool create(DATA &data, C KEY &key, Ptr user)=null, Ptr user=null, Int block_elms=64); // 'compare'=function which compares two keys, 'create'=function that creates 'data' on the base of the constant 'key'
  64. };
  65. /******************************************************************************/
  66. T2(KEY, DATA) struct ThreadSafeMap : _MapTS // Thread Safe Map
  67. {
  68. // manage
  69. ThreadSafeMap& clear(); // remove all elements
  70. ThreadSafeMap& del (); // remove all elements and free helper memory
  71. // get
  72. Int elms ()C; // get number of elements in container
  73. Int dataSize()C; // get size of DATA element
  74. DATA* find (C KEY &key); // find element, don't create if not found, null on fail
  75. DATA* get (C KEY &key); // get element, create if not found, null on fail
  76. DATA* operator()(C KEY &key); // require element, create if not found, Exit on fail (unless different MAP_MODE selected)
  77. Int findAbsIndex(C KEY &key)C; // find element absolute index, don't create if not found, -1 on fail
  78. Int getAbsIndex(C KEY &key) ; // get element absolute index, create if not found, -1 on fail
  79. Int requireAbsIndex(C KEY &key) ; // require element absolute index, create if not found, Exit on fail (unless different MAP_MODE selected)
  80. Bool containsKey (C KEY &key )C; // check if map contains an element with specified key
  81. Bool containsData (C DATA *data)C; // check if map contains an element, testing is done by comparing elements memory address only
  82. C KEY* dataToKey (C DATA *data)C; // get element key, this will return pointer to element's key if that element is stored in this Map, null on fail
  83. C KEY* dataInMapToKey(C DATA *data)C; // get element key, this will return pointer to element's key, that element must be stored in this Map or be null, this method is faster than 'key' because it does not check if element is stored in this Map
  84. C KEY& dataInMapToKey(C DATA &data)C; // get element key, this will return element's key, that element must be stored in this Map , this method is faster than 'key' because it does not check if element is stored in this Map
  85. Int dataToIndex (C DATA *data)C; // get element index in map, -1 on fail (if not stored in this Map)
  86. // operations
  87. void lock()C; // lock elements container, unlock must be called after locking container
  88. void unlock()C; // unlock elements container, this must be called after locking the container
  89. C KEY & lockedKey (Int i)C; // access i-th element key from container, this can be used after locking and before unlocking the container
  90. DATA& lockedData(Int i) ; // access i-th element data from container, this can be used after locking and before unlocking the container
  91. C DATA& lockedData(Int i)C; // access i-th element data from container, this can be used after locking and before unlocking the container
  92. C KEY & lockedAbsKey (Int abs_i)C; // access i-th absolute element key from container, this can be used after locking and before unlocking the container, 'abs_i'=absolute index of the element
  93. DATA& lockedAbsData(Int abs_i) ; // access i-th absolute element data from container, this can be used after locking and before unlocking the container, 'abs_i'=absolute index of the element
  94. C DATA& lockedAbsData(Int abs_i)C; // access i-th absolute element data from container, this can be used after locking and before unlocking the container, 'abs_i'=absolute index of the element
  95. MAP_MODE mode(MAP_MODE mode); // set map mode, returns previous mode
  96. void remove ( Int i ); // remove i-th element from container
  97. void removeKey (C KEY &key ); // remove element from container
  98. void removeData(C DATA *data); // remove element from container
  99. Bool replaceKey(C KEY &src, C KEY &dest); // replace existing element 'src' key with 'dest', false on fail
  100. T1(EXTENDED) ThreadSafeMap& replaceClass(); // replace the type of class stored in the container, all elements are automatically removed before changing the type of the class, the new type must be extended from the base 'DATA' (if you're receiving a compilation error pointing to this method this means that the new class isn't extended from the base class)
  101. ThreadSafeMap& operator=(C ThreadSafeMap &src); // create from 'src'
  102. explicit ThreadSafeMap(Int compare(C KEY &a, C KEY &b)=Compare, Bool create(DATA &data, C KEY &key, Ptr user)=null, Ptr user=null, Int block_elms=64); // 'compare'=function which compares two keys, 'create'=function that creates 'data' on the base of the constant 'key'
  103. };
  104. /******************************************************************************/
  105. struct MapLock // Map Lock (automatically locks and unlocks the map at object creation and destruction)
  106. {
  107. explicit MapLock(_MapTS &map) : _map(map) {_map. lock();}
  108. ~MapLock( ) {_map.unlock();}
  109. private:
  110. _MapTS &_map;
  111. NO_COPY_CONSTRUCTOR(MapLock);
  112. };
  113. /******************************************************************************/