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
|
// Copyright (c) 2000
// Kevin Atkinson
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. Kevin Atkinson makes no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
#ifndef __autil_hash_t_hh__
#define __autil_hash_t_hh__
#include <math.h>
#include "vector_hash.hpp"
#include "primes.hpp"
namespace aspeller {
template <class Parms>
VectorHashTable<Parms>::FindIterator
::FindIterator(const HashTable * ht, const key_type & k)
: vector(&ht->vector())
, parms(&ht->parms())
, key(k)
, i(ht->hash1(k))
, hash2(ht->hash2(k))
{
if (!parms->is_nonexistent((*vector)[i])
&& !parms->equal(parms->key((*vector)[i]), key))
adv();
}
template <class Parms>
void VectorHashTable<Parms>::FindIterator::adv() {
do {
i = (i + hash2) % vector->size();
} while (!parms->is_nonexistent((*vector)[i])
&& !parms->equal(parms->key((*vector)[i]), key));
}
template<class Parms>
void VectorHashTable<Parms>::nonexistent_vector() {
vector_iterator vector_end = vector_.end();
for (vector_iterator i = vector_.begin(); i != vector_end; ++i) {
parms_.make_nonexistent(*i);
}
}
template<class Parms>
std::pair<typename VectorHashTable<Parms>::iterator, bool>
VectorHashTable<Parms>::insert(const value_type & d)
{
MutableFindIterator j(this, parms_.key(d));
vector_iterator i = vector_.begin() + j.i;
if (!parms_.is_multi && !j.at_end())
return std::pair<iterator,bool>(iterator(i,this),false);
if (load_factor() > .92) {
resize(bucket_count()*2);
return insert(d);
}
if (parms_.is_multi)
while(!j.at_end()) j.adv();
*(vector_.begin() + j.i) = d;
++size_;
return std::pair<iterator,bool>(iterator(i,this),true);
}
template<class Parms>
bool VectorHashTable<Parms>::have(const key_type & key) const
{
return !ConstFindIterator(this,key).at_end();
}
template<class Parms>
typename VectorHashTable<Parms>::iterator
VectorHashTable<Parms>::find(const key_type & key)
{
MutableFindIterator i(this, key);
if (!i.at_end())
return iterator(vector_.begin() + i.i, this);
else
return end();
}
template<class Parms>
typename VectorHashTable<Parms>::const_iterator
VectorHashTable<Parms>::find(const key_type & key) const {
ConstFindIterator i(this, key);
if (!i.at_end())
return const_iterator(vector_.begin() + i.i, this);
else
return end();
}
#if 0 // it currently doesn't work needs fixing
template<class Parms>
VectorHashTable<Parms>::size_type
VectorHashTable<Parms>::erase(const key_type & key) {
MutableFindIterator i(this, key);
if (!i.at_end()) {
erase(iterator(vector_.begin() + i.i, this));
return 1;
} else {
return 0;
}
}
template<class Parms>
void VectorHashTable<Parms>::erase(const iterator & p) {
vector_iterator pos = p.vector_iterator();
parms_.make_nonexistent(*pos);
vector_iterator vector_end = vector_.end();
vector_iterator e = pos;
do {
++e;
if (e == vector_end) e = vector_.begin();
} while (!parms_.is_nonexistent(*e));
vector_iterator should_be;
while (pos != e) {
if (parms_.is_nonexistent(*pos)) {
should_be = find_item_v(*pos);
if (parms_.is_nonexistent(*should_be)) {
*should_be = *pos;
parms_.make_nonexistent(*pos);
}
}
++pos;
if (pos == vector_end) pos = vector_.begin();
}
--size_;
}
#endif
template<class Parms>
void VectorHashTable<Parms>::swap(VectorHashTable<Parms> &other) {
vector_.swap(other.vector_);
size_type temp = size_;
size_ = other.size_;
other.size_ = temp;
}
template<class Parms>
VectorHashTable<Parms>::VectorHashTable(size_type i, const Parms & p)
: parms_(p), size_(0)
{
if (i <= 19) {
i = 19;
} else {
size_type j = ((i - 3)/4)*4 + 3;
if (j == i)
i = j;
else
i = j + 4;
Primes p(static_cast<size_type>(sqrt(static_cast<double>(i))+2));
for (;;) {
if (i > p.max_num())
p.resize(static_cast<size_type>(sqrt(static_cast<double>(i))+2));
if (p.is_prime(i) && p.is_prime(i-2))
break;
i += 4;
}
}
vector_.resize(i);
nonexistent_vector();
}
template<class Parms>
void VectorHashTable<Parms>::resize(size_type i) {
VectorHashTable temp(i,parms_);
iterator e = end();
for (iterator i = begin(); i != e; ++i)
temp.insert(*i);
swap(temp);
}
template<class Parms>
void VectorHashTable<Parms>::recalc_size() {
size_ = 0;
for (iterator i = begin(), e = end(); i != e; ++i, ++size_);
}
}
#endif
|