sort_map.h - エンジンの C API リファレンス

sort_map.h
  1. #pragma once
  2. #include "pair.h"
  3. #include "vector.h"
  4. #include "allocator.h"
  5. #include "functional.h"
  6. namespace stingray_plugin_foundation {
  7. // A map data structure that needs to be explicitly sorted.
  8. //
  9. // This map is just a thin wrapper around a sorted Vector and has
  10. // the expected performance characteristics. Since data is stored
  11. // linearly it is very cache friendly and it should be fast for most
  12. // operations unless the map is huge, in which case an UnsortedMap
  13. // is better.
  14. //
  15. // Note that the map must be explicitly sorted before any lookups
  16. // can be performed and inserting new objects requires a resort.
  17. // If frequent insertion is needed, use an UnsortedMap or a Map instead.
  18. //
  19. // Warning: Any operation requiring data lookup, such as
  20. // [], erase, begin, end, find, etc, won't work as expected unless
  21. // the map has been sorted. Remember to call sort before doing such
  22. // operations.
  23. //
  24. // * `K` The key type of the map.
  25. // * `D` The data type of the map.
  26. // * `COMPARE` The less-than compare functor, defaults to std::less<K>.
  27. template <class K, class D, class COMPARE = less >
  28. class SortMap
  29. {
  30. public:
  31. ALLOCATOR_AWARE;
  32. typedef K key_type;
  33. typedef D data_type;
  34. typedef COMPARE key_compare;
  35. typedef Pair<key_type, data_type, IS_ALLOCATOR_AWARE(key_type), IS_ALLOCATOR_AWARE(data_type)> value_type;
  36. typedef Vector<value_type> storage_type;
  37. typedef typename storage_type::iterator iterator;
  38. typedef typename storage_type::const_iterator const_iterator;
  39. SortMap(const NoAllocator &a);
  40. explicit SortMap(Allocator &a);
  41. SortMap(const SortMap<K,D,COMPARE> &o);
  42. void operator=(const SortMap<K,D,COMPARE> &o);
  43. void swap(SortMap<K,D,COMPARE> &o);
  44. Allocator &allocator() const;
  45. // Sets the allocator of the sort map. You can only call this before any
  46. // allocations have been made by the vector.
  47. void set_allocator(Allocator &allocator);
  48. // Serializes the map to the stream.
  49. template <class STREAM> void serialize(STREAM &stream);
  50. unsigned size() const;
  51. bool empty() const;
  52. iterator begin();
  53. const_iterator begin() const;
  54. iterator end();
  55. const_iterator end() const;
  56. template <class KEY_EQ> iterator find(const KEY_EQ &k);
  57. template <class KEY_EQ> const_iterator find(const KEY_EQ &k) const;
  58. template <class KEY_EQ> iterator lower_bound(const KEY_EQ &k);
  59. template <class KEY_EQ> const iterator lower_bound(const KEY_EQ &k) const;
  60. template <class KEY_EQ> iterator upper_bound(const KEY_EQ &k);
  61. template <class KEY_EQ> const iterator upper_bound(const KEY_EQ &k) const;
  62. template <class KEY_EQ> unsigned lower_bound_index(const KEY_EQ &k) const;
  63. template <class KEY_EQ> unsigned upper_bound_index(const KEY_EQ &k) const;
  64. // True if the map has the key `k`.
  65. template <class KEY_EQ> bool has(const KEY_EQ &k) const;
  66. // Number of occurances of the key `k`.
  67. template <class KEY_EQ> int count(const KEY_EQ &k) const;
  68. // Access keys by index.
  69. key_type &key(int i);
  70. // Access keys by index.
  71. const key_type &key(int i) const;
  72. // Access data by index.
  73. data_type &data(int i);
  74. // Access data by index.
  75. const data_type &data(int i) const;
  76. // Access value by index.
  77. value_type &value(int i);
  78. // Access data by index.
  79. const value_type &value(int i) const;
  80. value_type &front();
  81. const value_type &front() const;
  82. value_type &back();
  83. const value_type &back() const;
  84. // Returns the value for the key `k`. The behavior is undefined if the key
  85. // does not exist.
  86. template <class KEY_EQ> const data_type &operator[](const KEY_EQ &k) const;
  87. // Unlike std::map, [] cannot be used to insert new keys in a SortMap,
  88. // only to change the values of existing keys. Returns a reference to
  89. // the value. The behavior is undefined if the map doesn't have the key `k`.
  90. template <class KEY_EQ> data_type &operator[](const KEY_EQ &k);
  91. // Returns the value for the key `k` or `def` if the key doesn't exist.
  92. template <class KEY_EQ> const data_type &get(const KEY_EQ &k, const data_type &def) const;
  93. // Clears all elements in the sort map (but does not deallocate memory).
  94. void clear();
  95. // Resets the sort map to its initial state (no memory allocated).
  96. void reset() {set_capacity(0);}
  97. // Sets the capacity of the sort map to exactly the specified number of elements.
  98. // This operation always reallocates the memory of the backing vector, no
  99. // swap trick is necessary to enforce reallocation.
  100. void set_capacity(unsigned capacity);
  101. // Inserts the data pair (`k`, `v`) at the end of the map.
  102. template <class KEY_EQ, class DATA_EQ> void insert(const KEY_EQ &k, const DATA_EQ &v);
  103. // Inserts the data pair (`k`, `v`) at the iterator position.
  104. template <class KEY_EQ> void insert(iterator pos, const KEY_EQ &k, const data_type &v);
  105. iterator erase(iterator i);
  106. template <class KEY_EQ> void erase(const KEY_EQ &k);
  107. #ifdef DEVELOPMENT
  108. // Returns true if the map is sorted.
  109. bool is_sorted();
  110. #endif
  111. // Sorts the map. You must call this before using any function that looks up
  112. // values by key.
  113. void sort();
  114. // Marks the map as sorted (without sorting it). Use this if you know that
  115. // the elements have been inserted in sorted order.
  116. void mark_sorted();
  117. // Returns true if the map contains duplicate keys.
  118. // You must sort the map before using this function.
  119. bool is_multi_map();
  120. // Sets the capacity of this map to its current size.
  121. void trim();
  122. // Reserves space
  123. void reserve(unsigned n);
  124. // Resizes
  125. void resize(unsigned n);
  126. private:
  127. template <class KEY_EQ> unsigned find_index(const KEY_EQ &k) const;
  128. void mark_unsorted();
  129. void check_sorted() const;
  130. class value_compare {
  131. public:
  132. value_compare(key_compare less) : _less(less) {}
  133. bool operator()(const value_type& left, const value_type& right) const
  134. {return (_less(left.first, right.first));}
  135. protected:
  136. key_compare _less;
  137. };
  138. key_compare _less;
  139. storage_type _data;
  140. #ifdef DEVELOPMENT
  141. bool _is_sorted;
  142. #endif
  143. };
  144. } // namespace stingray_plugin_foundation
  145. #include "sort_map.inl"