Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Naplavkov committed May 21, 2023
1 parent e9d6fcb commit 80aad13
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
6 changes: 3 additions & 3 deletions detail/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ class emplace_iterator {
}
};

template <class T, size_t N>
template <class T, std::size_t N>
class ring_table {
std::array<std::vector<T>, N> rows_;

public:
explicit ring_table(size_t cols)
explicit ring_table(std::size_t cols)
{
for (auto& row : rows_)
row.resize(cols);
}

auto& operator[](size_t row) { return rows_[row % N]; }
auto& operator[](std::size_t row) { return rows_[row % N]; }
};

} // namespace step20
Expand Down
25 changes: 16 additions & 9 deletions least_frequently_used.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ class cache {
};

struct freq_type {
size_t n;
std::size_t n;
item_list items;
};

freq_list list_;
std::unordered_map<Key, item_iterator, Hash, KeyEqual> map_;
size_t capacity_;
std::size_t capacity_;

freq_iterator emplace(freq_iterator it, size_t n)
bool equal(freq_iterator it, std::size_t n) const
{
return it != list_.end() && it->n == n ? it : list_.emplace(it, n);
return it != list_.end() && it->n == n;
}

public:
explicit cache(size_t capacity) : capacity_(capacity) {}
explicit cache(std::size_t capacity) : capacity_(capacity) {}

/// @return nullptr if key is not found
const T* find(const Key& key)
Expand All @@ -52,7 +52,14 @@ class cache {
return nullptr;
auto item = it->second;
auto freq = item->parent;
auto next = emplace(std::next(freq), freq->n + 1);
auto next = std::next(freq);
if (!equal(next, freq->n + 1)) {
if (freq->items.size() == 1) {
++freq->n;
return std::addressof(item->val);
}
next = list_.emplace(next, freq->n + 1);
}
try {
next->items.splice(next->items.end(), freq->items, item);
}
Expand Down Expand Up @@ -83,14 +90,14 @@ class cache {
if (freq->items.empty())
list_.erase(freq);
}
auto freq = emplace(list_.begin(), 1);
bool was_freq_empty = freq->items.empty();
auto exists = equal(list_.begin(), 1);
auto freq = exists ? list_.begin() : list_.emplace(list_.begin(), 1);
try {
auto item = freq->items.emplace(freq->items.end(), freq, key, val);
map_.emplace(key, item);
}
catch (...) {
if (was_freq_empty)
if (!exists)
list_.erase(freq);
throw;
}
Expand Down
10 changes: 6 additions & 4 deletions suffix_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace step20 {
/// @param Size - to specify the maximum number / offset of characters;
/// @param Compare - to determine the order of characters.
template <class Char,
std::unsigned_integral Size = size_t,
std::unsigned_integral Size = std::size_t,
class Compare = std::less<>>
class suffix_array {
public:
Expand Down Expand Up @@ -111,13 +111,13 @@ class suffix_array {

template <std::ranges::input_range R, class Compare = std::less<>>
suffix_array(R, Compare = {})
-> suffix_array<std::ranges::range_value_t<R>, size_t, Compare>;
-> suffix_array<std::ranges::range_value_t<R>, std::size_t, Compare>;

/// Kasai's algorithm for constructing longest common prefix array.

/// Time and space complexity O(N), where: N - text length.
template <class Char,
std::unsigned_integral Size = size_t,
std::unsigned_integral Size = std::size_t,
class Compare = std::less<>>
class enhanced_suffix_array : public suffix_array<Char, Size, Compare> {
std::vector<Size> lcp_;
Expand Down Expand Up @@ -158,7 +158,9 @@ class enhanced_suffix_array : public suffix_array<Char, Size, Compare> {

template <std::ranges::input_range R, class Compare = std::less<>>
enhanced_suffix_array(R, Compare = {})
-> enhanced_suffix_array<std::ranges::range_value_t<R>, size_t, Compare>;
-> enhanced_suffix_array<std::ranges::range_value_t<R>,
std::size_t,
Compare>;

} // namespace step20

Expand Down
2 changes: 1 addition & 1 deletion suffix_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace step20 {
/// @param Size - to specify the maximum number / offset of characters;
/// @param Map - to associate characters with nodes.
template <class Char,
std::unsigned_integral Size = size_t,
std::unsigned_integral Size = std::size_t,
class Map = std::unordered_map<Char, Size>>
class suffix_tree {
public:
Expand Down
4 changes: 2 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void check(bool cond,
std::terminate();
}

std::string generate_random_string(size_t len)
std::string generate_random_string(std::size_t len)
{
static auto gen = std::mt19937{std::random_device{}()};
static auto dist = std::uniform_int_distribution<int>{'a', 'z'};
Expand Down Expand Up @@ -437,7 +437,7 @@ void test_substring_search()
struct {
std::string_view str;
std::string_view substr;
std::initializer_list<size_t> expect;
std::initializer_list<std::size_t> expect;
} tests[] = {
{"", "", {0}},
{"abc$", "", {0, 1, 2, 3, 4}},
Expand Down

0 comments on commit 80aad13

Please sign in to comment.