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
|
#ifndef BLOOMFILTERWINDOW_H
#define BLOOMFILTERWINDOW_H 1
#include "Bloom.h"
#include "BloomFilter.h"
#include "Common/HashFunction.h"
#include "Common/Kmer.h"
#include "Common/IOUtil.h"
#include <algorithm>
#include <vector>
#include <iostream>
/**
* A bloom filter that represents a window
* within a larger bloom filter.
*/
class BloomFilterWindow : public Konnector::BloomFilter
{
public:
/** Constructor. */
BloomFilterWindow() : Konnector::BloomFilter() { };
/** Constructor.
*
* @param fullBloomSize size in bits of the containing bloom filter
* @param startBitPos index of first bit in the window
* @param endBitPos index of last bit in the window
*/
BloomFilterWindow(size_t fullBloomSize, size_t startBitPos,
size_t endBitPos, size_t hashSeed=0) :
Konnector::BloomFilter(endBitPos - startBitPos + 1, hashSeed),
m_fullBloomSize(fullBloomSize),
m_startBitPos(startBitPos),
m_endBitPos(endBitPos)
{
assert(startBitPos < fullBloomSize);
assert(endBitPos < fullBloomSize);
assert(startBitPos <= endBitPos);
}
/**
* Get the full size (in bits) of the bloom filter that
* this window is a part of.
*/
size_t fullBloomSize()
{
return m_fullBloomSize;
}
/** Get the start bit position for the window. */
size_t startBitPos()
{
return m_startBitPos;
}
/** Get the end bit position for the window. */
size_t endBitPos()
{
return m_endBitPos;
}
/** Return the size of the bit array. */
size_t size() const
{
return Konnector::BloomFilter::size();
}
/** Return the number of elements with count >= max_count. */
size_t popcount() const
{
return Konnector::BloomFilter::popcount();
}
/** Return the estimated false positive rate */
double FPR() const
{
return Konnector::BloomFilter::FPR();
}
/** Return whether the specified bit is set. */
bool operator[](size_t i) const
{
if (i >= m_startBitPos && i <= m_endBitPos)
return Konnector::BloomFilter::operator[](i - m_startBitPos);
return false;
}
/** Return whether the object is present in this set. */
bool operator[](const Bloom::key_type& key) const
{
return (*this)[Bloom::hash(key, m_hashSeed) % m_fullBloomSize];
}
/** Add the object with the specified index to this set. */
void insert(size_t i)
{
if (i >= m_startBitPos && i <= m_endBitPos)
Konnector::BloomFilter::insert(i - m_startBitPos);
}
/** Add the object to this set. */
void insert(const Bloom::key_type& key)
{
insert(Bloom::hash(key, m_hashSeed) % m_fullBloomSize);
}
/** Operator for reading a bloom filter from a stream. */
friend std::istream& operator>>(std::istream& in, BloomFilterWindow& o)
{
o.read(in, BITWISE_OVERWRITE);
return in;
}
/** Operator for writing the bloom filter to a stream. */
friend std::ostream& operator<<(std::ostream& out, const BloomFilterWindow& o)
{
o.write(out);
return out;
}
/** Read a bloom filter window from a stream. */
void read(std::istream& in, BitwiseOp readOp = BITWISE_OVERWRITE)
{
Bloom::FileHeader header = Bloom::readHeader(in);
assert(in);
m_fullBloomSize = header.fullBloomSize;
m_startBitPos = header.startBitPos;
m_endBitPos = header.endBitPos;
if (m_hashSeed != header.hashSeed) {
if (readOp == BITWISE_OVERWRITE) {
m_hashSeed = header.hashSeed;
} else {
std::cerr << "error: can't union/intersect bloom filters with "
<< "different hash seed values\n";
exit(EXIT_FAILURE);
}
}
size_t bits = header.endBitPos - header.startBitPos + 1;
if (m_size != bits) {
if (readOp == BITWISE_OVERWRITE) {
Konnector::BloomFilter::resize(bits);
} else {
std::cerr << "error: can't union/intersect bloom filters with "
<< "different sizes\n";
exit(EXIT_FAILURE);
}
}
readBits(in, m_array, bits, 0, readOp);
assert(in);
}
/** Write a bloom filter window to a stream. */
void write(std::ostream& out) const
{
Bloom::FileHeader header;
header.fullBloomSize = m_fullBloomSize;
header.startBitPos = m_startBitPos;
header.endBitPos = m_endBitPos;
header.hashSeed = m_hashSeed;
Bloom::writeHeader(out, header);
assert(out);
size_t windowSize = m_endBitPos - m_startBitPos + 1;
out.write(m_array, (windowSize + 7)/8);
assert(out);
}
private:
size_t m_fullBloomSize;
size_t m_startBitPos, m_endBitPos;
};
#endif
|