compact_flat_map-inl.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef COMPACT_FLAT_MAP_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include compact_flat_map.h"
  3. // For the sake of sane code completion.
  4. #include "compact_flat_map.h"
  5. #endif
  6. namespace NYT {
  7. ///////////////////////////////////////////////////////////////////////////////
  8. template <class TKey, class TValue, size_t N, class TKeyCompare>
  9. template <class TInputIterator>
  10. TCompactFlatMap<TKey, TValue, N, TKeyCompare>::TCompactFlatMap(TInputIterator begin, TInputIterator end)
  11. {
  12. insert(begin, end);
  13. }
  14. template <class TKey, class TValue, size_t N, class TKeyCompare>
  15. TCompactFlatMap<TKey, TValue, N, TKeyCompare>::TCompactFlatMap(std::initializer_list<value_type> values)
  16. : TCompactFlatMap<TKey, TValue, N, TKeyCompare>(values.begin(), values.end())
  17. { }
  18. template <class TKey, class TValue, size_t N, class TKeyCompare>
  19. bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator==(const TCompactFlatMap& rhs) const
  20. {
  21. return Storage_ == rhs.Storage_;
  22. }
  23. template <class TKey, class TValue, size_t N, class TKeyCompare>
  24. bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator!=(const TCompactFlatMap& rhs) const
  25. {
  26. return !(*this == rhs);
  27. }
  28. template <class TKey, class TValue, size_t N, class TKeyCompare>
  29. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::begin()
  30. {
  31. return Storage_.begin();
  32. }
  33. template <class TKey, class TValue, size_t N, class TKeyCompare>
  34. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::begin() const
  35. {
  36. return Storage_.begin();
  37. }
  38. template <class TKey, class TValue, size_t N, class TKeyCompare>
  39. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::cbegin() const
  40. {
  41. return Storage_.begin();
  42. }
  43. template <class TKey, class TValue, size_t N, class TKeyCompare>
  44. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::end()
  45. {
  46. return Storage_.end();
  47. }
  48. template <class TKey, class TValue, size_t N, class TKeyCompare>
  49. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::end() const
  50. {
  51. return Storage_.end();
  52. }
  53. template <class TKey, class TValue, size_t N, class TKeyCompare>
  54. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::cend() const
  55. {
  56. return Storage_.end();
  57. }
  58. template <class TKey, class TValue, size_t N, class TKeyCompare>
  59. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::reserve(size_type n)
  60. {
  61. Storage_.reserve(n);
  62. }
  63. template <class TKey, class TValue, size_t N, class TKeyCompare>
  64. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::size_type TCompactFlatMap<TKey, TValue, N, TKeyCompare>::size() const
  65. {
  66. return Storage_.size();
  67. }
  68. template <class TKey, class TValue, size_t N, class TKeyCompare>
  69. int TCompactFlatMap<TKey, TValue, N, TKeyCompare>::ssize() const
  70. {
  71. return static_cast<int>(Storage_.size());
  72. }
  73. template <class TKey, class TValue, size_t N, class TKeyCompare>
  74. bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::empty() const
  75. {
  76. return Storage_.empty();
  77. }
  78. template <class TKey, class TValue, size_t N, class TKeyCompare>
  79. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::clear()
  80. {
  81. Storage_.clear();
  82. }
  83. template <class TKey, class TValue, size_t N, class TKeyCompare>
  84. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::shrink_to_small()
  85. {
  86. Storage_.shrink_to_small();
  87. }
  88. template <class TKey, class TValue, size_t N, class TKeyCompare>
  89. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  90. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::find(const TOtherKey& k)
  91. {
  92. auto [rangeBegin, rangeEnd] = equal_range(k);
  93. return rangeBegin == rangeEnd ? end() : rangeBegin;
  94. }
  95. template <class TKey, class TValue, size_t N, class TKeyCompare>
  96. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  97. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::find(const TOtherKey& k) const
  98. {
  99. auto [rangeBegin, rangeEnd] = equal_range(k);
  100. return rangeBegin == rangeEnd ? end() : rangeBegin;
  101. }
  102. template <class TKey, class TValue, size_t N, class TKeyCompare>
  103. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  104. bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::contains(const TOtherKey& k) const
  105. {
  106. return find(k) != end();
  107. }
  108. template <class TKey, class TValue, size_t N, class TKeyCompare>
  109. auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(const value_type& value) -> std::pair<iterator, bool>
  110. {
  111. return do_insert(value);
  112. }
  113. template <class TKey, class TValue, size_t N, class TKeyCompare>
  114. auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(value_type&& value) -> std::pair<iterator, bool>
  115. {
  116. return do_insert(std::move(value));
  117. }
  118. template <class TKey, class TValue, size_t N, class TKeyCompare>
  119. template <class TArg>
  120. auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::do_insert(TArg&& value) -> std::pair<iterator, bool>
  121. {
  122. auto [rangeBegin, rangeEnd] = equal_range(value.first);
  123. if (rangeBegin != rangeEnd) {
  124. return {rangeBegin, false};
  125. } else {
  126. auto it = Storage_.insert(rangeBegin, std::forward<TArg>(value));
  127. return {it, true};
  128. }
  129. }
  130. template <class TKey, class TValue, size_t N, class TKeyCompare>
  131. template <class TInputIterator>
  132. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(TInputIterator begin, TInputIterator end)
  133. {
  134. for (auto it = begin; it != end; ++it) {
  135. insert(*it);
  136. }
  137. }
  138. template <class TKey, class TValue, size_t N, class TKeyCompare>
  139. template <class... TArgs>
  140. auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::emplace(TArgs&&... args) -> std::pair<iterator, bool>
  141. {
  142. return insert(value_type(std::forward<TArgs>(args)...));
  143. }
  144. template <class TKey, class TValue, size_t N, class TKeyCompare>
  145. TValue& TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator[](const TKey& k)
  146. {
  147. auto [it, inserted] = insert({k, TValue()});
  148. return it->second;
  149. }
  150. template <class TKey, class TValue, size_t N, class TKeyCompare>
  151. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(const TKey& k)
  152. {
  153. auto [rangeBegin, rangeEnd] = equal_range(k);
  154. erase(rangeBegin, rangeEnd);
  155. }
  156. template <class TKey, class TValue, size_t N, class TKeyCompare>
  157. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(iterator pos)
  158. {
  159. Storage_.erase(pos);
  160. // Try to keep the storage inline. This is why erase doesn't return an iterator.
  161. Storage_.shrink_to_small();
  162. }
  163. template <class TKey, class TValue, size_t N, class TKeyCompare>
  164. void TCompactFlatMap<TKey, TValue, N, TKeyCompare>::erase(iterator b, iterator e)
  165. {
  166. Storage_.erase(b, e);
  167. // Try to keep the storage inline. This is why erase doesn't return an iterator.
  168. Storage_.shrink_to_small();
  169. }
  170. template <class TKey, class TValue, size_t N, class TKeyCompare>
  171. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  172. std::pair<typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator, typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator>
  173. TCompactFlatMap<TKey, TValue, N, TKeyCompare>::equal_range(const TOtherKey& k)
  174. {
  175. auto result = std::ranges::equal_range(Storage_, k, {}, &value_type::first);
  176. YT_ASSERT(result.size() <= 1);
  177. return result;
  178. }
  179. template <class TKey, class TValue, size_t N, class TKeyCompare>
  180. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  181. std::pair<typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator, typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator>
  182. TCompactFlatMap<TKey, TValue, N, TKeyCompare>::equal_range(const TOtherKey& k) const
  183. {
  184. auto result = std::ranges::equal_range(Storage_, k, {}, &value_type::first);
  185. YT_ASSERT(result.size() <= 1);
  186. return result;
  187. }
  188. template <class TKey, class TValue, size_t N, class TKeyCompare>
  189. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  190. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::lower_bound(const TOtherKey& k) const
  191. {
  192. return std::ranges::lower_bound(Storage_, k, {}, &value_type::first);
  193. }
  194. template <class TKey, class TValue, size_t N, class TKeyCompare>
  195. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  196. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::lower_bound(const TOtherKey& k)
  197. {
  198. return std::ranges::lower_bound(Storage_, k, {}, &value_type::first);
  199. }
  200. template <class TKey, class TValue, size_t N, class TKeyCompare>
  201. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  202. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::const_iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::upper_bound(const TOtherKey& k) const
  203. {
  204. return std::ranges::upper_bound(Storage_, k, {}, &value_type::first);
  205. }
  206. template <class TKey, class TValue, size_t N, class TKeyCompare>
  207. template <NDetail::CComparisonAllowed<TKey, TKeyCompare> TOtherKey>
  208. typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::upper_bound(const TOtherKey& k)
  209. {
  210. return std::ranges::upper_bound(Storage_, k, {}, &value_type::first);
  211. }
  212. ////////////////////////////////////////////////////////////////////////////////
  213. } // namespace NYT