std::vector<int> hashtable;
std::vector<entry_t> entries;
- int free_list, counter;
+ int free_list, counter, begin_n;
OPS ops;
void init()
{
free_list = -1;
counter = 0;
+ begin_n = -1;
}
void init_from(const dict<K, T, OPS> &other)
void rehash()
{
free_list = -1;
+ begin_n = -1;
for (auto &h : hashtable)
h = -1;
int hash = mkhash(entries[i].udata.first);
entries[i].set_next_used(hashtable[hash]);
hashtable[hash] = i;
+ begin_n = i;
}
}
entries[index].set_next_free(free_list);
free_list = index;
if (--counter == 0)
- init();
+ clear();
+ else if (index == begin_n)
+ do begin_n--; while (begin_n >= 0 && entries[begin_n].is_free());
return;
}
last_index = index;
entries[i].udata = value;
entries[i].set_next_used(hashtable[hash]);
hashtable[hash] = i;
+ if (begin_n < i)
+ begin_n = i;
counter++;
return i;
}
public:
iterator() { }
iterator(dict<K, T, OPS> *ptr, int index) : ptr(ptr), index(index) { }
- iterator operator++() { do index++; while (index != int(ptr->entries.size()) && ptr->entries[index].is_free()); return *this; }
- iterator operator--() { do index--; while (index != 0 && ptr->entries[index].is_free()); return *this; }
+ iterator operator++() { do index--; while (index >= 0 && ptr->entries[index].is_free()); return *this; }
bool operator==(const iterator &other) const { return index == other.index; }
bool operator!=(const iterator &other) const { return index != other.index; }
std::pair<K, T> &operator*() { return ptr->entries[index].udata; }
public:
const_iterator() { }
const_iterator(const dict<K, T, OPS> *ptr, int index) : ptr(ptr), index(index) { }
- const_iterator operator++() { do index++; while (index != int(ptr->entries.size()) && ptr->entries[index].is_free()); return *this; }
- const_iterator operator--() { do index--; while (index != 0 && ptr->entries[index].is_free()); return *this; }
+ const_iterator operator++() { do index--; while (index >= 0 && ptr->entries[index].is_free()); return *this; }
bool operator==(const const_iterator &other) const { return index == other.index; }
bool operator!=(const const_iterator &other) const { return index != other.index; }
const std::pair<K, T> &operator*() const { return ptr->entries[index].udata; }
}
dict<K, T, OPS> &operator=(const dict<K, T, OPS> &other) {
- clear();
- init_from(other);
+ if (this != &other)
+ init_from(other);
return *this;
}
entries.swap(other.entries);
std::swap(free_list, other.free_list);
std::swap(counter, other.counter);
+ std::swap(begin_n, other.begin_n);
}
bool operator==(const dict<K, T, OPS> &other) const {
bool empty() const { return counter == 0; }
void clear() { hashtable.clear(); entries.clear(); init(); }
- iterator begin() { int index = 0; while (index != int(entries.size()) && entries[index].is_free()) index++; return iterator(this, index); }
- iterator end() { return iterator(this, entries.size()); }
+ iterator begin() { return iterator(this, begin_n); }
+ iterator end() { return iterator(this, -1); }
- const_iterator begin() const { int index = 0; while (index != int(entries.size()) && entries[index].is_free()) index++; return const_iterator(this, index); }
- const_iterator end() const { return const_iterator(this, entries.size()); }
+ const_iterator begin() const { return const_iterator(this, begin_n); }
+ const_iterator end() const { return const_iterator(this, -1); }
};
template<typename K, typename OPS = hash_ops<K>>
std::vector<int> hashtable;
std::vector<entry_t> entries;
- int free_list, counter;
+ int free_list, counter, begin_n;
OPS ops;
void init()
{
free_list = -1;
counter = 0;
+ begin_n = -1;
}
void init_from(const pool<K, OPS> &other)
void rehash()
{
free_list = -1;
+ begin_n = -1;
for (auto &h : hashtable)
h = -1;
int hash = mkhash(entries[i].key);
entries[i].set_next_used(hashtable[hash]);
hashtable[hash] = i;
+ begin_n = i;
}
}
entries[index].set_next_free(free_list);
free_list = index;
if (--counter == 0)
- init();
+ clear();
+ else if (index == begin_n)
+ do begin_n--; while (begin_n >= 0 && entries[begin_n].is_free());
return;
}
last_index = index;
entries[i].key = key;
entries[i].set_next_used(hashtable[hash]);
hashtable[hash] = i;
+ if (begin_n < i)
+ begin_n = i;
counter++;
return i;
}
public:
iterator() { }
iterator(pool<K, OPS> *ptr, int index) : ptr(ptr), index(index) { }
- iterator operator++() { do index++; while (index != int(ptr->entries.size()) && ptr->entries[index].is_free()); return *this; }
- iterator operator--() { do index--; while (index != 0 && ptr->entries[index].is_free()); return *this; }
+ iterator operator++() { do index--; while (index >= 0 && ptr->entries[index].is_free()); return *this; }
bool operator==(const iterator &other) const { return index == other.index; }
bool operator!=(const iterator &other) const { return index != other.index; }
K &operator*() { return ptr->entries[index].key; }
public:
const_iterator() { }
const_iterator(const pool<K, OPS> *ptr, int index) : ptr(ptr), index(index) { }
- const_iterator operator++() { do index++; while (index != int(ptr->entries.size()) && ptr->entries[index].is_free()); return *this; }
- const_iterator operator--() { do index--; while (index != 0 && ptr->entries[index].is_free()); return *this; }
+ const_iterator operator++() { do index--; while (index >= 0 && ptr->entries[index].is_free()); return *this; }
bool operator==(const const_iterator &other) const { return index == other.index; }
bool operator!=(const const_iterator &other) const { return index != other.index; }
const K &operator*() const { return ptr->entries[index].key; }
}
pool<K, OPS> &operator=(const pool<K, OPS> &other) {
- clear();
- init_from(other);
+ if (this != &other)
+ init_from(other);
return *this;
}
entries.swap(other.entries);
std::swap(free_list, other.free_list);
std::swap(counter, other.counter);
+ std::swap(begin_n, other.begin_n);
}
bool operator==(const pool<K, OPS> &other) const {
bool empty() const { return counter == 0; }
void clear() { hashtable.clear(); entries.clear(); init(); }
- iterator begin() { int index = 0; while (index != int(entries.size()) && entries[index].is_free()) index++; return iterator(this, index); }
- iterator end() { return iterator(this, entries.size()); }
+ iterator begin() { return iterator(this, begin_n); }
+ iterator end() { return iterator(this, -1); }
- const_iterator begin() const { int index = 0; while (index != int(entries.size()) && entries[index].is_free()) index++; return const_iterator(this, index); }
- const_iterator end() const { return const_iterator(this, entries.size()); }
+ const_iterator begin() const { return const_iterator(this, begin_n); }
+ const_iterator end() const { return const_iterator(this, -1); }
};
} /* namespace hashlib */