Skip to content

Commit

Permalink
data_structures -> structures, add fenwick and fenwick_set + ordered_…
Browse files Browse the repository at this point in the history
…set test
  • Loading branch information
adamant-pwn committed Nov 12, 2024
1 parent 4a57282 commit 82e0d01
Show file tree
Hide file tree
Showing 30 changed files with 262 additions and 89 deletions.
10 changes: 5 additions & 5 deletions .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"verify/algebra/poly/convolution107.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/algebra/poly/find_linrec.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/algebra/poly/poly_sqrt.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/data_structures/segtree/range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/data_structures/segtree/range_chmin_chmax_add_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/data_structures/treap/cartesian_tree.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/data_structures/treap/dynamic_sequence_range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/data_structures/treap/range_reverse_range_sum.test.cpp": "2024-02-11 15:34:32 +0100"
"verify/structures/segtree/range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/structures/segtree/range_chmin_chmax_add_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/structures/treap/cartesian_tree.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/structures/treap/dynamic_sequence_range_affine_range_sum.test.cpp": "2024-02-11 15:34:32 +0100",
"verify/structures/treap/range_reverse_range_sum.test.cpp": "2024-02-11 15:34:32 +0100"
}
11 changes: 0 additions & 11 deletions cp-algo/data_structures/segtree/metas/base.hpp

This file was deleted.

4 changes: 0 additions & 4 deletions cp-algo/data_structures/treap/common.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions cp-algo/graph/base.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CP_ALGO_GRAPH_BASE_HPP
#define CP_ALGO_GRAPH_BASE_HPP
#include "edge_types.hpp"
#include "../data_structures/stack_union.hpp"
#include "../structures/stack_union.hpp"
#include <ranges>
#include <vector>
namespace cp_algo::graph {
Expand Down Expand Up @@ -56,7 +56,7 @@ namespace cp_algo::graph {
private:
node_index v0;
std::vector<edge_t> edges;
data_structures::stack_union<edge_index> adj;
structures::stack_union<edge_index> adj;
};
}
#endif // CP_ALGO_GRAPH_BASE_HPP
4 changes: 2 additions & 2 deletions cp-algo/graph/mst.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CP_ALGO_GRAPH_MST_HPP
#define CP_ALGO_GRAPH_MST_HPP
#include "base.hpp"
#include "../data_structures/dsu.hpp"
#include "../structures/dsu.hpp"
#include <algorithm>
namespace cp_algo::graph {
template<weighted_edge_type edge_t>
Expand All @@ -11,7 +11,7 @@ namespace cp_algo::graph {
edges.emplace_back(g.edge(e).w, e);
});
std::ranges::sort(edges);
data_structures::dsu me(g.n());
structures::dsu me(g.n());
int64_t total = 0;
std::vector<edge_index> mst;
for(auto [w, e]: edges) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

#ifndef CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
#define CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
#ifndef CP_ALGO_STRUCTURES_BITPACK_HPP
#define CP_ALGO_STRUCTURES_BITPACK_HPP
#include <cstdint>
#include <cstddef>
#include <string>
#include <array>
#include <bit>
namespace cp_algo::data_structures {
namespace cp_algo::structures {
template<size_t n, typename Int = uint64_t>
struct bitpack {
static constexpr uint8_t bits_per_block = 8 * sizeof(Int);
Expand Down Expand Up @@ -76,4 +76,4 @@ namespace cp_algo::data_structures {
}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_BITPACK_HPP
#endif // CP_ALGO_STRUCTURES_BITPACK_HPP
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_DATA_STRUCTURES_DSU_HPP
#define CP_ALGO_DATA_STRUCTURES_DSU_HPP
#ifndef CP_ALGO_STRUCTURES_DSU_HPP
#define CP_ALGO_STRUCTURES_DSU_HPP
#include <numeric>
#include <vector>
namespace cp_algo::data_structures {
namespace cp_algo::structures {
struct disjoint_set_union {
disjoint_set_union (int n): par(n) {
std::iota(begin(par), end(par), 0);
Expand All @@ -21,4 +21,4 @@ namespace cp_algo::data_structures {
};
using dsu = disjoint_set_union;
}
#endif // CP_ALGO_DATA_STRUCTURES_DSU_HPP
#endif // CP_ALGO_STRUCTURES_DSU_HPP
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
#define CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
#ifndef CP_ALGO_STRUCTURES_EERTREE_HPP
#define CP_ALGO_STRUCTURES_EERTREE_HPP
#include <forward_list>
#include <functional>
#include <iostream>
#include <vector>
#include <string>
#include "../bump_alloc.hpp"
namespace cp_algo::data_structures {
namespace cp_algo::structures {
template<int sigma = 26, char mch = 'a'>
struct eertree {
eertree(size_t q) {
Expand Down Expand Up @@ -69,4 +69,4 @@ namespace cp_algo::data_structures {
int n = 1, sz = 2, last = 0;
};
}
#endif // CP_ALGO_DATA_STRUCTURES_EERTREE_HPP
#endif // CP_ALGO_STRUCTURES_EERTREE_HPP
59 changes: 59 additions & 0 deletions cp-algo/structures/fenwick.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef CP_ALGO_STRUCTURES_FENWICK_HPP
#define CP_ALGO_STRUCTURES_FENWICK_HPP
#include <cassert>
#include <vector>
#include <bit>
namespace cp_algo::structures {
template<typename T, typename Container = std::vector<T>>
struct fenwick {
size_t n;
Container data;

fenwick(auto &&range) {
assign(range);
}
void to_prefix_sums() {
for(size_t i = 1; i < n; i++) {
if(i + (i & -i) <= n) {
data[i + (i & -i)] += data[i];
}
}
}
void assign(auto &&range) {
n = size(range) - 1;
data = move(range);
to_prefix_sums();
}
void add(size_t x, T const& v) {
for(++x; x <= n; x += x & -x) {
data[x] += v;
}
}
// sum of [0, r)
T prefix_sum(size_t r) const {
assert(r <= n);
T res = 0;
for(; r; r -= r & -r) {
res += data[r];
}
return res;
}
// sum of [l, r)
T range_sum(size_t l, size_t r) const {
return prefix_sum(r) - prefix_sum(l);
}
// First r s.t. prefix_sum(r) >= k
// Assumes data[x] >= 0 for all x
size_t prefix_lower_bound(T k) const {
int x = 0;
for(size_t i = std::bit_floor(n); i; i /= 2) {
if(x + i <= n && data[x + i] < k) {
k -= data[x + i];
x += i;
}
}
return x;
}
};
}
#endif // CP_ALGO_STRUCTURES_FENWICK_HPP
51 changes: 51 additions & 0 deletions cp-algo/structures/fenwick_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef CP_ALGO_STRUCTURES_FENWICK_SET_HPP
#define CP_ALGO_STRUCTURES_FENWICK_SET_HPP
#include "fenwick.hpp"
#include <bitset>
namespace cp_algo::structures {
// fenwick-based set for [0, maxc)
template<size_t maxc>
struct fenwick_set: fenwick<int, std::array<int, maxc+1>> {
using Base = fenwick<int, std::array<int, maxc+1>>;
size_t sz = 0;
std::bitset<maxc> present;
fenwick_set(): Base(std::array<int, maxc+1>()) {}
fenwick_set(auto &&range): fenwick_set() {
for(auto x: range) {
Base::data[x + 1] = 1;
sz += !present[x];
present[x] = 1;
}
Base::to_prefix_sums();
}
void insert(size_t x) {
if(present[x]) return;
present[x] = 1;
sz++;
Base::add(x, 1);
}
void erase(size_t x) {
if(!present[x]) return;
present[x] = 0;
sz--;
Base::add(x, -1);
}
size_t order_of_key(size_t x) const {
return Base::prefix_sum(x);
}
size_t find_by_order(size_t order) const {
return order < sz ? Base::prefix_lower_bound(order + 1) : -1;
}
size_t lower_bound(size_t x) const {
if(present[x]) {return x;}
auto order = order_of_key(x);
return order < sz ? find_by_order(order) : -1;
}
size_t pre_upper_bound(size_t x) const {
if(present[x]) {return x;}
auto order = order_of_key(x);
return order ? find_by_order(order - 1) : -1;
}
};
}
#endif // CP_ALGO_STRUCTURES_FENWICK_SET_HPP
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_HPP
#define CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_HPP
#ifndef CP_ALGO_STRUCTURES_SEGMENT_TREE_HPP
#define CP_ALGO_STRUCTURES_SEGMENT_TREE_HPP
#include <vector>
namespace cp_algo::data_structures {
namespace cp_algo::structures {
template<typename meta>
struct segtree_t {
const int N;
Expand Down Expand Up @@ -71,4 +71,4 @@ namespace cp_algo::data_structures {
}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_HPP
#endif // CP_ALGO_STRUCTURES_SEGMENT_TREE_HPP
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#define CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#ifndef CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#define CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#include "base.hpp"
#include "../../../math/affine.hpp"
namespace cp_algo::data_structures::segtree::metas {
namespace cp_algo::structures::segtree::metas {
template<typename base>
struct affine_meta: base_meta<affine_meta<base>> {
using meta = affine_meta;
Expand Down Expand Up @@ -30,4 +30,4 @@ namespace cp_algo::data_structures::segtree::metas {
}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
#endif // CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_AFFINE_HPP
11 changes: 11 additions & 0 deletions cp-algo/structures/segtree/metas/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP
#define CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP
namespace cp_algo::structures::segtree::metas {
template<typename derived_meta>
struct base_meta {
using meta = derived_meta;
virtual void pull(meta const&, meta const&, int, int) {};
virtual void push(meta*, meta*, int, int) {};
};
}
#endif // CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_BASE_HPP
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
#define CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
#ifndef CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
#define CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
#include "base.hpp"
#include <functional>
#include <algorithm>
#include <cstdint>
namespace cp_algo::data_structures::segtree::metas {
namespace cp_algo::structures::segtree::metas {
struct chmin_chmax_sum_meta: base_meta<chmin_chmax_sum_meta> {
static constexpr int64_t inf = 1e12;

Expand Down Expand Up @@ -98,4 +98,4 @@ namespace cp_algo::data_structures::segtree::metas {
}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
#endif // CP_ALGO_STRUCTURES_SEGMENT_TREE_METAS_CHMIN_CHMAX_ADD_HPP
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
#define CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
#ifndef CP_ALGO_STRUCTURES_STACK_UNION_HPP
#define CP_ALGO_STRUCTURES_STACK_UNION_HPP
#include <cstddef>
#include <vector>
namespace cp_algo::data_structures {
namespace cp_algo::structures {
template<class datatype>
struct stack_union {
stack_union(int n = 0): head(n), next(1), data(1) {}
Expand Down Expand Up @@ -31,4 +31,4 @@ namespace cp_algo::data_structures {
std::vector<datatype> data;
};
}
#endif // CP_ALGO_DATA_STRUCTURES_STACK_UNION_HPP
#endif // CP_ALGO_STRUCTURES_STACK_UNION_HPP
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef CP_ALGO_DATA_STRUCTURES_TREAP_HPP
#define CP_ALGO_DATA_STRUCTURES_TREAP_HPP
#ifndef CP_ALGO_STRUCTURES_TREAP_HPP
#define CP_ALGO_STRUCTURES_TREAP_HPP
#include "../random/rng.hpp"
#include "treap/common.hpp"
#include <array>
namespace cp_algo::data_structures::treap {
namespace cp_algo::structures::treap {
template<typename meta>
struct node {
using treap = node*;
Expand Down Expand Up @@ -131,4 +131,4 @@ namespace cp_algo::data_structures::treap {
void push(auto&, auto&) {}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_TREAP_HPP
#endif // CP_ALGO_STRUCTURES_TREAP_HPP
4 changes: 4 additions & 0 deletions cp-algo/structures/treap/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef CP_ALGO_STRUCTURES_TREAP_COMMON_HPP
#define CP_ALGO_STRUCTURES_TREAP_COMMON_HPP
#define _safe(t, op) (t ? t->op : typename std::remove_reference_t<decltype(t->op)>())
#endif // CP_ALGO_STRUCTURES_TREAP_COMMON_HPP
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP
#define CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP
#ifndef CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP
#define CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP
#include "../common.hpp"
#include <functional>
#include <algorithm>
#include <cstdint>
#define _safe_meta(i, op) _safe(i, _meta.op)
namespace cp_algo::data_structures::treap::metas {
namespace cp_algo::structures::treap::metas {
struct base_meta {
void pull(auto const, auto const){}
void push(auto&, auto&){}
};
}
#endif // CP_ALGO_DATA_STRUCTURES_TREAP_METAS_BASE_HPP
#endif // CP_ALGO_STRUCTURES_TREAP_METAS_BASE_HPP
Loading

0 comments on commit 82e0d01

Please sign in to comment.