compact_flat_map-inl.h 6.9 KB

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