container.h 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: container.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file provides Container-based versions of algorithmic functions
  20. // within the C++ standard library. The following standard library sets of
  21. // functions are covered within this file:
  22. //
  23. // * Algorithmic <iterator> functions
  24. // * Algorithmic <numeric> functions
  25. // * <algorithm> functions
  26. //
  27. // The standard library functions operate on iterator ranges; the functions
  28. // within this API operate on containers, though many return iterator ranges.
  29. //
  30. // All functions within this API are named with a `c_` prefix. Calls such as
  31. // `absl::c_xx(container, ...) are equivalent to std:: functions such as
  32. // `std::xx(std::begin(cont), std::end(cont), ...)`. Functions that act on
  33. // iterators but not conceptually on iterator ranges (e.g. `std::iter_swap`)
  34. // have no equivalent here.
  35. //
  36. // For template parameter and variable naming, `C` indicates the container type
  37. // to which the function is applied, `Pred` indicates the predicate object type
  38. // to be used by the function and `T` indicates the applicable element type.
  39. #ifndef ABSL_ALGORITHM_CONTAINER_H_
  40. #define ABSL_ALGORITHM_CONTAINER_H_
  41. #include <algorithm>
  42. #include <cassert>
  43. #include <iterator>
  44. #include <numeric>
  45. #include <type_traits>
  46. #include <unordered_map>
  47. #include <unordered_set>
  48. #include <utility>
  49. #include <vector>
  50. #include "absl/algorithm/algorithm.h"
  51. #include "absl/base/macros.h"
  52. #include "absl/base/nullability.h"
  53. #include "absl/meta/type_traits.h"
  54. namespace absl {
  55. ABSL_NAMESPACE_BEGIN
  56. namespace container_algorithm_internal {
  57. // NOTE: it is important to defer to ADL lookup for building with C++ modules,
  58. // especially for headers like <valarray> which are not visible from this file
  59. // but specialize std::begin and std::end.
  60. using std::begin;
  61. using std::end;
  62. // The type of the iterator given by begin(c) (possibly std::begin(c)).
  63. // ContainerIter<const vector<T>> gives vector<T>::const_iterator,
  64. // while ContainerIter<vector<T>> gives vector<T>::iterator.
  65. template <typename C>
  66. using ContainerIter = decltype(begin(std::declval<C&>()));
  67. // An MSVC bug involving template parameter substitution requires us to use
  68. // decltype() here instead of just std::pair.
  69. template <typename C1, typename C2>
  70. using ContainerIterPairType =
  71. decltype(std::make_pair(ContainerIter<C1>(), ContainerIter<C2>()));
  72. template <typename C>
  73. using ContainerDifferenceType = decltype(std::distance(
  74. std::declval<ContainerIter<C>>(), std::declval<ContainerIter<C>>()));
  75. template <typename C>
  76. using ContainerPointerType =
  77. typename std::iterator_traits<ContainerIter<C>>::pointer;
  78. // container_algorithm_internal::c_begin and
  79. // container_algorithm_internal::c_end are abbreviations for proper ADL
  80. // lookup of std::begin and std::end, i.e.
  81. // using std::begin;
  82. // using std::end;
  83. // std::foo(begin(c), end(c));
  84. // becomes
  85. // std::foo(container_algorithm_internal::begin(c),
  86. // container_algorithm_internal::end(c));
  87. // These are meant for internal use only.
  88. template <typename C>
  89. ContainerIter<C> c_begin(C& c) {
  90. return begin(c);
  91. }
  92. template <typename C>
  93. ContainerIter<C> c_end(C& c) {
  94. return end(c);
  95. }
  96. template <typename T>
  97. struct IsUnorderedContainer : std::false_type {};
  98. template <class Key, class T, class Hash, class KeyEqual, class Allocator>
  99. struct IsUnorderedContainer<
  100. std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {};
  101. template <class Key, class Hash, class KeyEqual, class Allocator>
  102. struct IsUnorderedContainer<std::unordered_set<Key, Hash, KeyEqual, Allocator>>
  103. : std::true_type {};
  104. } // namespace container_algorithm_internal
  105. // PUBLIC API
  106. //------------------------------------------------------------------------------
  107. // Abseil algorithm.h functions
  108. //------------------------------------------------------------------------------
  109. // c_linear_search()
  110. //
  111. // Container-based version of absl::linear_search() for performing a linear
  112. // search within a container.
  113. template <typename C, typename EqualityComparable>
  114. bool c_linear_search(const C& c, EqualityComparable&& value) {
  115. return linear_search(container_algorithm_internal::c_begin(c),
  116. container_algorithm_internal::c_end(c),
  117. std::forward<EqualityComparable>(value));
  118. }
  119. //------------------------------------------------------------------------------
  120. // <iterator> algorithms
  121. //------------------------------------------------------------------------------
  122. // c_distance()
  123. //
  124. // Container-based version of the <iterator> `std::distance()` function to
  125. // return the number of elements within a container.
  126. template <typename C>
  127. container_algorithm_internal::ContainerDifferenceType<const C> c_distance(
  128. const C& c) {
  129. return std::distance(container_algorithm_internal::c_begin(c),
  130. container_algorithm_internal::c_end(c));
  131. }
  132. //------------------------------------------------------------------------------
  133. // <algorithm> Non-modifying sequence operations
  134. //------------------------------------------------------------------------------
  135. // c_all_of()
  136. //
  137. // Container-based version of the <algorithm> `std::all_of()` function to
  138. // test if all elements within a container satisfy a condition.
  139. template <typename C, typename Pred>
  140. bool c_all_of(const C& c, Pred&& pred) {
  141. return std::all_of(container_algorithm_internal::c_begin(c),
  142. container_algorithm_internal::c_end(c),
  143. std::forward<Pred>(pred));
  144. }
  145. // c_any_of()
  146. //
  147. // Container-based version of the <algorithm> `std::any_of()` function to
  148. // test if any element in a container fulfills a condition.
  149. template <typename C, typename Pred>
  150. bool c_any_of(const C& c, Pred&& pred) {
  151. return std::any_of(container_algorithm_internal::c_begin(c),
  152. container_algorithm_internal::c_end(c),
  153. std::forward<Pred>(pred));
  154. }
  155. // c_none_of()
  156. //
  157. // Container-based version of the <algorithm> `std::none_of()` function to
  158. // test if no elements in a container fulfill a condition.
  159. template <typename C, typename Pred>
  160. bool c_none_of(const C& c, Pred&& pred) {
  161. return std::none_of(container_algorithm_internal::c_begin(c),
  162. container_algorithm_internal::c_end(c),
  163. std::forward<Pred>(pred));
  164. }
  165. // c_for_each()
  166. //
  167. // Container-based version of the <algorithm> `std::for_each()` function to
  168. // apply a function to a container's elements.
  169. template <typename C, typename Function>
  170. decay_t<Function> c_for_each(C&& c, Function&& f) {
  171. return std::for_each(container_algorithm_internal::c_begin(c),
  172. container_algorithm_internal::c_end(c),
  173. std::forward<Function>(f));
  174. }
  175. // c_find()
  176. //
  177. // Container-based version of the <algorithm> `std::find()` function to find
  178. // the first element containing the passed value within a container value.
  179. template <typename C, typename T>
  180. container_algorithm_internal::ContainerIter<C> c_find(C& c, T&& value) {
  181. return std::find(container_algorithm_internal::c_begin(c),
  182. container_algorithm_internal::c_end(c),
  183. std::forward<T>(value));
  184. }
  185. // c_find_if()
  186. //
  187. // Container-based version of the <algorithm> `std::find_if()` function to find
  188. // the first element in a container matching the given condition.
  189. template <typename C, typename Pred>
  190. container_algorithm_internal::ContainerIter<C> c_find_if(C& c, Pred&& pred) {
  191. return std::find_if(container_algorithm_internal::c_begin(c),
  192. container_algorithm_internal::c_end(c),
  193. std::forward<Pred>(pred));
  194. }
  195. // c_find_if_not()
  196. //
  197. // Container-based version of the <algorithm> `std::find_if_not()` function to
  198. // find the first element in a container not matching the given condition.
  199. template <typename C, typename Pred>
  200. container_algorithm_internal::ContainerIter<C> c_find_if_not(C& c,
  201. Pred&& pred) {
  202. return std::find_if_not(container_algorithm_internal::c_begin(c),
  203. container_algorithm_internal::c_end(c),
  204. std::forward<Pred>(pred));
  205. }
  206. // c_find_end()
  207. //
  208. // Container-based version of the <algorithm> `std::find_end()` function to
  209. // find the last subsequence within a container.
  210. template <typename Sequence1, typename Sequence2>
  211. container_algorithm_internal::ContainerIter<Sequence1> c_find_end(
  212. Sequence1& sequence, Sequence2& subsequence) {
  213. return std::find_end(container_algorithm_internal::c_begin(sequence),
  214. container_algorithm_internal::c_end(sequence),
  215. container_algorithm_internal::c_begin(subsequence),
  216. container_algorithm_internal::c_end(subsequence));
  217. }
  218. // Overload of c_find_end() for using a predicate evaluation other than `==` as
  219. // the function's test condition.
  220. template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
  221. container_algorithm_internal::ContainerIter<Sequence1> c_find_end(
  222. Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) {
  223. return std::find_end(container_algorithm_internal::c_begin(sequence),
  224. container_algorithm_internal::c_end(sequence),
  225. container_algorithm_internal::c_begin(subsequence),
  226. container_algorithm_internal::c_end(subsequence),
  227. std::forward<BinaryPredicate>(pred));
  228. }
  229. // c_find_first_of()
  230. //
  231. // Container-based version of the <algorithm> `std::find_first_of()` function to
  232. // find the first element within the container that is also within the options
  233. // container.
  234. template <typename C1, typename C2>
  235. container_algorithm_internal::ContainerIter<C1> c_find_first_of(C1& container,
  236. C2& options) {
  237. return std::find_first_of(container_algorithm_internal::c_begin(container),
  238. container_algorithm_internal::c_end(container),
  239. container_algorithm_internal::c_begin(options),
  240. container_algorithm_internal::c_end(options));
  241. }
  242. // Overload of c_find_first_of() for using a predicate evaluation other than
  243. // `==` as the function's test condition.
  244. template <typename C1, typename C2, typename BinaryPredicate>
  245. container_algorithm_internal::ContainerIter<C1> c_find_first_of(
  246. C1& container, C2& options, BinaryPredicate&& pred) {
  247. return std::find_first_of(container_algorithm_internal::c_begin(container),
  248. container_algorithm_internal::c_end(container),
  249. container_algorithm_internal::c_begin(options),
  250. container_algorithm_internal::c_end(options),
  251. std::forward<BinaryPredicate>(pred));
  252. }
  253. // c_adjacent_find()
  254. //
  255. // Container-based version of the <algorithm> `std::adjacent_find()` function to
  256. // find equal adjacent elements within a container.
  257. template <typename Sequence>
  258. container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find(
  259. Sequence& sequence) {
  260. return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
  261. container_algorithm_internal::c_end(sequence));
  262. }
  263. // Overload of c_adjacent_find() for using a predicate evaluation other than
  264. // `==` as the function's test condition.
  265. template <typename Sequence, typename BinaryPredicate>
  266. container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find(
  267. Sequence& sequence, BinaryPredicate&& pred) {
  268. return std::adjacent_find(container_algorithm_internal::c_begin(sequence),
  269. container_algorithm_internal::c_end(sequence),
  270. std::forward<BinaryPredicate>(pred));
  271. }
  272. // c_count()
  273. //
  274. // Container-based version of the <algorithm> `std::count()` function to count
  275. // values that match within a container.
  276. template <typename C, typename T>
  277. container_algorithm_internal::ContainerDifferenceType<const C> c_count(
  278. const C& c, T&& value) {
  279. return std::count(container_algorithm_internal::c_begin(c),
  280. container_algorithm_internal::c_end(c),
  281. std::forward<T>(value));
  282. }
  283. // c_count_if()
  284. //
  285. // Container-based version of the <algorithm> `std::count_if()` function to
  286. // count values matching a condition within a container.
  287. template <typename C, typename Pred>
  288. container_algorithm_internal::ContainerDifferenceType<const C> c_count_if(
  289. const C& c, Pred&& pred) {
  290. return std::count_if(container_algorithm_internal::c_begin(c),
  291. container_algorithm_internal::c_end(c),
  292. std::forward<Pred>(pred));
  293. }
  294. // c_mismatch()
  295. //
  296. // Container-based version of the <algorithm> `std::mismatch()` function to
  297. // return the first element where two ordered containers differ. Applies `==` to
  298. // the first N elements of `c1` and `c2`, where N = min(size(c1), size(c2)).
  299. template <typename C1, typename C2>
  300. container_algorithm_internal::ContainerIterPairType<C1, C2> c_mismatch(C1& c1,
  301. C2& c2) {
  302. return std::mismatch(container_algorithm_internal::c_begin(c1),
  303. container_algorithm_internal::c_end(c1),
  304. container_algorithm_internal::c_begin(c2),
  305. container_algorithm_internal::c_end(c2));
  306. }
  307. // Overload of c_mismatch() for using a predicate evaluation other than `==` as
  308. // the function's test condition. Applies `pred`to the first N elements of `c1`
  309. // and `c2`, where N = min(size(c1), size(c2)).
  310. template <typename C1, typename C2, typename BinaryPredicate>
  311. container_algorithm_internal::ContainerIterPairType<C1, C2> c_mismatch(
  312. C1& c1, C2& c2, BinaryPredicate pred) {
  313. return std::mismatch(container_algorithm_internal::c_begin(c1),
  314. container_algorithm_internal::c_end(c1),
  315. container_algorithm_internal::c_begin(c2),
  316. container_algorithm_internal::c_end(c2), pred);
  317. }
  318. // c_equal()
  319. //
  320. // Container-based version of the <algorithm> `std::equal()` function to
  321. // test whether two containers are equal.
  322. template <typename C1, typename C2>
  323. bool c_equal(const C1& c1, const C2& c2) {
  324. return std::equal(container_algorithm_internal::c_begin(c1),
  325. container_algorithm_internal::c_end(c1),
  326. container_algorithm_internal::c_begin(c2),
  327. container_algorithm_internal::c_end(c2));
  328. }
  329. // Overload of c_equal() for using a predicate evaluation other than `==` as
  330. // the function's test condition.
  331. template <typename C1, typename C2, typename BinaryPredicate>
  332. bool c_equal(const C1& c1, const C2& c2, BinaryPredicate&& pred) {
  333. return std::equal(container_algorithm_internal::c_begin(c1),
  334. container_algorithm_internal::c_end(c1),
  335. container_algorithm_internal::c_begin(c2),
  336. container_algorithm_internal::c_end(c2),
  337. std::forward<BinaryPredicate>(pred));
  338. }
  339. // c_is_permutation()
  340. //
  341. // Container-based version of the <algorithm> `std::is_permutation()` function
  342. // to test whether a container is a permutation of another.
  343. template <typename C1, typename C2>
  344. bool c_is_permutation(const C1& c1, const C2& c2) {
  345. return std::is_permutation(container_algorithm_internal::c_begin(c1),
  346. container_algorithm_internal::c_end(c1),
  347. container_algorithm_internal::c_begin(c2),
  348. container_algorithm_internal::c_end(c2));
  349. }
  350. // Overload of c_is_permutation() for using a predicate evaluation other than
  351. // `==` as the function's test condition.
  352. template <typename C1, typename C2, typename BinaryPredicate>
  353. bool c_is_permutation(const C1& c1, const C2& c2, BinaryPredicate&& pred) {
  354. return std::is_permutation(container_algorithm_internal::c_begin(c1),
  355. container_algorithm_internal::c_end(c1),
  356. container_algorithm_internal::c_begin(c2),
  357. container_algorithm_internal::c_end(c2),
  358. std::forward<BinaryPredicate>(pred));
  359. }
  360. // c_search()
  361. //
  362. // Container-based version of the <algorithm> `std::search()` function to search
  363. // a container for a subsequence.
  364. template <typename Sequence1, typename Sequence2>
  365. container_algorithm_internal::ContainerIter<Sequence1> c_search(
  366. Sequence1& sequence, Sequence2& subsequence) {
  367. return std::search(container_algorithm_internal::c_begin(sequence),
  368. container_algorithm_internal::c_end(sequence),
  369. container_algorithm_internal::c_begin(subsequence),
  370. container_algorithm_internal::c_end(subsequence));
  371. }
  372. // Overload of c_search() for using a predicate evaluation other than
  373. // `==` as the function's test condition.
  374. template <typename Sequence1, typename Sequence2, typename BinaryPredicate>
  375. container_algorithm_internal::ContainerIter<Sequence1> c_search(
  376. Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) {
  377. return std::search(container_algorithm_internal::c_begin(sequence),
  378. container_algorithm_internal::c_end(sequence),
  379. container_algorithm_internal::c_begin(subsequence),
  380. container_algorithm_internal::c_end(subsequence),
  381. std::forward<BinaryPredicate>(pred));
  382. }
  383. // c_search_n()
  384. //
  385. // Container-based version of the <algorithm> `std::search_n()` function to
  386. // search a container for the first sequence of N elements.
  387. template <typename Sequence, typename Size, typename T>
  388. container_algorithm_internal::ContainerIter<Sequence> c_search_n(
  389. Sequence& sequence, Size count, T&& value) {
  390. return std::search_n(container_algorithm_internal::c_begin(sequence),
  391. container_algorithm_internal::c_end(sequence), count,
  392. std::forward<T>(value));
  393. }
  394. // Overload of c_search_n() for using a predicate evaluation other than
  395. // `==` as the function's test condition.
  396. template <typename Sequence, typename Size, typename T,
  397. typename BinaryPredicate>
  398. container_algorithm_internal::ContainerIter<Sequence> c_search_n(
  399. Sequence& sequence, Size count, T&& value, BinaryPredicate&& pred) {
  400. return std::search_n(container_algorithm_internal::c_begin(sequence),
  401. container_algorithm_internal::c_end(sequence), count,
  402. std::forward<T>(value),
  403. std::forward<BinaryPredicate>(pred));
  404. }
  405. //------------------------------------------------------------------------------
  406. // <algorithm> Modifying sequence operations
  407. //------------------------------------------------------------------------------
  408. // c_copy()
  409. //
  410. // Container-based version of the <algorithm> `std::copy()` function to copy a
  411. // container's elements into an iterator.
  412. template <typename InputSequence, typename OutputIterator>
  413. OutputIterator c_copy(const InputSequence& input, OutputIterator output) {
  414. return std::copy(container_algorithm_internal::c_begin(input),
  415. container_algorithm_internal::c_end(input), output);
  416. }
  417. // c_copy_n()
  418. //
  419. // Container-based version of the <algorithm> `std::copy_n()` function to copy a
  420. // container's first N elements into an iterator.
  421. template <typename C, typename Size, typename OutputIterator>
  422. OutputIterator c_copy_n(const C& input, Size n, OutputIterator output) {
  423. return std::copy_n(container_algorithm_internal::c_begin(input), n, output);
  424. }
  425. // c_copy_if()
  426. //
  427. // Container-based version of the <algorithm> `std::copy_if()` function to copy
  428. // a container's elements satisfying some condition into an iterator.
  429. template <typename InputSequence, typename OutputIterator, typename Pred>
  430. OutputIterator c_copy_if(const InputSequence& input, OutputIterator output,
  431. Pred&& pred) {
  432. return std::copy_if(container_algorithm_internal::c_begin(input),
  433. container_algorithm_internal::c_end(input), output,
  434. std::forward<Pred>(pred));
  435. }
  436. // c_copy_backward()
  437. //
  438. // Container-based version of the <algorithm> `std::copy_backward()` function to
  439. // copy a container's elements in reverse order into an iterator.
  440. template <typename C, typename BidirectionalIterator>
  441. BidirectionalIterator c_copy_backward(const C& src,
  442. BidirectionalIterator dest) {
  443. return std::copy_backward(container_algorithm_internal::c_begin(src),
  444. container_algorithm_internal::c_end(src), dest);
  445. }
  446. // c_move()
  447. //
  448. // Container-based version of the <algorithm> `std::move()` function to move
  449. // a container's elements into an iterator.
  450. template <typename C, typename OutputIterator>
  451. OutputIterator c_move(C&& src, OutputIterator dest) {
  452. return std::move(container_algorithm_internal::c_begin(src),
  453. container_algorithm_internal::c_end(src), dest);
  454. }
  455. // c_move_backward()
  456. //
  457. // Container-based version of the <algorithm> `std::move_backward()` function to
  458. // move a container's elements into an iterator in reverse order.
  459. template <typename C, typename BidirectionalIterator>
  460. BidirectionalIterator c_move_backward(C&& src, BidirectionalIterator dest) {
  461. return std::move_backward(container_algorithm_internal::c_begin(src),
  462. container_algorithm_internal::c_end(src), dest);
  463. }
  464. // c_swap_ranges()
  465. //
  466. // Container-based version of the <algorithm> `std::swap_ranges()` function to
  467. // swap a container's elements with another container's elements. Swaps the
  468. // first N elements of `c1` and `c2`, where N = min(size(c1), size(c2)).
  469. template <typename C1, typename C2>
  470. container_algorithm_internal::ContainerIter<C2> c_swap_ranges(C1& c1, C2& c2) {
  471. auto first1 = container_algorithm_internal::c_begin(c1);
  472. auto last1 = container_algorithm_internal::c_end(c1);
  473. auto first2 = container_algorithm_internal::c_begin(c2);
  474. auto last2 = container_algorithm_internal::c_end(c2);
  475. using std::swap;
  476. for (; first1 != last1 && first2 != last2; ++first1, (void)++first2) {
  477. swap(*first1, *first2);
  478. }
  479. return first2;
  480. }
  481. // c_transform()
  482. //
  483. // Container-based version of the <algorithm> `std::transform()` function to
  484. // transform a container's elements using the unary operation, storing the
  485. // result in an iterator pointing to the last transformed element in the output
  486. // range.
  487. template <typename InputSequence, typename OutputIterator, typename UnaryOp>
  488. OutputIterator c_transform(const InputSequence& input, OutputIterator output,
  489. UnaryOp&& unary_op) {
  490. return std::transform(container_algorithm_internal::c_begin(input),
  491. container_algorithm_internal::c_end(input), output,
  492. std::forward<UnaryOp>(unary_op));
  493. }
  494. // Overload of c_transform() for performing a transformation using a binary
  495. // predicate. Applies `binary_op` to the first N elements of `c1` and `c2`,
  496. // where N = min(size(c1), size(c2)).
  497. template <typename InputSequence1, typename InputSequence2,
  498. typename OutputIterator, typename BinaryOp>
  499. OutputIterator c_transform(const InputSequence1& input1,
  500. const InputSequence2& input2, OutputIterator output,
  501. BinaryOp&& binary_op) {
  502. auto first1 = container_algorithm_internal::c_begin(input1);
  503. auto last1 = container_algorithm_internal::c_end(input1);
  504. auto first2 = container_algorithm_internal::c_begin(input2);
  505. auto last2 = container_algorithm_internal::c_end(input2);
  506. for (; first1 != last1 && first2 != last2;
  507. ++first1, (void)++first2, ++output) {
  508. *output = binary_op(*first1, *first2);
  509. }
  510. return output;
  511. }
  512. // c_replace()
  513. //
  514. // Container-based version of the <algorithm> `std::replace()` function to
  515. // replace a container's elements of some value with a new value. The container
  516. // is modified in place.
  517. template <typename Sequence, typename T>
  518. void c_replace(Sequence& sequence, const T& old_value, const T& new_value) {
  519. std::replace(container_algorithm_internal::c_begin(sequence),
  520. container_algorithm_internal::c_end(sequence), old_value,
  521. new_value);
  522. }
  523. // c_replace_if()
  524. //
  525. // Container-based version of the <algorithm> `std::replace_if()` function to
  526. // replace a container's elements of some value with a new value based on some
  527. // condition. The container is modified in place.
  528. template <typename C, typename Pred, typename T>
  529. void c_replace_if(C& c, Pred&& pred, T&& new_value) {
  530. std::replace_if(container_algorithm_internal::c_begin(c),
  531. container_algorithm_internal::c_end(c),
  532. std::forward<Pred>(pred), std::forward<T>(new_value));
  533. }
  534. // c_replace_copy()
  535. //
  536. // Container-based version of the <algorithm> `std::replace_copy()` function to
  537. // replace a container's elements of some value with a new value and return the
  538. // results within an iterator.
  539. template <typename C, typename OutputIterator, typename T>
  540. OutputIterator c_replace_copy(const C& c, OutputIterator result, T&& old_value,
  541. T&& new_value) {
  542. return std::replace_copy(container_algorithm_internal::c_begin(c),
  543. container_algorithm_internal::c_end(c), result,
  544. std::forward<T>(old_value),
  545. std::forward<T>(new_value));
  546. }
  547. // c_replace_copy_if()
  548. //
  549. // Container-based version of the <algorithm> `std::replace_copy_if()` function
  550. // to replace a container's elements of some value with a new value based on
  551. // some condition, and return the results within an iterator.
  552. template <typename C, typename OutputIterator, typename Pred, typename T>
  553. OutputIterator c_replace_copy_if(const C& c, OutputIterator result, Pred&& pred,
  554. const T& new_value) {
  555. return std::replace_copy_if(container_algorithm_internal::c_begin(c),
  556. container_algorithm_internal::c_end(c), result,
  557. std::forward<Pred>(pred), new_value);
  558. }
  559. // c_fill()
  560. //
  561. // Container-based version of the <algorithm> `std::fill()` function to fill a
  562. // container with some value.
  563. template <typename C, typename T>
  564. void c_fill(C& c, const T& value) {
  565. std::fill(container_algorithm_internal::c_begin(c),
  566. container_algorithm_internal::c_end(c), value);
  567. }
  568. // c_fill_n()
  569. //
  570. // Container-based version of the <algorithm> `std::fill_n()` function to fill
  571. // the first N elements in a container with some value.
  572. template <typename C, typename Size, typename T>
  573. void c_fill_n(C& c, Size n, const T& value) {
  574. std::fill_n(container_algorithm_internal::c_begin(c), n, value);
  575. }
  576. // c_generate()
  577. //
  578. // Container-based version of the <algorithm> `std::generate()` function to
  579. // assign a container's elements to the values provided by the given generator.
  580. template <typename C, typename Generator>
  581. void c_generate(C& c, Generator&& gen) {
  582. std::generate(container_algorithm_internal::c_begin(c),
  583. container_algorithm_internal::c_end(c),
  584. std::forward<Generator>(gen));
  585. }
  586. // c_generate_n()
  587. //
  588. // Container-based version of the <algorithm> `std::generate_n()` function to
  589. // assign a container's first N elements to the values provided by the given
  590. // generator.
  591. template <typename C, typename Size, typename Generator>
  592. container_algorithm_internal::ContainerIter<C> c_generate_n(C& c, Size n,
  593. Generator&& gen) {
  594. return std::generate_n(container_algorithm_internal::c_begin(c), n,
  595. std::forward<Generator>(gen));
  596. }
  597. // Note: `c_xx()` <algorithm> container versions for `remove()`, `remove_if()`,
  598. // and `unique()` are omitted, because it's not clear whether or not such
  599. // functions should call erase on their supplied sequences afterwards. Either
  600. // behavior would be surprising for a different set of users.
  601. // c_remove_copy()
  602. //
  603. // Container-based version of the <algorithm> `std::remove_copy()` function to
  604. // copy a container's elements while removing any elements matching the given
  605. // `value`.
  606. template <typename C, typename OutputIterator, typename T>
  607. OutputIterator c_remove_copy(const C& c, OutputIterator result,
  608. const T& value) {
  609. return std::remove_copy(container_algorithm_internal::c_begin(c),
  610. container_algorithm_internal::c_end(c), result,
  611. value);
  612. }
  613. // c_remove_copy_if()
  614. //
  615. // Container-based version of the <algorithm> `std::remove_copy_if()` function
  616. // to copy a container's elements while removing any elements matching the given
  617. // condition.
  618. template <typename C, typename OutputIterator, typename Pred>
  619. OutputIterator c_remove_copy_if(const C& c, OutputIterator result,
  620. Pred&& pred) {
  621. return std::remove_copy_if(container_algorithm_internal::c_begin(c),
  622. container_algorithm_internal::c_end(c), result,
  623. std::forward<Pred>(pred));
  624. }
  625. // c_unique_copy()
  626. //
  627. // Container-based version of the <algorithm> `std::unique_copy()` function to
  628. // copy a container's elements while removing any elements containing duplicate
  629. // values.
  630. template <typename C, typename OutputIterator>
  631. OutputIterator c_unique_copy(const C& c, OutputIterator result) {
  632. return std::unique_copy(container_algorithm_internal::c_begin(c),
  633. container_algorithm_internal::c_end(c), result);
  634. }
  635. // Overload of c_unique_copy() for using a predicate evaluation other than
  636. // `==` for comparing uniqueness of the element values.
  637. template <typename C, typename OutputIterator, typename BinaryPredicate>
  638. OutputIterator c_unique_copy(const C& c, OutputIterator result,
  639. BinaryPredicate&& pred) {
  640. return std::unique_copy(container_algorithm_internal::c_begin(c),
  641. container_algorithm_internal::c_end(c), result,
  642. std::forward<BinaryPredicate>(pred));
  643. }
  644. // c_reverse()
  645. //
  646. // Container-based version of the <algorithm> `std::reverse()` function to
  647. // reverse a container's elements.
  648. template <typename Sequence>
  649. void c_reverse(Sequence& sequence) {
  650. std::reverse(container_algorithm_internal::c_begin(sequence),
  651. container_algorithm_internal::c_end(sequence));
  652. }
  653. // c_reverse_copy()
  654. //
  655. // Container-based version of the <algorithm> `std::reverse()` function to
  656. // reverse a container's elements and write them to an iterator range.
  657. template <typename C, typename OutputIterator>
  658. OutputIterator c_reverse_copy(const C& sequence, OutputIterator result) {
  659. return std::reverse_copy(container_algorithm_internal::c_begin(sequence),
  660. container_algorithm_internal::c_end(sequence),
  661. result);
  662. }
  663. // c_rotate()
  664. //
  665. // Container-based version of the <algorithm> `std::rotate()` function to
  666. // shift a container's elements leftward such that the `middle` element becomes
  667. // the first element in the container.
  668. template <typename C,
  669. typename Iterator = container_algorithm_internal::ContainerIter<C>>
  670. Iterator c_rotate(C& sequence, Iterator middle) {
  671. return absl::rotate(container_algorithm_internal::c_begin(sequence), middle,
  672. container_algorithm_internal::c_end(sequence));
  673. }
  674. // c_rotate_copy()
  675. //
  676. // Container-based version of the <algorithm> `std::rotate_copy()` function to
  677. // shift a container's elements leftward such that the `middle` element becomes
  678. // the first element in a new iterator range.
  679. template <typename C, typename OutputIterator>
  680. OutputIterator c_rotate_copy(
  681. const C& sequence,
  682. container_algorithm_internal::ContainerIter<const C> middle,
  683. OutputIterator result) {
  684. return std::rotate_copy(container_algorithm_internal::c_begin(sequence),
  685. middle, container_algorithm_internal::c_end(sequence),
  686. result);
  687. }
  688. // c_shuffle()
  689. //
  690. // Container-based version of the <algorithm> `std::shuffle()` function to
  691. // randomly shuffle elements within the container using a `gen()` uniform random
  692. // number generator.
  693. template <typename RandomAccessContainer, typename UniformRandomBitGenerator>
  694. void c_shuffle(RandomAccessContainer& c, UniformRandomBitGenerator&& gen) {
  695. std::shuffle(container_algorithm_internal::c_begin(c),
  696. container_algorithm_internal::c_end(c),
  697. std::forward<UniformRandomBitGenerator>(gen));
  698. }
  699. // c_sample()
  700. //
  701. // Container-based version of the <algorithm> `std::sample()` function to
  702. // randomly sample elements from the container without replacement using a
  703. // `gen()` uniform random number generator and write them to an iterator range.
  704. template <typename C, typename OutputIterator, typename Distance,
  705. typename UniformRandomBitGenerator>
  706. OutputIterator c_sample(const C& c, OutputIterator result, Distance n,
  707. UniformRandomBitGenerator&& gen) {
  708. #if defined(__cpp_lib_sample) && __cpp_lib_sample >= 201603L
  709. return std::sample(container_algorithm_internal::c_begin(c),
  710. container_algorithm_internal::c_end(c), result, n,
  711. std::forward<UniformRandomBitGenerator>(gen));
  712. #else
  713. // Fall back to a stable selection-sampling implementation.
  714. auto first = container_algorithm_internal::c_begin(c);
  715. Distance unsampled_elements = c_distance(c);
  716. n = (std::min)(n, unsampled_elements);
  717. for (; n != 0; ++first) {
  718. Distance r =
  719. std::uniform_int_distribution<Distance>(0, --unsampled_elements)(gen);
  720. if (r < n) {
  721. *result++ = *first;
  722. --n;
  723. }
  724. }
  725. return result;
  726. #endif
  727. }
  728. //------------------------------------------------------------------------------
  729. // <algorithm> Partition functions
  730. //------------------------------------------------------------------------------
  731. // c_is_partitioned()
  732. //
  733. // Container-based version of the <algorithm> `std::is_partitioned()` function
  734. // to test whether all elements in the container for which `pred` returns `true`
  735. // precede those for which `pred` is `false`.
  736. template <typename C, typename Pred>
  737. bool c_is_partitioned(const C& c, Pred&& pred) {
  738. return std::is_partitioned(container_algorithm_internal::c_begin(c),
  739. container_algorithm_internal::c_end(c),
  740. std::forward<Pred>(pred));
  741. }
  742. // c_partition()
  743. //
  744. // Container-based version of the <algorithm> `std::partition()` function
  745. // to rearrange all elements in a container in such a way that all elements for
  746. // which `pred` returns `true` precede all those for which it returns `false`,
  747. // returning an iterator to the first element of the second group.
  748. template <typename C, typename Pred>
  749. container_algorithm_internal::ContainerIter<C> c_partition(C& c, Pred&& pred) {
  750. return std::partition(container_algorithm_internal::c_begin(c),
  751. container_algorithm_internal::c_end(c),
  752. std::forward<Pred>(pred));
  753. }
  754. // c_stable_partition()
  755. //
  756. // Container-based version of the <algorithm> `std::stable_partition()` function
  757. // to rearrange all elements in a container in such a way that all elements for
  758. // which `pred` returns `true` precede all those for which it returns `false`,
  759. // preserving the relative ordering between the two groups. The function returns
  760. // an iterator to the first element of the second group.
  761. template <typename C, typename Pred>
  762. container_algorithm_internal::ContainerIter<C> c_stable_partition(C& c,
  763. Pred&& pred) {
  764. return std::stable_partition(container_algorithm_internal::c_begin(c),
  765. container_algorithm_internal::c_end(c),
  766. std::forward<Pred>(pred));
  767. }
  768. // c_partition_copy()
  769. //
  770. // Container-based version of the <algorithm> `std::partition_copy()` function
  771. // to partition a container's elements and return them into two iterators: one
  772. // for which `pred` returns `true`, and one for which `pred` returns `false.`
  773. template <typename C, typename OutputIterator1, typename OutputIterator2,
  774. typename Pred>
  775. std::pair<OutputIterator1, OutputIterator2> c_partition_copy(
  776. const C& c, OutputIterator1 out_true, OutputIterator2 out_false,
  777. Pred&& pred) {
  778. return std::partition_copy(container_algorithm_internal::c_begin(c),
  779. container_algorithm_internal::c_end(c), out_true,
  780. out_false, std::forward<Pred>(pred));
  781. }
  782. // c_partition_point()
  783. //
  784. // Container-based version of the <algorithm> `std::partition_point()` function
  785. // to return the first element of an already partitioned container for which
  786. // the given `pred` is not `true`.
  787. template <typename C, typename Pred>
  788. container_algorithm_internal::ContainerIter<C> c_partition_point(C& c,
  789. Pred&& pred) {
  790. return std::partition_point(container_algorithm_internal::c_begin(c),
  791. container_algorithm_internal::c_end(c),
  792. std::forward<Pred>(pred));
  793. }
  794. //------------------------------------------------------------------------------
  795. // <algorithm> Sorting functions
  796. //------------------------------------------------------------------------------
  797. // c_sort()
  798. //
  799. // Container-based version of the <algorithm> `std::sort()` function
  800. // to sort elements in ascending order of their values.
  801. template <typename C>
  802. void c_sort(C& c) {
  803. std::sort(container_algorithm_internal::c_begin(c),
  804. container_algorithm_internal::c_end(c));
  805. }
  806. // Overload of c_sort() for performing a `comp` comparison other than the
  807. // default `operator<`.
  808. template <typename C, typename LessThan>
  809. void c_sort(C& c, LessThan&& comp) {
  810. std::sort(container_algorithm_internal::c_begin(c),
  811. container_algorithm_internal::c_end(c),
  812. std::forward<LessThan>(comp));
  813. }
  814. // c_stable_sort()
  815. //
  816. // Container-based version of the <algorithm> `std::stable_sort()` function
  817. // to sort elements in ascending order of their values, preserving the order
  818. // of equivalents.
  819. template <typename C>
  820. void c_stable_sort(C& c) {
  821. std::stable_sort(container_algorithm_internal::c_begin(c),
  822. container_algorithm_internal::c_end(c));
  823. }
  824. // Overload of c_stable_sort() for performing a `comp` comparison other than the
  825. // default `operator<`.
  826. template <typename C, typename LessThan>
  827. void c_stable_sort(C& c, LessThan&& comp) {
  828. std::stable_sort(container_algorithm_internal::c_begin(c),
  829. container_algorithm_internal::c_end(c),
  830. std::forward<LessThan>(comp));
  831. }
  832. // c_is_sorted()
  833. //
  834. // Container-based version of the <algorithm> `std::is_sorted()` function
  835. // to evaluate whether the given container is sorted in ascending order.
  836. template <typename C>
  837. bool c_is_sorted(const C& c) {
  838. return std::is_sorted(container_algorithm_internal::c_begin(c),
  839. container_algorithm_internal::c_end(c));
  840. }
  841. // c_is_sorted() overload for performing a `comp` comparison other than the
  842. // default `operator<`.
  843. template <typename C, typename LessThan>
  844. bool c_is_sorted(const C& c, LessThan&& comp) {
  845. return std::is_sorted(container_algorithm_internal::c_begin(c),
  846. container_algorithm_internal::c_end(c),
  847. std::forward<LessThan>(comp));
  848. }
  849. // c_partial_sort()
  850. //
  851. // Container-based version of the <algorithm> `std::partial_sort()` function
  852. // to rearrange elements within a container such that elements before `middle`
  853. // are sorted in ascending order.
  854. template <typename RandomAccessContainer>
  855. void c_partial_sort(
  856. RandomAccessContainer& sequence,
  857. container_algorithm_internal::ContainerIter<RandomAccessContainer> middle) {
  858. std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
  859. container_algorithm_internal::c_end(sequence));
  860. }
  861. // Overload of c_partial_sort() for performing a `comp` comparison other than
  862. // the default `operator<`.
  863. template <typename RandomAccessContainer, typename LessThan>
  864. void c_partial_sort(
  865. RandomAccessContainer& sequence,
  866. container_algorithm_internal::ContainerIter<RandomAccessContainer> middle,
  867. LessThan&& comp) {
  868. std::partial_sort(container_algorithm_internal::c_begin(sequence), middle,
  869. container_algorithm_internal::c_end(sequence),
  870. std::forward<LessThan>(comp));
  871. }
  872. // c_partial_sort_copy()
  873. //
  874. // Container-based version of the <algorithm> `std::partial_sort_copy()`
  875. // function to sort the elements in the given range `result` within the larger
  876. // `sequence` in ascending order (and using `result` as the output parameter).
  877. // At most min(result.last - result.first, sequence.last - sequence.first)
  878. // elements from the sequence will be stored in the result.
  879. template <typename C, typename RandomAccessContainer>
  880. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  881. c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) {
  882. return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
  883. container_algorithm_internal::c_end(sequence),
  884. container_algorithm_internal::c_begin(result),
  885. container_algorithm_internal::c_end(result));
  886. }
  887. // Overload of c_partial_sort_copy() for performing a `comp` comparison other
  888. // than the default `operator<`.
  889. template <typename C, typename RandomAccessContainer, typename LessThan>
  890. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  891. c_partial_sort_copy(const C& sequence, RandomAccessContainer& result,
  892. LessThan&& comp) {
  893. return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence),
  894. container_algorithm_internal::c_end(sequence),
  895. container_algorithm_internal::c_begin(result),
  896. container_algorithm_internal::c_end(result),
  897. std::forward<LessThan>(comp));
  898. }
  899. // c_is_sorted_until()
  900. //
  901. // Container-based version of the <algorithm> `std::is_sorted_until()` function
  902. // to return the first element within a container that is not sorted in
  903. // ascending order as an iterator.
  904. template <typename C>
  905. container_algorithm_internal::ContainerIter<C> c_is_sorted_until(C& c) {
  906. return std::is_sorted_until(container_algorithm_internal::c_begin(c),
  907. container_algorithm_internal::c_end(c));
  908. }
  909. // Overload of c_is_sorted_until() for performing a `comp` comparison other than
  910. // the default `operator<`.
  911. template <typename C, typename LessThan>
  912. container_algorithm_internal::ContainerIter<C> c_is_sorted_until(
  913. C& c, LessThan&& comp) {
  914. return std::is_sorted_until(container_algorithm_internal::c_begin(c),
  915. container_algorithm_internal::c_end(c),
  916. std::forward<LessThan>(comp));
  917. }
  918. // c_nth_element()
  919. //
  920. // Container-based version of the <algorithm> `std::nth_element()` function
  921. // to rearrange the elements within a container such that the `nth` element
  922. // would be in that position in an ordered sequence; other elements may be in
  923. // any order, except that all preceding `nth` will be less than that element,
  924. // and all following `nth` will be greater than that element.
  925. template <typename RandomAccessContainer>
  926. void c_nth_element(
  927. RandomAccessContainer& sequence,
  928. container_algorithm_internal::ContainerIter<RandomAccessContainer> nth) {
  929. std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
  930. container_algorithm_internal::c_end(sequence));
  931. }
  932. // Overload of c_nth_element() for performing a `comp` comparison other than
  933. // the default `operator<`.
  934. template <typename RandomAccessContainer, typename LessThan>
  935. void c_nth_element(
  936. RandomAccessContainer& sequence,
  937. container_algorithm_internal::ContainerIter<RandomAccessContainer> nth,
  938. LessThan&& comp) {
  939. std::nth_element(container_algorithm_internal::c_begin(sequence), nth,
  940. container_algorithm_internal::c_end(sequence),
  941. std::forward<LessThan>(comp));
  942. }
  943. //------------------------------------------------------------------------------
  944. // <algorithm> Binary Search
  945. //------------------------------------------------------------------------------
  946. // c_lower_bound()
  947. //
  948. // Container-based version of the <algorithm> `std::lower_bound()` function
  949. // to return an iterator pointing to the first element in a sorted container
  950. // which does not compare less than `value`.
  951. template <typename Sequence, typename T>
  952. container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
  953. Sequence& sequence, const T& value) {
  954. return std::lower_bound(container_algorithm_internal::c_begin(sequence),
  955. container_algorithm_internal::c_end(sequence), value);
  956. }
  957. // Overload of c_lower_bound() for performing a `comp` comparison other than
  958. // the default `operator<`.
  959. template <typename Sequence, typename T, typename LessThan>
  960. container_algorithm_internal::ContainerIter<Sequence> c_lower_bound(
  961. Sequence& sequence, const T& value, LessThan&& comp) {
  962. return std::lower_bound(container_algorithm_internal::c_begin(sequence),
  963. container_algorithm_internal::c_end(sequence), value,
  964. std::forward<LessThan>(comp));
  965. }
  966. // c_upper_bound()
  967. //
  968. // Container-based version of the <algorithm> `std::upper_bound()` function
  969. // to return an iterator pointing to the first element in a sorted container
  970. // which is greater than `value`.
  971. template <typename Sequence, typename T>
  972. container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
  973. Sequence& sequence, const T& value) {
  974. return std::upper_bound(container_algorithm_internal::c_begin(sequence),
  975. container_algorithm_internal::c_end(sequence), value);
  976. }
  977. // Overload of c_upper_bound() for performing a `comp` comparison other than
  978. // the default `operator<`.
  979. template <typename Sequence, typename T, typename LessThan>
  980. container_algorithm_internal::ContainerIter<Sequence> c_upper_bound(
  981. Sequence& sequence, const T& value, LessThan&& comp) {
  982. return std::upper_bound(container_algorithm_internal::c_begin(sequence),
  983. container_algorithm_internal::c_end(sequence), value,
  984. std::forward<LessThan>(comp));
  985. }
  986. // c_equal_range()
  987. //
  988. // Container-based version of the <algorithm> `std::equal_range()` function
  989. // to return an iterator pair pointing to the first and last elements in a
  990. // sorted container which compare equal to `value`.
  991. template <typename Sequence, typename T>
  992. container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
  993. c_equal_range(Sequence& sequence, const T& value) {
  994. return std::equal_range(container_algorithm_internal::c_begin(sequence),
  995. container_algorithm_internal::c_end(sequence), value);
  996. }
  997. // Overload of c_equal_range() for performing a `comp` comparison other than
  998. // the default `operator<`.
  999. template <typename Sequence, typename T, typename LessThan>
  1000. container_algorithm_internal::ContainerIterPairType<Sequence, Sequence>
  1001. c_equal_range(Sequence& sequence, const T& value, LessThan&& comp) {
  1002. return std::equal_range(container_algorithm_internal::c_begin(sequence),
  1003. container_algorithm_internal::c_end(sequence), value,
  1004. std::forward<LessThan>(comp));
  1005. }
  1006. // c_binary_search()
  1007. //
  1008. // Container-based version of the <algorithm> `std::binary_search()` function
  1009. // to test if any element in the sorted container contains a value equivalent to
  1010. // 'value'.
  1011. template <typename Sequence, typename T>
  1012. bool c_binary_search(const Sequence& sequence, const T& value) {
  1013. return std::binary_search(container_algorithm_internal::c_begin(sequence),
  1014. container_algorithm_internal::c_end(sequence),
  1015. value);
  1016. }
  1017. // Overload of c_binary_search() for performing a `comp` comparison other than
  1018. // the default `operator<`.
  1019. template <typename Sequence, typename T, typename LessThan>
  1020. bool c_binary_search(const Sequence& sequence, const T& value,
  1021. LessThan&& comp) {
  1022. return std::binary_search(container_algorithm_internal::c_begin(sequence),
  1023. container_algorithm_internal::c_end(sequence),
  1024. value, std::forward<LessThan>(comp));
  1025. }
  1026. //------------------------------------------------------------------------------
  1027. // <algorithm> Merge functions
  1028. //------------------------------------------------------------------------------
  1029. // c_merge()
  1030. //
  1031. // Container-based version of the <algorithm> `std::merge()` function
  1032. // to merge two sorted containers into a single sorted iterator.
  1033. template <typename C1, typename C2, typename OutputIterator>
  1034. OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result) {
  1035. return std::merge(container_algorithm_internal::c_begin(c1),
  1036. container_algorithm_internal::c_end(c1),
  1037. container_algorithm_internal::c_begin(c2),
  1038. container_algorithm_internal::c_end(c2), result);
  1039. }
  1040. // Overload of c_merge() for performing a `comp` comparison other than
  1041. // the default `operator<`.
  1042. template <typename C1, typename C2, typename OutputIterator, typename LessThan>
  1043. OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result,
  1044. LessThan&& comp) {
  1045. return std::merge(container_algorithm_internal::c_begin(c1),
  1046. container_algorithm_internal::c_end(c1),
  1047. container_algorithm_internal::c_begin(c2),
  1048. container_algorithm_internal::c_end(c2), result,
  1049. std::forward<LessThan>(comp));
  1050. }
  1051. // c_inplace_merge()
  1052. //
  1053. // Container-based version of the <algorithm> `std::inplace_merge()` function
  1054. // to merge a supplied iterator `middle` into a container.
  1055. template <typename C>
  1056. void c_inplace_merge(C& c,
  1057. container_algorithm_internal::ContainerIter<C> middle) {
  1058. std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
  1059. container_algorithm_internal::c_end(c));
  1060. }
  1061. // Overload of c_inplace_merge() for performing a merge using a `comp` other
  1062. // than `operator<`.
  1063. template <typename C, typename LessThan>
  1064. void c_inplace_merge(C& c,
  1065. container_algorithm_internal::ContainerIter<C> middle,
  1066. LessThan&& comp) {
  1067. std::inplace_merge(container_algorithm_internal::c_begin(c), middle,
  1068. container_algorithm_internal::c_end(c),
  1069. std::forward<LessThan>(comp));
  1070. }
  1071. // c_includes()
  1072. //
  1073. // Container-based version of the <algorithm> `std::includes()` function
  1074. // to test whether a sorted container `c1` entirely contains another sorted
  1075. // container `c2`.
  1076. template <typename C1, typename C2>
  1077. bool c_includes(const C1& c1, const C2& c2) {
  1078. return std::includes(container_algorithm_internal::c_begin(c1),
  1079. container_algorithm_internal::c_end(c1),
  1080. container_algorithm_internal::c_begin(c2),
  1081. container_algorithm_internal::c_end(c2));
  1082. }
  1083. // Overload of c_includes() for performing a merge using a `comp` other than
  1084. // `operator<`.
  1085. template <typename C1, typename C2, typename LessThan>
  1086. bool c_includes(const C1& c1, const C2& c2, LessThan&& comp) {
  1087. return std::includes(container_algorithm_internal::c_begin(c1),
  1088. container_algorithm_internal::c_end(c1),
  1089. container_algorithm_internal::c_begin(c2),
  1090. container_algorithm_internal::c_end(c2),
  1091. std::forward<LessThan>(comp));
  1092. }
  1093. // c_set_union()
  1094. //
  1095. // Container-based version of the <algorithm> `std::set_union()` function
  1096. // to return an iterator containing the union of two containers; duplicate
  1097. // values are not copied into the output.
  1098. template <typename C1, typename C2, typename OutputIterator,
  1099. typename = typename std::enable_if<
  1100. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1101. void>::type,
  1102. typename = typename std::enable_if<
  1103. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1104. void>::type>
  1105. OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output) {
  1106. return std::set_union(container_algorithm_internal::c_begin(c1),
  1107. container_algorithm_internal::c_end(c1),
  1108. container_algorithm_internal::c_begin(c2),
  1109. container_algorithm_internal::c_end(c2), output);
  1110. }
  1111. // Overload of c_set_union() for performing a merge using a `comp` other than
  1112. // `operator<`.
  1113. template <typename C1, typename C2, typename OutputIterator, typename LessThan,
  1114. typename = typename std::enable_if<
  1115. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1116. void>::type,
  1117. typename = typename std::enable_if<
  1118. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1119. void>::type>
  1120. OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output,
  1121. LessThan&& comp) {
  1122. return std::set_union(container_algorithm_internal::c_begin(c1),
  1123. container_algorithm_internal::c_end(c1),
  1124. container_algorithm_internal::c_begin(c2),
  1125. container_algorithm_internal::c_end(c2), output,
  1126. std::forward<LessThan>(comp));
  1127. }
  1128. // c_set_intersection()
  1129. //
  1130. // Container-based version of the <algorithm> `std::set_intersection()` function
  1131. // to return an iterator containing the intersection of two sorted containers.
  1132. template <typename C1, typename C2, typename OutputIterator,
  1133. typename = typename std::enable_if<
  1134. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1135. void>::type,
  1136. typename = typename std::enable_if<
  1137. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1138. void>::type>
  1139. OutputIterator c_set_intersection(const C1& c1, const C2& c2,
  1140. OutputIterator output) {
  1141. // In debug builds, ensure that both containers are sorted with respect to the
  1142. // default comparator. std::set_intersection requires the containers be sorted
  1143. // using operator<.
  1144. assert(absl::c_is_sorted(c1));
  1145. assert(absl::c_is_sorted(c2));
  1146. return std::set_intersection(container_algorithm_internal::c_begin(c1),
  1147. container_algorithm_internal::c_end(c1),
  1148. container_algorithm_internal::c_begin(c2),
  1149. container_algorithm_internal::c_end(c2), output);
  1150. }
  1151. // Overload of c_set_intersection() for performing a merge using a `comp` other
  1152. // than `operator<`.
  1153. template <typename C1, typename C2, typename OutputIterator, typename LessThan,
  1154. typename = typename std::enable_if<
  1155. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1156. void>::type,
  1157. typename = typename std::enable_if<
  1158. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1159. void>::type>
  1160. OutputIterator c_set_intersection(const C1& c1, const C2& c2,
  1161. OutputIterator output, LessThan&& comp) {
  1162. // In debug builds, ensure that both containers are sorted with respect to the
  1163. // default comparator. std::set_intersection requires the containers be sorted
  1164. // using the same comparator.
  1165. assert(absl::c_is_sorted(c1, comp));
  1166. assert(absl::c_is_sorted(c2, comp));
  1167. return std::set_intersection(container_algorithm_internal::c_begin(c1),
  1168. container_algorithm_internal::c_end(c1),
  1169. container_algorithm_internal::c_begin(c2),
  1170. container_algorithm_internal::c_end(c2), output,
  1171. std::forward<LessThan>(comp));
  1172. }
  1173. // c_set_difference()
  1174. //
  1175. // Container-based version of the <algorithm> `std::set_difference()` function
  1176. // to return an iterator containing elements present in the first container but
  1177. // not in the second.
  1178. template <typename C1, typename C2, typename OutputIterator,
  1179. typename = typename std::enable_if<
  1180. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1181. void>::type,
  1182. typename = typename std::enable_if<
  1183. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1184. void>::type>
  1185. OutputIterator c_set_difference(const C1& c1, const C2& c2,
  1186. OutputIterator output) {
  1187. return std::set_difference(container_algorithm_internal::c_begin(c1),
  1188. container_algorithm_internal::c_end(c1),
  1189. container_algorithm_internal::c_begin(c2),
  1190. container_algorithm_internal::c_end(c2), output);
  1191. }
  1192. // Overload of c_set_difference() for performing a merge using a `comp` other
  1193. // than `operator<`.
  1194. template <typename C1, typename C2, typename OutputIterator, typename LessThan,
  1195. typename = typename std::enable_if<
  1196. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1197. void>::type,
  1198. typename = typename std::enable_if<
  1199. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1200. void>::type>
  1201. OutputIterator c_set_difference(const C1& c1, const C2& c2,
  1202. OutputIterator output, LessThan&& comp) {
  1203. return std::set_difference(container_algorithm_internal::c_begin(c1),
  1204. container_algorithm_internal::c_end(c1),
  1205. container_algorithm_internal::c_begin(c2),
  1206. container_algorithm_internal::c_end(c2), output,
  1207. std::forward<LessThan>(comp));
  1208. }
  1209. // c_set_symmetric_difference()
  1210. //
  1211. // Container-based version of the <algorithm> `std::set_symmetric_difference()`
  1212. // function to return an iterator containing elements present in either one
  1213. // container or the other, but not both.
  1214. template <typename C1, typename C2, typename OutputIterator,
  1215. typename = typename std::enable_if<
  1216. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1217. void>::type,
  1218. typename = typename std::enable_if<
  1219. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1220. void>::type>
  1221. OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
  1222. OutputIterator output) {
  1223. return std::set_symmetric_difference(
  1224. container_algorithm_internal::c_begin(c1),
  1225. container_algorithm_internal::c_end(c1),
  1226. container_algorithm_internal::c_begin(c2),
  1227. container_algorithm_internal::c_end(c2), output);
  1228. }
  1229. // Overload of c_set_symmetric_difference() for performing a merge using a
  1230. // `comp` other than `operator<`.
  1231. template <typename C1, typename C2, typename OutputIterator, typename LessThan,
  1232. typename = typename std::enable_if<
  1233. !container_algorithm_internal::IsUnorderedContainer<C1>::value,
  1234. void>::type,
  1235. typename = typename std::enable_if<
  1236. !container_algorithm_internal::IsUnorderedContainer<C2>::value,
  1237. void>::type>
  1238. OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2,
  1239. OutputIterator output,
  1240. LessThan&& comp) {
  1241. return std::set_symmetric_difference(
  1242. container_algorithm_internal::c_begin(c1),
  1243. container_algorithm_internal::c_end(c1),
  1244. container_algorithm_internal::c_begin(c2),
  1245. container_algorithm_internal::c_end(c2), output,
  1246. std::forward<LessThan>(comp));
  1247. }
  1248. //------------------------------------------------------------------------------
  1249. // <algorithm> Heap functions
  1250. //------------------------------------------------------------------------------
  1251. // c_push_heap()
  1252. //
  1253. // Container-based version of the <algorithm> `std::push_heap()` function
  1254. // to push a value onto a container heap.
  1255. template <typename RandomAccessContainer>
  1256. void c_push_heap(RandomAccessContainer& sequence) {
  1257. std::push_heap(container_algorithm_internal::c_begin(sequence),
  1258. container_algorithm_internal::c_end(sequence));
  1259. }
  1260. // Overload of c_push_heap() for performing a push operation on a heap using a
  1261. // `comp` other than `operator<`.
  1262. template <typename RandomAccessContainer, typename LessThan>
  1263. void c_push_heap(RandomAccessContainer& sequence, LessThan&& comp) {
  1264. std::push_heap(container_algorithm_internal::c_begin(sequence),
  1265. container_algorithm_internal::c_end(sequence),
  1266. std::forward<LessThan>(comp));
  1267. }
  1268. // c_pop_heap()
  1269. //
  1270. // Container-based version of the <algorithm> `std::pop_heap()` function
  1271. // to pop a value from a heap container.
  1272. template <typename RandomAccessContainer>
  1273. void c_pop_heap(RandomAccessContainer& sequence) {
  1274. std::pop_heap(container_algorithm_internal::c_begin(sequence),
  1275. container_algorithm_internal::c_end(sequence));
  1276. }
  1277. // Overload of c_pop_heap() for performing a pop operation on a heap using a
  1278. // `comp` other than `operator<`.
  1279. template <typename RandomAccessContainer, typename LessThan>
  1280. void c_pop_heap(RandomAccessContainer& sequence, LessThan&& comp) {
  1281. std::pop_heap(container_algorithm_internal::c_begin(sequence),
  1282. container_algorithm_internal::c_end(sequence),
  1283. std::forward<LessThan>(comp));
  1284. }
  1285. // c_make_heap()
  1286. //
  1287. // Container-based version of the <algorithm> `std::make_heap()` function
  1288. // to make a container a heap.
  1289. template <typename RandomAccessContainer>
  1290. void c_make_heap(RandomAccessContainer& sequence) {
  1291. std::make_heap(container_algorithm_internal::c_begin(sequence),
  1292. container_algorithm_internal::c_end(sequence));
  1293. }
  1294. // Overload of c_make_heap() for performing heap comparisons using a
  1295. // `comp` other than `operator<`
  1296. template <typename RandomAccessContainer, typename LessThan>
  1297. void c_make_heap(RandomAccessContainer& sequence, LessThan&& comp) {
  1298. std::make_heap(container_algorithm_internal::c_begin(sequence),
  1299. container_algorithm_internal::c_end(sequence),
  1300. std::forward<LessThan>(comp));
  1301. }
  1302. // c_sort_heap()
  1303. //
  1304. // Container-based version of the <algorithm> `std::sort_heap()` function
  1305. // to sort a heap into ascending order (after which it is no longer a heap).
  1306. template <typename RandomAccessContainer>
  1307. void c_sort_heap(RandomAccessContainer& sequence) {
  1308. std::sort_heap(container_algorithm_internal::c_begin(sequence),
  1309. container_algorithm_internal::c_end(sequence));
  1310. }
  1311. // Overload of c_sort_heap() for performing heap comparisons using a
  1312. // `comp` other than `operator<`
  1313. template <typename RandomAccessContainer, typename LessThan>
  1314. void c_sort_heap(RandomAccessContainer& sequence, LessThan&& comp) {
  1315. std::sort_heap(container_algorithm_internal::c_begin(sequence),
  1316. container_algorithm_internal::c_end(sequence),
  1317. std::forward<LessThan>(comp));
  1318. }
  1319. // c_is_heap()
  1320. //
  1321. // Container-based version of the <algorithm> `std::is_heap()` function
  1322. // to check whether the given container is a heap.
  1323. template <typename RandomAccessContainer>
  1324. bool c_is_heap(const RandomAccessContainer& sequence) {
  1325. return std::is_heap(container_algorithm_internal::c_begin(sequence),
  1326. container_algorithm_internal::c_end(sequence));
  1327. }
  1328. // Overload of c_is_heap() for performing heap comparisons using a
  1329. // `comp` other than `operator<`
  1330. template <typename RandomAccessContainer, typename LessThan>
  1331. bool c_is_heap(const RandomAccessContainer& sequence, LessThan&& comp) {
  1332. return std::is_heap(container_algorithm_internal::c_begin(sequence),
  1333. container_algorithm_internal::c_end(sequence),
  1334. std::forward<LessThan>(comp));
  1335. }
  1336. // c_is_heap_until()
  1337. //
  1338. // Container-based version of the <algorithm> `std::is_heap_until()` function
  1339. // to find the first element in a given container which is not in heap order.
  1340. template <typename RandomAccessContainer>
  1341. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  1342. c_is_heap_until(RandomAccessContainer& sequence) {
  1343. return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
  1344. container_algorithm_internal::c_end(sequence));
  1345. }
  1346. // Overload of c_is_heap_until() for performing heap comparisons using a
  1347. // `comp` other than `operator<`
  1348. template <typename RandomAccessContainer, typename LessThan>
  1349. container_algorithm_internal::ContainerIter<RandomAccessContainer>
  1350. c_is_heap_until(RandomAccessContainer& sequence, LessThan&& comp) {
  1351. return std::is_heap_until(container_algorithm_internal::c_begin(sequence),
  1352. container_algorithm_internal::c_end(sequence),
  1353. std::forward<LessThan>(comp));
  1354. }
  1355. //------------------------------------------------------------------------------
  1356. // <algorithm> Min/max
  1357. //------------------------------------------------------------------------------
  1358. // c_min_element()
  1359. //
  1360. // Container-based version of the <algorithm> `std::min_element()` function
  1361. // to return an iterator pointing to the element with the smallest value, using
  1362. // `operator<` to make the comparisons.
  1363. template <typename Sequence>
  1364. container_algorithm_internal::ContainerIter<Sequence> c_min_element(
  1365. Sequence& sequence) {
  1366. return std::min_element(container_algorithm_internal::c_begin(sequence),
  1367. container_algorithm_internal::c_end(sequence));
  1368. }
  1369. // Overload of c_min_element() for performing a `comp` comparison other than
  1370. // `operator<`.
  1371. template <typename Sequence, typename LessThan>
  1372. container_algorithm_internal::ContainerIter<Sequence> c_min_element(
  1373. Sequence& sequence, LessThan&& comp) {
  1374. return std::min_element(container_algorithm_internal::c_begin(sequence),
  1375. container_algorithm_internal::c_end(sequence),
  1376. std::forward<LessThan>(comp));
  1377. }
  1378. // c_max_element()
  1379. //
  1380. // Container-based version of the <algorithm> `std::max_element()` function
  1381. // to return an iterator pointing to the element with the largest value, using
  1382. // `operator<` to make the comparisons.
  1383. template <typename Sequence>
  1384. container_algorithm_internal::ContainerIter<Sequence> c_max_element(
  1385. Sequence& sequence) {
  1386. return std::max_element(container_algorithm_internal::c_begin(sequence),
  1387. container_algorithm_internal::c_end(sequence));
  1388. }
  1389. // Overload of c_max_element() for performing a `comp` comparison other than
  1390. // `operator<`.
  1391. template <typename Sequence, typename LessThan>
  1392. container_algorithm_internal::ContainerIter<Sequence> c_max_element(
  1393. Sequence& sequence, LessThan&& comp) {
  1394. return std::max_element(container_algorithm_internal::c_begin(sequence),
  1395. container_algorithm_internal::c_end(sequence),
  1396. std::forward<LessThan>(comp));
  1397. }
  1398. // c_minmax_element()
  1399. //
  1400. // Container-based version of the <algorithm> `std::minmax_element()` function
  1401. // to return a pair of iterators pointing to the elements containing the
  1402. // smallest and largest values, respectively, using `operator<` to make the
  1403. // comparisons.
  1404. template <typename C>
  1405. container_algorithm_internal::ContainerIterPairType<C, C> c_minmax_element(
  1406. C& c) {
  1407. return std::minmax_element(container_algorithm_internal::c_begin(c),
  1408. container_algorithm_internal::c_end(c));
  1409. }
  1410. // Overload of c_minmax_element() for performing `comp` comparisons other than
  1411. // `operator<`.
  1412. template <typename C, typename LessThan>
  1413. container_algorithm_internal::ContainerIterPairType<C, C> c_minmax_element(
  1414. C& c, LessThan&& comp) {
  1415. return std::minmax_element(container_algorithm_internal::c_begin(c),
  1416. container_algorithm_internal::c_end(c),
  1417. std::forward<LessThan>(comp));
  1418. }
  1419. //------------------------------------------------------------------------------
  1420. // <algorithm> Lexicographical Comparisons
  1421. //------------------------------------------------------------------------------
  1422. // c_lexicographical_compare()
  1423. //
  1424. // Container-based version of the <algorithm> `std::lexicographical_compare()`
  1425. // function to lexicographically compare (e.g. sort words alphabetically) two
  1426. // container sequences. The comparison is performed using `operator<`. Note
  1427. // that capital letters ("A-Z") have ASCII values less than lowercase letters
  1428. // ("a-z").
  1429. template <typename Sequence1, typename Sequence2>
  1430. bool c_lexicographical_compare(const Sequence1& sequence1,
  1431. const Sequence2& sequence2) {
  1432. return std::lexicographical_compare(
  1433. container_algorithm_internal::c_begin(sequence1),
  1434. container_algorithm_internal::c_end(sequence1),
  1435. container_algorithm_internal::c_begin(sequence2),
  1436. container_algorithm_internal::c_end(sequence2));
  1437. }
  1438. // Overload of c_lexicographical_compare() for performing a lexicographical
  1439. // comparison using a `comp` operator instead of `operator<`.
  1440. template <typename Sequence1, typename Sequence2, typename LessThan>
  1441. bool c_lexicographical_compare(const Sequence1& sequence1,
  1442. const Sequence2& sequence2, LessThan&& comp) {
  1443. return std::lexicographical_compare(
  1444. container_algorithm_internal::c_begin(sequence1),
  1445. container_algorithm_internal::c_end(sequence1),
  1446. container_algorithm_internal::c_begin(sequence2),
  1447. container_algorithm_internal::c_end(sequence2),
  1448. std::forward<LessThan>(comp));
  1449. }
  1450. // c_next_permutation()
  1451. //
  1452. // Container-based version of the <algorithm> `std::next_permutation()` function
  1453. // to rearrange a container's elements into the next lexicographically greater
  1454. // permutation.
  1455. template <typename C>
  1456. bool c_next_permutation(C& c) {
  1457. return std::next_permutation(container_algorithm_internal::c_begin(c),
  1458. container_algorithm_internal::c_end(c));
  1459. }
  1460. // Overload of c_next_permutation() for performing a lexicographical
  1461. // comparison using a `comp` operator instead of `operator<`.
  1462. template <typename C, typename LessThan>
  1463. bool c_next_permutation(C& c, LessThan&& comp) {
  1464. return std::next_permutation(container_algorithm_internal::c_begin(c),
  1465. container_algorithm_internal::c_end(c),
  1466. std::forward<LessThan>(comp));
  1467. }
  1468. // c_prev_permutation()
  1469. //
  1470. // Container-based version of the <algorithm> `std::prev_permutation()` function
  1471. // to rearrange a container's elements into the next lexicographically lesser
  1472. // permutation.
  1473. template <typename C>
  1474. bool c_prev_permutation(C& c) {
  1475. return std::prev_permutation(container_algorithm_internal::c_begin(c),
  1476. container_algorithm_internal::c_end(c));
  1477. }
  1478. // Overload of c_prev_permutation() for performing a lexicographical
  1479. // comparison using a `comp` operator instead of `operator<`.
  1480. template <typename C, typename LessThan>
  1481. bool c_prev_permutation(C& c, LessThan&& comp) {
  1482. return std::prev_permutation(container_algorithm_internal::c_begin(c),
  1483. container_algorithm_internal::c_end(c),
  1484. std::forward<LessThan>(comp));
  1485. }
  1486. //------------------------------------------------------------------------------
  1487. // <numeric> algorithms
  1488. //------------------------------------------------------------------------------
  1489. // c_iota()
  1490. //
  1491. // Container-based version of the <numeric> `std::iota()` function
  1492. // to compute successive values of `value`, as if incremented with `++value`
  1493. // after each element is written, and write them to the container.
  1494. template <typename Sequence, typename T>
  1495. void c_iota(Sequence& sequence, const T& value) {
  1496. std::iota(container_algorithm_internal::c_begin(sequence),
  1497. container_algorithm_internal::c_end(sequence), value);
  1498. }
  1499. // c_accumulate()
  1500. //
  1501. // Container-based version of the <numeric> `std::accumulate()` function
  1502. // to accumulate the element values of a container to `init` and return that
  1503. // accumulation by value.
  1504. //
  1505. // Note: Due to a language technicality this function has return type
  1506. // absl::decay_t<T>. As a user of this function you can casually read
  1507. // this as "returns T by value" and assume it does the right thing.
  1508. template <typename Sequence, typename T>
  1509. decay_t<T> c_accumulate(const Sequence& sequence, T&& init) {
  1510. return std::accumulate(container_algorithm_internal::c_begin(sequence),
  1511. container_algorithm_internal::c_end(sequence),
  1512. std::forward<T>(init));
  1513. }
  1514. // Overload of c_accumulate() for using a binary operations other than
  1515. // addition for computing the accumulation.
  1516. template <typename Sequence, typename T, typename BinaryOp>
  1517. decay_t<T> c_accumulate(const Sequence& sequence, T&& init,
  1518. BinaryOp&& binary_op) {
  1519. return std::accumulate(container_algorithm_internal::c_begin(sequence),
  1520. container_algorithm_internal::c_end(sequence),
  1521. std::forward<T>(init),
  1522. std::forward<BinaryOp>(binary_op));
  1523. }
  1524. // c_inner_product()
  1525. //
  1526. // Container-based version of the <numeric> `std::inner_product()` function
  1527. // to compute the cumulative inner product of container element pairs.
  1528. //
  1529. // Note: Due to a language technicality this function has return type
  1530. // absl::decay_t<T>. As a user of this function you can casually read
  1531. // this as "returns T by value" and assume it does the right thing.
  1532. template <typename Sequence1, typename Sequence2, typename T>
  1533. decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
  1534. T&& sum) {
  1535. return std::inner_product(container_algorithm_internal::c_begin(factors1),
  1536. container_algorithm_internal::c_end(factors1),
  1537. container_algorithm_internal::c_begin(factors2),
  1538. std::forward<T>(sum));
  1539. }
  1540. // Overload of c_inner_product() for using binary operations other than
  1541. // `operator+` (for computing the accumulation) and `operator*` (for computing
  1542. // the product between the two container's element pair).
  1543. template <typename Sequence1, typename Sequence2, typename T,
  1544. typename BinaryOp1, typename BinaryOp2>
  1545. decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2,
  1546. T&& sum, BinaryOp1&& op1, BinaryOp2&& op2) {
  1547. return std::inner_product(container_algorithm_internal::c_begin(factors1),
  1548. container_algorithm_internal::c_end(factors1),
  1549. container_algorithm_internal::c_begin(factors2),
  1550. std::forward<T>(sum), std::forward<BinaryOp1>(op1),
  1551. std::forward<BinaryOp2>(op2));
  1552. }
  1553. // c_adjacent_difference()
  1554. //
  1555. // Container-based version of the <numeric> `std::adjacent_difference()`
  1556. // function to compute the difference between each element and the one preceding
  1557. // it and write it to an iterator.
  1558. template <typename InputSequence, typename OutputIt>
  1559. OutputIt c_adjacent_difference(const InputSequence& input,
  1560. OutputIt output_first) {
  1561. return std::adjacent_difference(container_algorithm_internal::c_begin(input),
  1562. container_algorithm_internal::c_end(input),
  1563. output_first);
  1564. }
  1565. // Overload of c_adjacent_difference() for using a binary operation other than
  1566. // subtraction to compute the adjacent difference.
  1567. template <typename InputSequence, typename OutputIt, typename BinaryOp>
  1568. OutputIt c_adjacent_difference(const InputSequence& input,
  1569. OutputIt output_first, BinaryOp&& op) {
  1570. return std::adjacent_difference(container_algorithm_internal::c_begin(input),
  1571. container_algorithm_internal::c_end(input),
  1572. output_first, std::forward<BinaryOp>(op));
  1573. }
  1574. // c_partial_sum()
  1575. //
  1576. // Container-based version of the <numeric> `std::partial_sum()` function
  1577. // to compute the partial sum of the elements in a sequence and write them
  1578. // to an iterator. The partial sum is the sum of all element values so far in
  1579. // the sequence.
  1580. template <typename InputSequence, typename OutputIt>
  1581. OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first) {
  1582. return std::partial_sum(container_algorithm_internal::c_begin(input),
  1583. container_algorithm_internal::c_end(input),
  1584. output_first);
  1585. }
  1586. // Overload of c_partial_sum() for using a binary operation other than addition
  1587. // to compute the "partial sum".
  1588. template <typename InputSequence, typename OutputIt, typename BinaryOp>
  1589. OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first,
  1590. BinaryOp&& op) {
  1591. return std::partial_sum(container_algorithm_internal::c_begin(input),
  1592. container_algorithm_internal::c_end(input),
  1593. output_first, std::forward<BinaryOp>(op));
  1594. }
  1595. ABSL_NAMESPACE_END
  1596. } // namespace absl
  1597. #endif // ABSL_ALGORITHM_CONTAINER_H_