1 #ifndef KALLISTO_KMERHASHTABLE_H 2 #define KALLISTO_KMERHASHTABLE_H 10 template<
typename T,
typename Hash = KmerHash>
13 using value_type = std::pair<Kmer, T>;
15 using mapped_type = T;
19 size_t size_, pop, num_empty;
25 template<
bool is_const_iterator = true>
26 class iterator_ :
public std::iterator<std::forward_iterator_tag, value_type> {
30 typedef typename std::conditional<is_const_iterator, const KmerHashTable *, KmerHashTable *>::type DataStructurePointerType;
31 typedef typename std::conditional<is_const_iterator, const value_type&, value_type&>::type ValueReferenceType;
32 typedef typename std::conditional<is_const_iterator, const value_type *, value_type *>::type ValuePointerType;
35 DataStructurePointerType ht;
39 iterator_(DataStructurePointerType ht_) : ht(ht_), h(ht_->size_) {}
40 iterator_(DataStructurePointerType ht_,
size_t h_) : ht(ht_), h(h_) {}
44 ValueReferenceType operator*()
const {
return ht->table[h];}
45 ValuePointerType operator->()
const {
return &(ht->table[h]);}
47 size_t getHash()
const {
return h; }
53 if (ht->table !=
nullptr && ht->size_>0) {
55 Kmer& km = ht->table[h].first;
56 if (km == ht->empty_val.first || km == ht->deleted.first) operator++();
69 if (h == ht->size_)
return *
this;
73 for (; h < ht->size_; ++h) {
75 Kmer& km = ht->table[h].first;
77 if (km != ht->empty_val.first && km != ht->deleted.first)
break;
83 bool operator==(
const iterator_ &o)
const {
return (ht->table == o.ht->table) && (h == o.h);}
84 bool operator!=(
const iterator_ &o)
const {
return !(this->operator==(o));}
94 KmerHashTable(
const Hash& h = Hash() ) : hasher(h), table(
nullptr), size_(0), pop(0), num_empty(0) {
96 empty_val.first.set_empty();
97 deleted.first.set_deleted();
101 KmerHashTable(
size_t sz,
const Hash& h = Hash() ) : hasher(h), table(
nullptr), size_(0), pop(0), num_empty(0) {
102 empty_val.first.set_empty();
103 deleted.first.set_deleted();
104 init_table((
size_t) (1.2*sz));
112 num_empty = o.num_empty;
114 empty_val = o.empty_val;
131 num_empty = o.num_empty;
133 empty_val = o.empty_val;
148 if (table !=
nullptr) {
160 size_t size()
const {
return pop; }
162 bool empty()
const {
return pop == 0; }
166 std::fill(table, table+size_, empty_val);
172 void init_table(
size_t sz) {
178 table =
new value_type[size_];
184 iterator find(
const Kmer& key) {
186 size_t h = hasher(key) & (size_-1);
187 size_t end_h = (h == 0) ? (size_-1) : h-1;
189 for (;; h = (h+1!=size_ ? h+1 : 0)) {
191 if (table[h].first == empty_val.first)
return iterator(
this);
192 else if (table[h].first == key)
return iterator(
this, h);
194 if (h==end_h)
return iterator(
this);
198 const_iterator find(
const Kmer& key)
const {
200 size_t h = hasher(key) & (size_-1);
201 size_t end_h = (h == 0) ? (size_-1) : h-1;
203 for (;; h = (h+1!=size_ ? h+1 : 0)) {
205 if (table[h].first == empty_val.first)
return const_iterator(
this);
206 else if (table[h].first == key)
return const_iterator(
this, h);
208 if (h==end_h)
return const_iterator(
this);
212 iterator find(
const size_t h) {
214 if ((h < size_) && (table[h].first != empty_val.first) && (table[h].first != deleted.first))
215 return iterator(
this, h);
217 return iterator(
this);
220 const_iterator find(
const size_t h)
const {
222 if ((h < size_) && (table[h].first != empty_val.first) && (table[h].first != deleted.first))
223 return const_iterator(
this, h);
225 return const_iterator(
this);
228 iterator erase(const_iterator pos) {
230 if (pos == this->end())
return this->end();
237 return ++iterator(
this, h);
240 iterator erase(
const size_t h) {
242 if (h >= size_)
return this->end();
247 return ++iterator(
this, h);
250 size_t erase(
const Kmer& km) {
252 const_iterator pos = find(km);
255 if (pos != this->end()) erase(pos);
260 std::pair<iterator,bool> insert(
const value_type& val) {
262 if ((5*num_empty) < size_) reserve(2*size_);
264 bool is_deleted =
false;
266 for (
size_t h = hasher(val.first) & (size_-1), h_tmp;; h = (h+1 != size_ ? h+1 : 0)) {
268 if (table[h].first == empty_val.first) {
270 if (!is_deleted) num_empty--;
276 return {iterator(
this, h),
true};
278 else if (table[h].first == val.first)
return {iterator(
this, h),
false};
279 else if (!is_deleted && (table[h].first == deleted.first)) {
286 void reserve(
size_t sz) {
288 if (sz <= size_)
return;
290 value_type *old_table = table;
291 size_t old_size_ = size_;
297 table =
new value_type[size_];
299 std::fill(table, table+size_, empty_val);
301 for (
size_t i = 0; i < old_size_; i++) {
303 if (old_table[i].first != empty_val.first && old_table[i].first != deleted.first) insert(old_table[i]);
362 size_t rndup(
size_t v) {
381 const_iterator begin()
const {
383 const_iterator it(
this);
388 iterator end() {
return iterator(
this); }
390 const_iterator end()
const {
return const_iterator(
this); }
410 template<
typename T,
typename Hash = MinimizerHash>
413 using value_type = std::pair<Minimizer, T>;
415 using mapped_type = T;
419 size_t size_, pop, num_empty;
423 value_type empty_val;
427 template<
bool is_const_iterator = true>
428 class iterator_ :
public std::iterator<std::forward_iterator_tag, value_type> {
432 typedef typename std::conditional<is_const_iterator, const MinimizerHashTable *, MinimizerHashTable *>::type DataStructurePointerType;
433 typedef typename std::conditional<is_const_iterator, const value_type&, value_type&>::type ValueReferenceType;
434 typedef typename std::conditional<is_const_iterator, const value_type *, value_type *>::type ValuePointerType;
436 DataStructurePointerType ht;
440 iterator_(DataStructurePointerType ht_) : ht(ht_), h(ht_->size_) {}
441 iterator_(DataStructurePointerType ht_,
size_t h_) : ht(ht_), h(h_) {}
445 ValueReferenceType operator*()
const {
return ht->table[h];}
446 ValuePointerType operator->()
const {
return &(ht->table[h]);}
448 size_t getHash()
const {
return h; }
454 if (ht->table !=
nullptr && ht->size_>0) {
458 if (minz == ht->empty_val.first || minz == ht->deleted.first) operator++();
471 if (h == ht->size_)
return *
this;
475 for (; h < ht->size_; ++h) {
479 if (minz != ht->empty_val.first && minz != ht->deleted.first)
break;
485 bool operator==(
const iterator_ &o)
const {
return (ht->table == o.ht->table) && (h == o.h);}
486 bool operator!=(
const iterator_ &o)
const {
return !(this->operator==(o));}
494 MinimizerHashTable(
const Hash& h = Hash() ) : hasher(h), table(
nullptr), size_(0), pop(0), num_empty(0) {
496 empty_val.first.set_empty();
497 deleted.first.set_deleted();
501 MinimizerHashTable(
size_t sz,
const Hash& h = Hash() ) : hasher(h), table(
nullptr), size_(0), pop(0), num_empty(0) {
503 empty_val.first.set_empty();
504 deleted.first.set_deleted();
505 init_table((
size_t) (1.2*sz));
513 num_empty = o.num_empty;
515 empty_val = o.empty_val;
532 num_empty = o.num_empty;
534 empty_val = o.empty_val;
549 if (table !=
nullptr) {
561 size_t size()
const {
return pop; }
563 bool empty()
const {
return pop == 0; }
567 std::fill(table, table+size_, empty_val);
573 void init_table(
size_t sz) {
579 table =
new value_type[size_];
587 size_t h = hasher(key) & (size_-1);
588 size_t end_h = (h == 0) ? (size_-1) : h-1;
590 for (;; h = (h+1!=size_ ? h+1 : 0)) {
592 if (table[h].first == empty_val.first)
return iterator(
this);
593 else if (table[h].first == key)
return iterator(
this, h);
596 if (h==end_h)
return iterator(
this);
600 const_iterator find(
const Minimizer& key)
const {
602 size_t h = hasher(key) & (size_-1);
603 size_t end_h = (h == 0) ? (size_-1) : h-1;
605 for (;; h = (h+1!=size_ ? h+1 : 0)) {
607 if (table[h].first == empty_val.first)
return const_iterator(
this);
608 else if (table[h].first == key)
return const_iterator(
this, h);
610 if (h==end_h)
return const_iterator(
this);
614 iterator find(
const size_t h) {
616 if ((h < size_) && (table[h].first != empty_val.first) && (table[h].first != deleted.first))
617 return iterator(
this, h);
619 return iterator(
this);
622 const_iterator find(
const size_t h)
const {
624 if ((h < size_) && (table[h].first != empty_val.first) && (table[h].first != deleted.first))
625 return const_iterator(
this, h);
627 return const_iterator(
this);
630 iterator erase(const_iterator pos) {
632 if (pos == this->end())
return this->end();
634 table[pos.h] = deleted;
637 return ++iterator(
this, pos.h);
642 const_iterator pos = find(minz);
645 if (pos != this->end()) erase(pos);
650 std::pair<iterator,bool> insert(
const value_type& val) {
652 if ((5*num_empty) < size_) reserve(2*size_);
654 bool is_deleted =
false;
656 for (
size_t h = hasher(val.first) & (size_-1), h_tmp;; h = (h+1 != size_ ? h+1 : 0)) {
658 if (table[h].first == empty_val.first) {
660 if (!is_deleted) num_empty--;
666 return {iterator(
this, h),
true};
668 else if (table[h].first == val.first)
return {iterator(
this, h),
false};
669 else if (!is_deleted && (table[h].first == deleted.first)) {
676 void reserve(
size_t sz) {
678 if (sz <= size_)
return;
680 value_type *old_table = table;
681 size_t old_size_ = size_;
687 table =
new value_type[size_];
689 std::fill(table, table+size_, empty_val);
691 for (
size_t i = 0; i < old_size_; i++) {
693 if (old_table[i].first != empty_val.first && old_table[i].first != deleted.first) insert(old_table[i]);
752 size_t rndup(
size_t v) {
773 const_iterator begin()
const {
775 const_iterator it(
this);
780 iterator end() {
return iterator(
this); }
782 const_iterator end()
const {
return const_iterator(
this); }
802 #endif // KALLISTO_KMERHASHTABLE_H Definition: KmerHashTable.h:26
Definition: KmerHashTable.h:11
Definition: KmerHashTable.h:428
Definition: KmerHashTable.h:411