Coverage Report

Created: 2025-07-18 06:42

/src/aspell/modules/speller/default/vector_hash-t.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2000
2
// Kevin Atkinson
3
//
4
// Permission to use, copy, modify, distribute and sell this software
5
// and its documentation for any purpose is hereby granted without
6
// fee, provided that the above copyright notice appear in all copies
7
// and that both that copyright notice and this permission notice
8
// appear in supporting documentation. Kevin Atkinson makes no
9
// representations about the suitability of this software for any
10
// purpose.  It is provided "as is" without express or implied
11
// warranty.
12
13
#ifndef __autil_hash_t_hh__
14
#define __autil_hash_t_hh__
15
16
#include <math.h>
17
18
#include "vector_hash.hpp"
19
#include "primes.hpp"
20
21
namespace aspeller {
22
23
  template <class Parms>
24
  VectorHashTable<Parms>::FindIterator
25
  ::FindIterator(const HashTable * ht, const key_type & k)
26
16.3M
    : vector(&ht->vector())
27
16.3M
    , parms(&ht->parms())
28
16.3M
    , key(k)
29
16.3M
    , i(ht->hash1(k))
30
16.3M
    , hash2(ht->hash2(k))
31
16.3M
  {
32
16.3M
    if (!parms->is_nonexistent((*vector)[i])
33
16.3M
  && !parms->equal(parms->key((*vector)[i]), key))
34
12.8M
      adv();
35
16.3M
  }
readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::ReadOnlyDict::WordLookupParms>::FindIterator::FindIterator(aspeller::VectorHashTable<(anonymous namespace)::ReadOnlyDict::WordLookupParms> const*, char const* const&)
Line
Count
Source
26
16.3M
    : vector(&ht->vector())
27
16.3M
    , parms(&ht->parms())
28
16.3M
    , key(k)
29
16.3M
    , i(ht->hash1(k))
30
16.3M
    , hash2(ht->hash2(k))
31
16.3M
  {
32
16.3M
    if (!parms->is_nonexistent((*vector)[i])
33
16.3M
  && !parms->equal(parms->key((*vector)[i]), key))
34
12.8M
      adv();
35
16.3M
  }
Unexecuted instantiation: readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::WordLookupParms>::FindIterator::FindIterator(aspeller::VectorHashTable<(anonymous namespace)::WordLookupParms> const*, char const* const&)
36
37
  template <class Parms>
38
12.8M
  void VectorHashTable<Parms>::FindIterator::adv() {
39
64.5M
    do {
40
64.5M
      i = (i + hash2) % vector->size();
41
64.5M
    } while (!parms->is_nonexistent((*vector)[i])
42
64.5M
       && !parms->equal(parms->key((*vector)[i]), key));
43
12.8M
  }
readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::ReadOnlyDict::WordLookupParms>::FindIterator::adv()
Line
Count
Source
38
12.8M
  void VectorHashTable<Parms>::FindIterator::adv() {
39
64.5M
    do {
40
64.5M
      i = (i + hash2) % vector->size();
41
64.5M
    } while (!parms->is_nonexistent((*vector)[i])
42
64.5M
       && !parms->equal(parms->key((*vector)[i]), key));
43
12.8M
  }
Unexecuted instantiation: readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::WordLookupParms>::FindIterator::adv()
44
45
  template<class Parms>
46
2.02k
  void VectorHashTable<Parms>::nonexistent_vector() {
47
2.02k
    vector_iterator vector_end = vector_.end();
48
2.02k
    for (vector_iterator i = vector_.begin(); i != vector_end; ++i) {
49
0
      parms_.make_nonexistent(*i);
50
0
    }
51
2.02k
  }
readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::ReadOnlyDict::WordLookupParms>::nonexistent_vector()
Line
Count
Source
46
2.02k
  void VectorHashTable<Parms>::nonexistent_vector() {
47
2.02k
    vector_iterator vector_end = vector_.end();
48
2.02k
    for (vector_iterator i = vector_.begin(); i != vector_end; ++i) {
49
0
      parms_.make_nonexistent(*i);
50
0
    }
51
2.02k
  }
Unexecuted instantiation: readonly_ws.cpp:aspeller::VectorHashTable<(anonymous namespace)::WordLookupParms>::nonexistent_vector()
52
  
53
  template<class Parms>
54
  std::pair<typename VectorHashTable<Parms>::iterator, bool> 
55
  VectorHashTable<Parms>::insert(const value_type & d) 
56
0
  {
57
0
    MutableFindIterator j(this, parms_.key(d));
58
0
    vector_iterator i = vector_.begin() + j.i;
59
0
    if (!parms_.is_multi && !j.at_end())
60
0
      return std::pair<iterator,bool>(iterator(i,this),false);
61
0
    if (load_factor() > .92) {
62
0
      resize(bucket_count()*2);
63
0
      return insert(d);
64
0
    } 
65
0
    if (parms_.is_multi) 
66
0
      while(!j.at_end()) j.adv();
67
0
    *(vector_.begin() + j.i) = d;
68
0
    ++size_;
69
0
    return std::pair<iterator,bool>(iterator(i,this),true);
70
0
  }
71
72
  template<class Parms>
73
  bool VectorHashTable<Parms>::have(const key_type & key) const 
74
  {
75
    return !ConstFindIterator(this,key).at_end();
76
  }
77
78
  template<class Parms>
79
  typename VectorHashTable<Parms>::iterator 
80
  VectorHashTable<Parms>::find(const key_type & key) 
81
  {
82
    MutableFindIterator i(this, key);
83
    if (!i.at_end()) 
84
      return iterator(vector_.begin() + i.i, this);
85
    else
86
      return end();
87
  }
88
89
  template<class Parms>
90
  typename VectorHashTable<Parms>::const_iterator 
91
16.3M
  VectorHashTable<Parms>::find(const key_type & key) const {
92
16.3M
    ConstFindIterator i(this, key);
93
16.3M
    if (!i.at_end()) 
94
438k
      return const_iterator(vector_.begin() + i.i, this);
95
15.9M
    else
96
15.9M
      return end();
97
16.3M
  }
98
99
#if 0 // it currently doesn't work needs fixing
100
101
  template<class Parms>
102
  VectorHashTable<Parms>::size_type 
103
  VectorHashTable<Parms>::erase(const key_type & key) {
104
    MutableFindIterator i(this, key);
105
    if (!i.at_end()) {
106
      erase(iterator(vector_.begin() + i.i, this));
107
      return 1;
108
    } else {
109
      return 0;
110
    }
111
  }
112
113
  template<class Parms>
114
  void VectorHashTable<Parms>::erase(const iterator & p) {
115
    vector_iterator pos = p.vector_iterator();
116
    parms_.make_nonexistent(*pos);
117
    vector_iterator vector_end = vector_.end();
118
119
    vector_iterator e = pos;
120
    do {
121
      ++e;
122
      if (e == vector_end) e = vector_.begin();
123
    } while (!parms_.is_nonexistent(*e));
124
125
    vector_iterator should_be;
126
    while (pos != e) {
127
      if (parms_.is_nonexistent(*pos)) {
128
  should_be = find_item_v(*pos);
129
  if (parms_.is_nonexistent(*should_be)) {
130
    *should_be = *pos;
131
    parms_.make_nonexistent(*pos);
132
  }
133
      }
134
      ++pos;
135
      if (pos == vector_end) pos = vector_.begin();
136
    }
137
    --size_;
138
  }
139
#endif
140
141
  template<class Parms>
142
0
  void VectorHashTable<Parms>::swap(VectorHashTable<Parms> &other) {
143
0
    vector_.swap(other.vector_);
144
0
    size_type temp = size_;
145
0
    size_ = other.size_;
146
0
    other.size_ = temp;
147
0
  }
148
149
  template<class Parms>
150
  VectorHashTable<Parms>::VectorHashTable(size_type i, const Parms & p)
151
0
    : parms_(p), size_(0) 
152
0
  {
153
0
    if (i <= 19) {
154
0
      i = 19;
155
0
    } else {
156
0
      size_type j =  ((i - 3)/4)*4 + 3;
157
0
      if (j == i) 
158
0
  i = j;
159
0
      else
160
0
  i = j + 4;
161
0
      Primes p(static_cast<size_type>(sqrt(static_cast<double>(i))+2));
162
0
      for (;;) {
163
0
  if (i > p.max_num())
164
0
    p.resize(static_cast<size_type>(sqrt(static_cast<double>(i))+2));
165
0
  if (p.is_prime(i) && p.is_prime(i-2))
166
0
    break;
167
0
  i += 4;
168
0
      }
169
0
    }
170
0
    vector_.resize(i);
171
0
    nonexistent_vector();
172
0
  }
173
174
  template<class Parms>
175
0
  void VectorHashTable<Parms>::resize(size_type i) {
176
0
    VectorHashTable temp(i,parms_);
177
0
    iterator e = end();
178
0
    for (iterator i = begin(); i != e; ++i)
179
0
      temp.insert(*i);
180
0
    swap(temp);
181
0
  }
182
183
  template<class Parms>
184
  void VectorHashTable<Parms>::recalc_size() {
185
    size_ = 0;
186
    for (iterator i = begin(), e = end(); i != e; ++i, ++size_);
187
  }
188
189
}
190
191
#endif