Sux
FenwickFixedF.hpp
Go to the documentation of this file.
1 /*
2  * Sux: Succinct data structures
3  *
4  * Copyright (C) 2019-2020 Stefano Marchini
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 3 of the License, or (at your option)
9  * any later version.
10  *
11  * This library is free software; you can redistribute it and/or modify it under
12  * the terms of the GNU General Public License as published by the Free Software
13  * Foundation; either version 3, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
17  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
18  *
19  * Under Section 7 of GPL version 3, you are granted additional permissions
20  * described in the GCC Runtime Library Exception, version 3.1, as published by
21  * the Free Software Foundation.
22  *
23  * You should have received a copy of the GNU General Public License and a copy of
24  * the GCC Runtime Library Exception along with this program; see the files
25  * COPYING3 and COPYING.RUNTIME respectively. If not, see
26  * <http://www.gnu.org/licenses/>.
27  */
28 
29 #pragma once
30 
31 #include "SearchablePrefixSums.hpp"
32 #include "Vector.hpp"
33 
34 namespace sux::util {
35 
42 template <size_t BOUND, AllocType AT = MALLOC> class FenwickFixedF : public SearchablePrefixSums, public Expandable {
43  public:
44  static constexpr size_t BOUNDSIZE = ceil_log2_plus1(BOUND);
45  static_assert(BOUNDSIZE >= 1 && BOUNDSIZE <= 64, "Leaves can't be stored in a 64-bit word");
46 
47  protected:
49  size_t Size;
50 
51  public:
53  FenwickFixedF() : Size(0) {}
54 
63  FenwickFixedF(uint64_t sequence[], size_t size) : Tree(pos(size) + 1), Size(size) {
64  for (size_t j = 1; j <= Size; j++) Tree[pos(j)] = sequence[j - 1];
65 
66  for (size_t m = 2; m <= Size; m <<= 1) {
67  for (size_t idx = m; idx <= Size; idx += m) Tree[pos(idx)] += Tree[pos(idx - m / 2)];
68  }
69  }
70 
71  virtual uint64_t prefix(size_t idx) {
72  uint64_t sum = 0;
73 
74  while (idx != 0) {
75  sum += Tree[pos(idx)];
76  idx = clear_rho(idx);
77  }
78 
79  return sum;
80  }
81 
82  virtual void add(size_t idx, int64_t inc) {
83  while (idx <= Size) {
84  Tree[pos(idx)] += inc;
85  idx += mask_rho(idx);
86  }
87  }
88 
90  virtual size_t find(uint64_t *val) {
91  size_t node = 0;
92 
93  for (size_t m = mask_lambda(Size); m != 0; m >>= 1) {
94  if (node + m > Size) continue;
95 
96  uint64_t value = Tree[pos(node + m)];
97 
98  if (*val >= value) {
99  node += m;
100  *val -= value;
101  }
102  }
103 
104  return node;
105  }
106 
108  virtual size_t compFind(uint64_t *val) {
109  size_t node = 0;
110 
111  for (size_t m = mask_lambda(Size); m != 0; m >>= 1) {
112  if (node + m > Size) continue;
113 
114  uint64_t value = (BOUND << rho(node + m)) - Tree[pos(node + m)];
115 
116  if (*val >= value) {
117  node += m;
118  *val -= value;
119  }
120  }
121 
122  return node;
123  }
124 
125  virtual void push(uint64_t val) {
126  size_t p = pos(++Size);
127  Tree.resize(p + 1);
128  Tree[p] = val;
129 
130  if ((Size & 1) == 0) {
131  for (size_t idx = Size - 1; rho(idx) < rho(Size); idx = clear_rho(idx)) Tree[p] += Tree[pos(idx)];
132  }
133  }
134 
135  virtual void pop() {
136  Size--;
137  Tree.popBack();
138  }
139 
140  virtual void grow(size_t space) { Tree.grow(space); }
141 
142  virtual void reserve(size_t space) { Tree.reserve(space); }
143 
144  using Expandable::trimToFit;
145  virtual void trim(size_t space) { Tree.trim(space); };
146 
147  virtual void resize(size_t space) { Tree.resize(space); }
148 
149  virtual void size(size_t space) { Tree.size(space); }
150 
151  virtual size_t size() const { return Size; }
152 
153  virtual size_t bitCount() const { return Tree.bitCount() - sizeof(Tree) * 8 + sizeof(*this) * 8; }
154 
155  private:
156  static inline size_t holes(size_t idx) { return idx >> 14; }
157 
158  static inline size_t pos(size_t idx) { return idx + holes(idx); }
159 
160  friend std::ostream &operator<<(std::ostream &os, const FenwickFixedF<BOUND, AT> &ft) {
161  os.write((char *)&ft.Size, sizeof(uint64_t));
162  return os << ft.Tree;
163  }
164 
165  friend std::istream &operator>>(std::istream &is, FenwickFixedF<BOUND, AT> &ft) {
166  is.read((char *)&ft.Size, sizeof(uint64_t));
167  return is >> ft.Tree;
168  }
169 };
170 
171 } // namespace sux::util
sux::util::FenwickFixedF::reserve
virtual void reserve(size_t space)
Definition: FenwickFixedF.hpp:142
sux::rho
int rho(uint64_t word)
Definition: common.hpp:168
sux::util
Definition: Expandable.hpp:37
Vector.hpp
sux::util::FenwickFixedF::size
virtual size_t size() const
Definition: FenwickFixedF.hpp:151
sux::util::Expandable::trimToFit
void trimToFit()
Definition: Expandable.hpp:93
sux::util::Expandable
Definition: Expandable.hpp:43
sux::util::FenwickFixedF::FenwickFixedF
FenwickFixedF()
Definition: FenwickFixedF.hpp:53
sux::util::FenwickFixedF::trim
virtual void trim(size_t space)
Definition: FenwickFixedF.hpp:145
sux::util::SearchablePrefixSums::find
virtual size_t find(uint64_t *val)=0
sux::util::FenwickFixedF::bitCount
virtual size_t bitCount() const
Definition: FenwickFixedF.hpp:153
sux::mask_lambda
uint64_t mask_lambda(uint64_t word)
Definition: common.hpp:216
sux::util::FenwickFixedF
Definition: FenwickFixedF.hpp:42
sux::util::FenwickFixedF::resize
virtual void resize(size_t space)
Definition: FenwickFixedF.hpp:147
sux::util::FenwickFixedF::grow
virtual void grow(size_t space)
Definition: FenwickFixedF.hpp:140
sux::util::FenwickFixedF::compFind
virtual size_t compFind(uint64_t *val)
Definition: FenwickFixedF.hpp:108
sux::util::FenwickFixedF::find
virtual size_t find(uint64_t *val)
Definition: FenwickFixedF.hpp:90
sux::util::Vector::reserve
void reserve(size_t capacity)
Definition: Vector.hpp:141
sux::util::FenwickFixedF::prefix
virtual uint64_t prefix(size_t idx)
Definition: FenwickFixedF.hpp:71
sux::util::Vector::size
void size(size_t size)
Definition: Vector.hpp:178
sux::util::FenwickFixedF::operator>>
friend std::istream & operator>>(std::istream &is, FenwickFixedF< BOUND, AT > &ft)
Definition: FenwickFixedF.hpp:165
sux::util::Vector::grow
void grow(size_t capacity)
Definition: Vector.hpp:153
sux::util::FenwickFixedF::add
virtual void add(size_t idx, int64_t inc)
Definition: FenwickFixedF.hpp:82
sux::util::FenwickFixedF::Tree
Vector< uint64_t, AT > Tree
Definition: FenwickFixedF.hpp:45
sux::util::FenwickFixedF::push
virtual void push(uint64_t val)
Definition: FenwickFixedF.hpp:125
SearchablePrefixSums.hpp
sux::ceil_log2_plus1
constexpr size_t ceil_log2_plus1(size_t n)
Definition: common.hpp:100
sux::util::Vector::bitCount
size_t bitCount() const
Definition: Vector.hpp:231
sux::util::FenwickFixedF::BOUNDSIZE
static constexpr size_t BOUNDSIZE
Definition: FenwickFixedF.hpp:44
sux::clear_rho
uint64_t clear_rho(uint64_t word)
Definition: common.hpp:194
sux::util::SearchablePrefixSums
Definition: SearchablePrefixSums.hpp:53
sux::util::FenwickFixedF::Size
size_t Size
Definition: FenwickFixedF.hpp:49
sux::util::FenwickFixedF::pop
virtual void pop()
Definition: FenwickFixedF.hpp:135
sux::util::Vector::resize
void resize(size_t size)
Definition: Vector.hpp:166
sux::util::FenwickFixedF::size
virtual void size(size_t space)
Definition: FenwickFixedF.hpp:149
sux::util::FenwickFixedF::operator<<
friend std::ostream & operator<<(std::ostream &os, const FenwickFixedF< BOUND, AT > &ft)
Definition: FenwickFixedF.hpp:160
sux::util::FenwickFixedF::FenwickFixedF
FenwickFixedF(uint64_t sequence[], size_t size)
Definition: FenwickFixedF.hpp:63
sux::mask_rho
uint64_t mask_rho(uint64_t word)
Definition: common.hpp:208
sux::util::Vector< uint64_t, AT >
sux::util::Vector::trim
void trim(size_t capacity)
Definition: Vector.hpp:127
sux::util::Vector::popBack
T popBack()
Definition: Vector.hpp:200
sux::util::SearchablePrefixSums::compFind
virtual size_t compFind(uint64_t *val)=0