NuriKit v0.1.0b2
Loading...
Searching...
No Matches
container_ext.h
Go to the documentation of this file.
1//
2// Project NuriKit - Copyright 2025 SNU Compbio Lab.
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#ifndef NURI_CORE_CONTAINER_CONTAINER_EXT_H_
7#define NURI_CORE_CONTAINER_CONTAINER_EXT_H_
8
10#include <cstddef>
11#include <functional>
12#include <queue>
13#include <vector>
14
15#include <absl/algorithm/container.h>
17
18namespace nuri {
19namespace internal {
20 template <class T, class C = std::less<>, class S = std::vector<T>>
21 struct ClearablePQ: public std::priority_queue<T, S, C> {
22 using Base = std::priority_queue<T, S, C>;
23
24 public:
25 using Base::Base;
26
27 T pop_get() noexcept {
28 T v = std::move(data().front());
29 this->pop();
30 return v;
31 }
32
33 void clear() noexcept { data().clear(); }
34
35 auto &data() { return this->c; }
36
37 void rebuild() noexcept { absl::c_make_heap(data(), C()); }
38 };
39} // namespace internal
40
41#if __cplusplus >= 202002L
42using std::erase;
43using std::erase_if;
44#else
45template <class T, class Alloc, class U>
46typename std::vector<T, Alloc>::size_type erase(std::vector<T, Alloc> &c,
47 const U &value) {
48 auto it = std::remove(c.begin(), c.end(), value);
49 auto r = std::distance(it, c.end());
50 c.erase(it, c.end());
51 return r;
52}
53
54template <class T, class Alloc, class Pred>
55typename std::vector<T, Alloc>::size_type erase_if(std::vector<T, Alloc> &c,
56 Pred &&pred) {
57 auto it = std::remove_if(c.begin(), c.end(), std::forward<Pred>(pred));
58 auto r = std::distance(it, c.end());
59 c.erase(it, c.end());
60 return r;
61}
62#endif
63
64template <class T, class Alloc, class Pred>
65typename std::vector<T, Alloc>::iterator erase_first(std::vector<T, Alloc> &c,
66 Pred &&pred) {
67 auto it = std::find_if(c.begin(), c.end(), std::forward<Pred>(pred));
68 if (it != c.end()) {
69 return c.erase(it);
70 }
71 return it;
72}
73
74template <class T, size_t N>
75constexpr size_t array_size(T (& /* arr */)[N]) {
76 return N;
77}
78} // namespace nuri
79
80#endif /* NURI_CORE_CONTAINER_CONTAINER_EXT_H_ */
Definition crdgen.h:16
std::vector< T, Alloc >::size_type erase_if(std::vector< T, Alloc > &c, Pred &&pred)
Definition container_ext.h:55
std::vector< T, Alloc >::iterator erase_first(std::vector< T, Alloc > &c, Pred &&pred)
Definition container_ext.h:65
std::vector< T, Alloc >::size_type erase(std::vector< T, Alloc > &c, const U &value)
Definition container_ext.h:46
constexpr size_t array_size(T(&)[N])
Definition container_ext.h:75