Skip to content

Commit

Permalink
Move std::map compatible operations inside pointer_set to flat_map
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Nov 19, 2023
1 parent 0ea62bb commit 83a53d9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 41 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.88_amd64.deb
sudo apt install build/meevax_0.5.89_amd64.deb
```

or
Expand Down Expand Up @@ -123,9 +123,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.88.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.89.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.88_amd64.deb`
| `package` | Generate debian package `meevax_0.5.89_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.88
0.5.89
84 changes: 47 additions & 37 deletions include/meevax/memory/pointer_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,9 @@ inline namespace memory

struct chunk : public Bitset<N>
{
std::size_t offset;

explicit chunk(compact_pointer const p) noexcept
: offset { p.offset() }
explicit chunk(std::size_t const& index) noexcept
{
Bitset<N>::set(p.index());
Bitset<N>::set(index);
}

auto begin() const
Expand All @@ -93,7 +90,30 @@ inline namespace memory
}
};

std::vector<chunk> chunks;
template <typename Key, typename Value>
struct flat_map : public std::vector<std::pair<Key, Value>>
{
auto lower_bound(Key const& key) -> decltype(auto)
{
auto compare = [](auto && pair, auto && key) constexpr
{
return pair.first < key;
};

return std::lower_bound(this->begin(), this->end(), key, compare);
}

template <typename... Ts>
auto emplace_hint(Ts&&... xs) -> decltype(auto)
{
return this->emplace(std::forward<decltype(xs)>(xs)...);
}
};

template <typename... Ts>
using map = flat_map<Ts...>;

map<std::size_t, chunk> chunks;

public:
struct iterator
Expand All @@ -108,26 +128,26 @@ inline namespace memory

using difference_type = std::ptrdiff_t;

std::vector<chunk> const& chunks;
map<std::size_t, chunk> const& chunks;

typename std::vector<chunk>::const_iterator outer;
typename map<std::size_t, chunk>::const_iterator outer;

std::optional<naive_index_iterator<chunk>> inner;

explicit iterator(std::vector<chunk> const& chunks,
typename std::vector<chunk>::const_iterator iter,
explicit iterator(map<std::size_t, chunk> const& chunks,
typename map<std::size_t, chunk>::const_iterator outer,
std::size_t hint) noexcept
: chunks { chunks }
, outer { iter }
, inner { outer != chunks.end() ? std::make_optional(naive_index_iterator(*outer, hint)) : std::nullopt }
, outer { outer }
, inner { outer != chunks.end() ? std::make_optional(naive_index_iterator(outer->second, hint)) : std::nullopt }
{
if (not dereferenceable() and incrementable())
{
operator ++();
}
}

explicit iterator(std::vector<chunk> const& chunks) noexcept
explicit iterator(map<std::size_t, chunk> const& chunks) noexcept
: chunks { chunks }
, outer { chunks.end() }
, inner { std::nullopt }
Expand All @@ -137,12 +157,12 @@ inline namespace memory

auto incrementable() const -> bool
{
return outer != chunks.end() and inner and inner != outer->end();
return outer != chunks.end() and inner and inner != outer->second.end();
}

auto decrementable() const -> bool
{
return outer != chunks.begin() or not inner or inner != outer->begin();
return outer != chunks.begin() or not inner or inner != outer->second.begin();
}

auto dereferenceable() const -> bool
Expand All @@ -153,7 +173,7 @@ inline namespace memory
auto operator *() const noexcept
{
assert(dereferenceable());
return compact_pointer::to_pointer(outer->offset + inner->index);
return compact_pointer::to_pointer(outer->first + inner->index);
}

auto operator ++() noexcept -> auto &
Expand All @@ -164,9 +184,9 @@ inline namespace memory
*/
assert(incrementable());

for (++*inner; outer != chunks.end(); inner = (++outer)->begin())
for (++*inner; outer != chunks.end(); inner = (++outer)->second.begin())
{
for (; inner != outer->end(); ++*inner)
for (; inner != outer->second.end(); ++*inner)
{
if (**inner)
{
Expand Down Expand Up @@ -195,9 +215,9 @@ inline namespace memory
{
assert(decrementable());

if (outer == chunks.end() or inner == outer->begin())
if (outer == chunks.end() or inner == outer->second.begin())
{
inner = std::prev((--outer)->end());
inner = std::prev((--outer)->second.end());
}
else
{
Expand Down Expand Up @@ -238,39 +258,29 @@ inline namespace memory
assert(begin() == end());
}

auto lower_bound_chunk(compact_pointer p) noexcept
{
auto compare = [](auto && chunk, auto && offset) constexpr
{
return chunk.offset < offset;
};

return std::lower_bound(chunks.begin(), chunks.end(), p.offset(), compare);
}

auto size() const noexcept
{
return std::distance(begin(), end());
}

auto insert(compact_pointer p) noexcept
{
if (auto iter = lower_bound_chunk(p); iter != chunks.end() and iter->offset == p.offset())
if (auto iter = chunks.lower_bound(p.offset()); iter != chunks.end() and iter->first == p.offset())
{
iter->set(p.index());
iter->second.set(p.index());
}
else
{
assert(iter == chunks.end() or p.offset() < iter->offset);
chunks.emplace(iter, p);
assert(iter == chunks.end() or p.offset() < iter->first);
chunks.emplace_hint(iter, p.offset(), p.index());
}
}

auto erase(compact_pointer p) noexcept
{
auto iter = lower_bound_chunk(p);
auto iter = chunks.lower_bound(p.offset());
assert(iter != chunks.end());
iter->reset(p.index());
iter->second.reset(p.index());
}

auto begin() const noexcept
Expand All @@ -285,7 +295,7 @@ inline namespace memory

auto lower_bound(compact_pointer p) noexcept
{
if (auto iter = lower_bound_chunk(p); iter != chunks.end())
if (auto iter = chunks.lower_bound(p.offset()); iter != chunks.end())
{
return iterator(chunks, iter, p.index());
}
Expand Down

0 comments on commit 83a53d9

Please sign in to comment.