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
|
/*
* Copyright (c) 2010 Daisuke Okanohara
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above Copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above Copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the authors nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*/
#ifndef WAT_ARRAY_BIT_ARRAY_HPP_
#define WAT_ARRAY_BIT_ARRAY_HPP_
#include <stdint.h>
#include <vector>
#include <iostream>
namespace wat_array {
enum {
NOTFOUND = 0xFFFFFFFFFFFFFFFFLLU
};
class BitArray {
private:
enum {
BLOCK_BITNUM = 64,
TABLE_INTERVAL = 4
};
public:
BitArray();
~BitArray();
BitArray(uint64_t size);
uint64_t length() const;
uint64_t one_num() const;
void Init(uint64_t size);
void Clear();
void SetBit(uint64_t bit, uint64_t pos);
void Build();
uint64_t Rank(uint64_t bit, uint64_t pos) const;
uint64_t Select(uint64_t bit, uint64_t rank) const;
uint64_t Lookup(uint64_t pos) const;
static uint64_t PopCount(uint64_t x);
static uint64_t PopCountMask(uint64_t x, uint64_t offset);
static uint64_t SelectInBlock(uint64_t x, uint64_t rank);
static uint64_t GetBitNum(uint64_t one_num, uint64_t num, uint64_t bit);
void PrintForDebug(std::ostream& os) const;
void Save(std::ostream& os) const;
void Load(std::istream& is);
private:
uint64_t RankOne(uint64_t pos) const;
uint64_t SelectOutBlock(uint64_t bit, uint64_t& rank) const;
private:
std::vector<uint64_t> bit_blocks_;
std::vector<uint64_t> rank_tables_;
uint64_t length_;
uint64_t one_num_;
};
}
#endif // WAT_ARRAY_BIT_ARRAY_HPP_
|