1#include "intvector.h"
2
3namespace marisa {
4
5IntVector::IntVector()
6    : units_(), num_bits_per_int_(0), mask_(0), size_(0) {}
7
8void IntVector::build(const Vector<UInt32> &ints) {
9  UInt32 max_int = 0;
10  for (UInt32 i = 0; i < ints.size(); ++i) {
11    if (ints[i] > max_int) {
12      max_int = ints[i];
13    }
14  }
15  build(max_int, ints.size());
16  for (UInt32 i = 0; i < ints.size(); ++i) {
17    set(i, ints[i]);
18  }
19}
20
21void IntVector::build(UInt32 max_int, std::size_t size) {
22  UInt32 num_bits_per_int = 0;
23  do {
24    ++num_bits_per_int;
25    max_int >>= 1;
26  } while (max_int != 0);
27
28  const std::size_t new_size = (std::size_t)(
29      (((UInt64)num_bits_per_int * size) + 31) / 32);
30
31  Vector<UInt32> temp_units;
32  temp_units.resize(new_size, 0);
33  units_.swap(&temp_units);
34
35  num_bits_per_int_ = num_bits_per_int;
36  mask_ = ~0U;
37  if (num_bits_per_int != 32) {
38    mask_ = (1U << num_bits_per_int) - 1;
39  }
40  size_ = (UInt32)size;
41}
42
43void IntVector::mmap(Mapper *mapper, const char *filename,
44    long offset, int whence) {
45  MARISA_THROW_IF(mapper == NULL, MARISA_PARAM_ERROR);
46  Mapper temp_mapper;
47  temp_mapper.open(filename, offset, whence);
48  map(temp_mapper);
49  temp_mapper.swap(mapper);
50}
51
52void IntVector::map(const void *ptr, std::size_t size) {
53  Mapper mapper(ptr, size);
54  map(mapper);
55}
56
57void IntVector::map(Mapper &mapper) {
58  IntVector temp;
59  temp.units_.map(mapper);
60  mapper.map(&temp.num_bits_per_int_);
61  mapper.map(&temp.mask_);
62  mapper.map(&temp.size_);
63  temp.swap(this);
64}
65
66void IntVector::load(const char *filename,
67    long offset, int whence) {
68  Reader reader;
69  reader.open(filename, offset, whence);
70  read(reader);
71}
72
73void IntVector::fread(std::FILE *file) {
74  Reader reader(file);
75  read(reader);
76}
77
78void IntVector::read(int fd) {
79  Reader reader(fd);
80  read(reader);
81}
82
83void IntVector::read(std::istream &stream) {
84  Reader reader(&stream);
85  read(reader);
86}
87
88void IntVector::read(Reader &reader) {
89  IntVector temp;
90  temp.units_.read(reader);
91  reader.read(&temp.num_bits_per_int_);
92  reader.read(&temp.mask_);
93  reader.read(&temp.size_);
94  temp.swap(this);
95}
96
97void IntVector::save(const char *filename, bool trunc_flag,
98    long offset, int whence) const {
99  Writer writer;
100  writer.open(filename, trunc_flag, offset, whence);
101  write(writer);
102}
103
104void IntVector::fwrite(std::FILE *file) const {
105  Writer writer(file);
106  write(writer);
107}
108
109void IntVector::write(int fd) const {
110  Writer writer(fd);
111  write(writer);
112}
113
114void IntVector::write(std::ostream &stream) const {
115  Writer writer(&stream);
116  write(writer);
117}
118
119void IntVector::write(Writer &writer) const {
120  units_.write(writer);
121  writer.write(num_bits_per_int_);
122  writer.write(mask_);
123  writer.write(size_);
124}
125
126void IntVector::clear() {
127  IntVector().swap(this);
128}
129
130void IntVector::swap(IntVector *rhs) {
131  MARISA_THROW_IF(rhs == NULL, MARISA_PARAM_ERROR);
132  units_.swap(&rhs->units_);
133  Swap(&num_bits_per_int_, &rhs->num_bits_per_int_);
134  Swap(&mask_, &rhs->mask_);
135  Swap(&size_, &rhs->size_);
136}
137
138}  // namespace marisa
139