/usr/include/sdsl/csa_alphabet_strategy.hpp is in libsdsl-dev 2.0.3-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | /* sdsl - succinct data structures library
Copyright (C) 2012 Simon Gog
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
*/
/*! \file csa_alphabet_strategy.hpp
\brief csa_alphabet_strategy.hpp includes different strategy classes for representing an alphabet of a CSA.
\author Simon Gog
*/
#ifndef INCLUDED_CSA_ALPHABET_STRATEGY
#define INCLUDED_CSA_ALPHABET_STRATEGY
// TODO: Strategy with 1-to-1 mapping and C_array type as template parameter
// This can be also used for a integer based CSA.
/* A alphabet strategy provides the following features:
* * Member `sigma` which contains the size (=umber of unique symbols) of the alphabet.
* * Method `is_present(char_type c)` which indicates if character c occurs in the text.
* * Container `char2comp` which maps a symbol to a number [0..sigma-1]. The alphabetic
* order is preserved.
* * Container `comp2char` which is the inverse mapping of char2comp.
* * Container `C` contains the cumulative counts of occurrences. C[i] is the cumulative
* count of occurrences of symbols `comp2char[0]` to `comp2char[i-1]` in the text.
* * Typedefs for the four above members:
* * char2comp_type
* * comp2char_type
* * C_type
* * sigma_type
* * Constructor. Takes a int_vector_buffer<8> for byte-alphabets
* and int_vector_buffer<0> for integer-alphabets.
*
* \par Note
* sigma_type has to be large enough to represent the alphabet size 2*sigma,
* since there is code which will perform a binary search on array `C`.
*/
#include "int_vector.hpp"
#include "sd_vector.hpp"
#include "rank_support.hpp"
#include "select_support.hpp"
#include "sdsl_concepts.hpp"
#include "config.hpp"
#include <string>
namespace sdsl
{
// forward declarations
class byte_alphabet;
template<class bit_vector_type = bit_vector,
class rank_support_type = rank_support_scan<>, //typename bit_vector_type::rank_1_type,
class select_support_type = select_support_scan<>, //typename bit_vector_type::select_1_type,
class C_array_type = int_vector<>
>
class succinct_byte_alphabet;
template<class bit_vector_type = sd_vector<>,
class rank_support_type = typename bit_vector_type::rank_1_type,
class select_support_type = typename bit_vector_type::select_1_type,
class C_array_type = int_vector<>
>
class int_alphabet;
template <uint8_t int_width>
struct key_trait {
static const char* KEY_BWT;
static const char* KEY_TEXT;
};
template<>
struct key_trait<8> {
static const char* KEY_BWT;
static const char* KEY_TEXT;
};
template<uint8_t int_width>
const char* key_trait<int_width>::KEY_BWT = conf::KEY_BWT_INT;
template<uint8_t int_width>
const char* key_trait<int_width>::KEY_TEXT = conf::KEY_TEXT_INT;
template<class t_alphabet_strategy>
struct alphabet_trait {
typedef byte_alphabet type;
};
template<>
struct alphabet_trait<int_alphabet_tag> {
typedef int_alphabet<> type;
};
//! A simple space greedy representation for byte alphabets.
/*!
* \par Space consumption:
* At least: 2.5 kB
* Details: char2comp + comp2char take 2*256 + 2*8 bytes
* m_C takes 257*8 bytes
* m_sigma takes 2 bytes
*/
class byte_alphabet
{
public:
typedef int_vector<>::size_type size_type;
typedef int_vector<8> char2comp_type;
typedef int_vector<8> comp2char_type;
typedef int_vector<64> C_type;
typedef uint16_t sigma_type;
typedef uint8_t char_type;
typedef uint8_t comp_char_type;
typedef std::string string_type;
enum { int_width = 8 };
typedef byte_alphabet_tag alphabet_category;
private:
char2comp_type m_char2comp; // Mapping from a character into the compact alphabet.
comp2char_type m_comp2char; // Inverse mapping of m_char2comp.
C_type m_C; // Cumulative counts for the compact alphabet [0..sigma].
sigma_type m_sigma; // Effective size of the alphabet.
void copy(const byte_alphabet&);
public:
const char2comp_type& char2comp;
const comp2char_type& comp2char;
const C_type& C;
const sigma_type& sigma;
//! Default constructor
byte_alphabet();
//! Construct from a byte-stream
/*!
* \param text_buf Byte stream.
* \param len Length of the byte stream.
*/
byte_alphabet(int_vector_buffer<8>& text_buf, int_vector_size_type len);
byte_alphabet(const byte_alphabet&);
byte_alphabet(byte_alphabet&& b) : byte_alphabet() {
*this = std::move(b);
}
byte_alphabet& operator=(const byte_alphabet&);
byte_alphabet& operator=(byte_alphabet&&);
void swap(byte_alphabet&);
size_type serialize(std::ostream& out, structure_tree_node* v=nullptr, std::string name="")const;
void load(std::istream& in);
};
//! A space-efficient representation for byte alphabets.
/*!
* The mapping `char2comp` and its inverse `comp2char` is realized internally
* by a bitvector of size 256 bits and a rank and a select structure. The rank
* structure is used to calculate `char2comp`; the select structure is used to
* calculate `comp2char`. Array `C` is represented by a bit-compressed
* `int_vector` and `sigma` by a uint16_t.
* The types to represent `char2comp`, `comp2char`, and `C` can be specified
* by template parameters.
*/
template<class bit_vector_type, class rank_support_type, class select_support_type, class C_array_type>
class succinct_byte_alphabet
{
public:
class char2comp_wrapper;
class comp2char_wrapper;
friend class char2comp_wrapper;
friend class comp2char_wrapper;
typedef int_vector<>::size_type size_type;
typedef char2comp_wrapper char2comp_type;
typedef comp2char_wrapper comp2char_type;
typedef C_array_type C_type;
typedef uint16_t sigma_type;
typedef uint8_t char_type;
typedef uint8_t comp_char_type;
typedef std::string string_type;
typedef byte_alphabet_tag alphabet_category;
enum { int_width = 8 };
//! Helper class for the char2comp mapping
class char2comp_wrapper
{
private:
const succinct_byte_alphabet* m_strat;
public:
char2comp_wrapper(const succinct_byte_alphabet* strat) : m_strat(strat) {}
comp_char_type operator[](char_type c) const {
return (comp_char_type) m_strat->m_char_rank((size_type)c);
}
};
//! Helper class for the comp2char mapping
class comp2char_wrapper
{
private:
const succinct_byte_alphabet* m_strat;
public:
comp2char_wrapper(const succinct_byte_alphabet* strat) : m_strat(strat) {}
char_type operator[](comp_char_type c) const {
return (char_type) m_strat->m_char_select(((size_type)c)+1);
}
};
private:
bit_vector_type m_char; // `m_char[i]` indicates if character with code i is present or not
rank_support_type m_char_rank; // rank data structure for `m_char` to answer char2comp
select_support_type m_char_select; // select data structure for `m_char` to answer comp2char
C_type m_C; // cumulative counts for the compact alphabet [0..sigma]
sigma_type m_sigma; // effective size of the alphabet
void copy(const succinct_byte_alphabet& strat) {
m_char = strat.m_char;
m_char_rank = strat.m_char_rank;
m_char_rank.set_vector(&m_char);
m_char_select = strat.m_char_select;
m_char_select.set_vector(&m_char);
m_C = strat.m_C;
m_sigma = strat.m_sigma;
}
public:
const char2comp_type char2comp;
const comp2char_type comp2char;
const C_type& C;
const sigma_type& sigma;
//! Default constructor
succinct_byte_alphabet() : char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
m_sigma = 0;
}
//! Construct from a byte-stream
/*!
* \param text_buf Byte stream.
* \param len Length of the byte stream.
*/
succinct_byte_alphabet(int_vector_buffer<8>& text_buf, int_vector_size_type len):
char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
m_sigma = 0;
if (0 == len or 0 == text_buf.size())
return;
assert(len <= text_buf.size());
// initialize vectors
int_vector<64> D(257, 0);
bit_vector tmp_char(256, 0);
// count occurrences of each symbol
for (size_type i=0; i < len; ++i) {
++D[text_buf[i]];
}
assert(1 == D[0]); // null-byte should occur exactly once
m_sigma = 0;
for (int i=0; i<256; ++i)
if (D[i]) {
tmp_char[i] = 1; // mark occurring character
D[m_sigma] = D[i]; // compactify m_C
++m_sigma;
}
// resize to sigma+1, since CSAs also need the sum of all elements
m_C = C_type(m_sigma+1, 0, bits::hi(len)+1);
for (int i=(int)m_sigma; i > 0; --i) m_C[i] = D[i-1];
m_C[0] = 0;
for (int i=1; i <= (int)m_sigma; ++i) m_C[i] = m_C[i] + m_C[i-1];
assert(m_C[sigma]==len);
m_char = tmp_char;
util::init_support(m_char_rank, &m_char);
util::init_support(m_char_select, &m_char);
}
//! Copy constructor
succinct_byte_alphabet(const succinct_byte_alphabet& strat): char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
copy(strat);
}
//! Move constructor
succinct_byte_alphabet(succinct_byte_alphabet&& strat) {
*this = std::move(strat);
}
succinct_byte_alphabet& operator=(const succinct_byte_alphabet& strat) {
if (this != &strat) {
copy(strat);
}
return *this;
}
succinct_byte_alphabet& operator=(succinct_byte_alphabet&& strat) {
if (this != &strat) {
m_char = std::move(strat.m_char);
m_char_rank = std::move(strat.m_char_rank);
m_char_rank.set_vector(&m_char);
m_char_select = std::move(strat.m_char_select);
m_char_select.set_vector(&m_char);
m_C = std::move(strat.m_C);
m_sigma = std::move(strat.m_sigma);
}
return *this;
}
//! Swap operator
void swap(succinct_byte_alphabet& strat) {
m_char.swap(strat.m_char);
util::swap_support(m_char_rank, strat.m_char_rank, &m_char, &(strat.m_char));
util::swap_support(m_char_select, strat.m_char_select, &m_char, &(strat.m_char));
m_C.swap(strat.m_C);
std::swap(m_sigma,strat.m_sigma);
}
//! Serialize method
size_type serialize(std::ostream& out, structure_tree_node* v=nullptr, std::string name="")const {
structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
size_type written_bytes = 0;
written_bytes += m_char.serialize(out, child, "m_char");
written_bytes += m_char_rank.serialize(out, child, "m_char_rank");
written_bytes += m_char_select.serialize(out, child, "m_char_select");
written_bytes += m_C.serialize(out, child, "m_C");
written_bytes += write_member(m_sigma, out, child, "m_sigma");
structure_tree::add_size(child, written_bytes);
return written_bytes;
}
//! Load method
void load(std::istream& in) {
m_char.load(in);
m_char_rank.load(in);
m_char_rank.set_vector(&m_char);
m_char_select.load(in);
m_char_select.set_vector(&m_char);
m_C.load(in);
read_member(m_sigma, in);
}
};
//! A space-efficient representation for byte alphabets.
/*!
* The mapping `char2comp` and its inverse `comp2char` is realized internally
* by a bitvector of size sigma bits and a rank and a select structure, if the
* alphabet contains not all symbols in the range [0..sigma-1]. If it contains
* all symbols, i.e. the alphabet is continuous, then we map the symbols
* directly and no extra space is used.
*
* The types to represent `char2comp`, `comp2char`, and `C` can be specified
* by template parameters.
*/
template<class bit_vector_type, class rank_support_type, class select_support_type, class C_array_type>
class int_alphabet
{
public:
class char2comp_wrapper;
class comp2char_wrapper;
friend class char2comp_wrapper;
friend class comp2char_wrapper;
typedef int_vector<>::size_type size_type;
typedef char2comp_wrapper char2comp_type;
typedef comp2char_wrapper comp2char_type;
typedef C_array_type C_type;
typedef uint64_t sigma_type;
typedef uint64_t char_type;
typedef uint64_t comp_char_type;
typedef std::vector<char_type> string_type;
typedef int_alphabet_tag alphabet_category;
enum { int_width = 0 };
//! Helper class for the char2comp mapping
class char2comp_wrapper
{
private:
const int_alphabet* m_strat;
public:
char2comp_wrapper(const int_alphabet* strat) : m_strat(strat) {}
comp_char_type operator[](char_type c) const {
if (m_strat->m_char.size() > 0) { // if alphabet is not continuous
return (comp_char_type) m_strat->m_char_rank((size_type)c);
} else { // direct map if it is continuous
return (comp_char_type) c;
}
}
};
//! Helper class for the comp2char mapping
class comp2char_wrapper
{
private:
const int_alphabet* m_strat;
public:
comp2char_wrapper(const int_alphabet* strat) : m_strat(strat) {}
char_type operator[](comp_char_type c) const {
if (m_strat->m_char.size() > 0) { // if alphabet is not continuous
return (char_type) m_strat->m_char_select(((size_type)c)+1);
} else { // direct map if it is continuous
return (char_type) c;
}
}
};
private:
bit_vector_type m_char; // `m_char[i]` indicates if character with code i is present or not
rank_support_type m_char_rank; // rank data structure for `m_char` to answer char2comp
select_support_type m_char_select; // select data structure for `m_char` to answer comp2char
C_type m_C; // cumulative counts for the compact alphabet [0..sigma]
sigma_type m_sigma; // effective size of the alphabet
void copy(const int_alphabet& strat) {
m_char = strat.m_char;
m_char_rank = strat.m_char_rank;
m_char_rank.set_vector(&m_char);
m_char_select = strat.m_char_select;
m_char_select.set_vector(&m_char);
m_C = strat.m_C;
m_sigma = strat.m_sigma;
}
//! Check if the alphabet is continuous.
bool is_continuous_alphabet(std::map<size_type, size_type>& D) {
if (D.size() == 0) { // an empty alphabet is continuous
return true;
} else {
// max key + 1 == size of map
return ((--D.end())->first + 1) == D.size();
}
}
public:
const char2comp_type char2comp;
const comp2char_type comp2char;
const C_type& C;
const sigma_type& sigma;
//! Default constructor
int_alphabet() : char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
m_sigma = 0;
}
//! Construct from a byte-stream
/*!
* \param text_buf Byte stream.
* \param len Length of the byte stream.
*/
int_alphabet(int_vector_buffer<0>& text_buf, int_vector_size_type len):
char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
m_sigma = 0;
if (0 == len or 0 == text_buf.size())
return;
assert(len <= text_buf.size());
// initialize vectors
std::map<size_type, size_type> D;
// count occurrences of each symbol
for (size_type i=0; i < len; ++i) {
D[text_buf[i]]++;
}
m_sigma = D.size();
if (is_continuous_alphabet(D)) {
// do not initialize m_char, m_char_rank and m_char_select since we can map directly
} else {
// note: the alphabet has at least size 1, so the following is safe:
size_type largest_symbol = (--D.end())->first;
bit_vector tmp_char(largest_symbol+1, 0);
for (std::map<size_type, size_type>::const_iterator it = D.begin(), end=D.end(); it != end; ++it) {
tmp_char[it->first] = 1;
}
m_char = tmp_char;
util::init_support(m_char_rank, &m_char);
util::init_support(m_char_select, &m_char);
}
assert(D.find(0) != D.end() and 1 == D[0]); // null-byte should occur exactly once
// resize to sigma+1, since CSAs also need the sum of all elements
m_C = C_type(m_sigma+1, 0, bits::hi(len)+1);
size_type sum = 0, idx=0;
for (std::map<size_type, size_type>::const_iterator it = D.begin(), end=D.end(); it != end; ++it) {
m_C[idx++] = sum;
sum += it->second;
}
m_C[idx] = sum; // insert sum of all elements
}
//! Copy constructor
int_alphabet(const int_alphabet& strat): char2comp(this), comp2char(this), C(m_C), sigma(m_sigma) {
copy(strat);
}
//! Copy constructor
int_alphabet(int_alphabet&& strat) {
*this = std::move(strat);
}
int_alphabet& operator=(const int_alphabet& strat) {
if (this != &strat) {
copy(strat);
}
return *this;
}
int_alphabet& operator=(int_alphabet&& strat) {
if (this != &strat) {
m_char = std::move(strat.m_char);
m_char_rank = std::move(strat.m_char_rank);
m_char_rank.set_vector(&m_char);
m_char_select = std::move(strat.m_char_select);
m_char_select.set_vector(&m_char);
m_C = std::move(strat.m_C);
m_sigma = std::move(strat.m_sigma);
}
return *this;
}
//! Swap operator
void swap(int_alphabet& strat) {
m_char.swap(strat.m_char);
util::swap_support(m_char_rank, strat.m_char_rank, &m_char, &(strat.m_char));
util::swap_support(m_char_select, strat.m_char_select, &m_char, &(strat.m_char));
m_C.swap(strat.m_C);
std::swap(m_sigma,strat.m_sigma);
}
//! Serialize method
size_type serialize(std::ostream& out, structure_tree_node* v=nullptr, std::string name="")const {
structure_tree_node* child = structure_tree::add_child(v, name, util::class_name(*this));
size_type written_bytes = 0;
written_bytes += m_char.serialize(out, child, "m_char");
written_bytes += m_char_rank.serialize(out, child, "m_char_rank");
written_bytes += m_char_select.serialize(out, child, "m_char_select");
written_bytes += m_C.serialize(out, child, "m_C");
written_bytes += write_member(m_sigma, out, child, "m_sigma");
structure_tree::add_size(child, written_bytes);
return written_bytes;
}
//! Load method
void load(std::istream& in) {
m_char.load(in);
m_char_rank.load(in);
m_char_rank.set_vector(&m_char);
m_char_select.load(in);
m_char_select.set_vector(&m_char);
m_C.load(in);
read_member(m_sigma, in);
}
};
} // end namespace sdsl
#endif
|