Commit 26ff4b50 authored by Kučera Petr RNDr. Ph.D.'s avatar Kučera Petr RNDr. Ph.D.
Browse files

vector_set implementation

parent 60e372ea
......@@ -98,8 +98,10 @@ namespace SUBool
template <class InputIt> void insert(InputIt first, InputIt last);
void insert(std::initializer_list<value_type> ilist);
// Tries first to add at the end, then it bubbles to the right position
template <class... Args>
std::pair<iterator, bool> emplace(Args &&...args);
// Hint is only checked for presence
template <class... Args>
iterator emplace_hint(const_iterator hint, Args &&...args);
......@@ -120,6 +122,8 @@ namespace SUBool
const_iterator find(const Key &key) const;
template <class K> iterator find(const K &x);
template <class K> const_iterator find(const K &x) const;
iterator find_hint(const_iterator hint, const Key &key);
const_iterator find_hint(const_iterator hint, const Key &key) const;
bool contains(const Key &key) const;
template <class K> bool contains(const K &x) const;
......@@ -143,6 +147,10 @@ namespace SUBool
key_compare key_comp() const;
value_compare value_comp() const;
private:
void merge_tail(iterator middle);
iterator bubble_last();
};
template <class Key, class Compare, class Alloc>
......@@ -322,4 +330,199 @@ SUBool::VectorSet<Key, Compare, Allocator>::crend() const noexcept
return mElements.crend();
}
template <class Key, class Compare, class Allocator>
bool
SUBool::VectorSet<Key, Compare, Allocator>::empty() const noexcept
{
return mElements.empty();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::size_type
SUBool::VectorSet<Key, Compare, Allocator>::size() const noexcept
{
return mElements.size();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::size_type
SUBool::VectorSet<Key, Compare, Allocator>::max_size() const noexcept
{
return mElements.max_size();
}
template <class Key, class Compare, class Allocator>
void
SUBool::VectorSet<Key, Compare, Allocator>::reserve(size_type new_cap)
{
mElements.reserve(new_cap);
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::size_type
SUBool::VectorSet<Key, Compare, Allocator>::capacity() const noexcept
{
return mElements.capacity();
}
template <class Key, class Compare, class Allocator>
void
SUBool::VectorSet<Key, Compare, Allocator>::shrink_to_fit()
{
mElements.shrink_to_fit();
}
template <class Key, class Compare, class Allocator>
void
SUBool::VectorSet<Key, Compare, Allocator>::clear() noexcept
{
mElements.clear();
}
template <class Key, class Compare, class Allocator>
std::pair<typename SUBool::VectorSet<Key, Compare, Allocator>::iterator, bool>
SUBool::VectorSet<Key, Compare, Allocator>::insert(const value_type &value)
{
auto pos = find(value);
if (pos != mElements.end())
{
return std::make_pair(pos, false);
}
mElements.push_back(value);
return std::make_pair(bubble_last(), true);
}
template <class Key, class Compare, class Allocator>
std::pair<typename SUBool::VectorSet<Key, Compare, Allocator>::iterator, bool>
SUBool::VectorSet<Key, Compare, Allocator>::insert(value_type &&value)
{
auto pos = find(value);
if (pos != mElements.end())
{
return std::make_pair(pos, false);
}
mElements.push_back(std::move(value));
return std::make_pair(bubble_last(), true);
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::insert(
const_iterator hint, const value_type &value)
{
auto pos = find_hint(hint, value);
if (pos != mElements.end())
{
return pos;
}
mElements.push_back(value);
return bubble_last();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::insert(
const_iterator hint, value_type &&value)
{
auto pos = find_hint(hint, value);
if (pos != mElements.end())
{
return pos;
}
mElements.push_back(std::move(value));
return bubble_last();
}
template <class Key, class Compare, class Allocator>
void
SUBool::VectorSet<Key, Compare, Allocator>::merge_tail(iterator middle)
{
if (middle == mElements.end())
{
return;
}
if (!std::is_sorted(middle, mElements.end(), mCompare))
{
std::sort(middle, mElements.end(), mCompare);
}
std::inplace_merge(mElements.begin(), middle, mElements.end());
auto end = std::unique(mElements.begin(), mElements.end());
mElements.erase(end, mElements.end());
}
template <class Key, class Compare, class Allocator>
template <class InputIt>
void
SUBool::VectorSet<Key, Compare, Allocator>::insert(InputIt first, InputIt last)
{
auto middle = mElements.insert(mElements.end(), first, last);
merge_tail(middle);
}
template <class Key, class Compare, class Allocator>
void
SUBool::VectorSet<Key, Compare, Allocator>::insert(
std::initializer_list<value_type> ilist)
{
insert(ilist.begin(), ilist.end());
}
template <class Key, class Compare, class Allocator>
template <class... Args>
std::pair<typename SUBool::VectorSet<Key, Compare, Allocator>::iterator, bool>
SUBool::VectorSet<Key, Compare, Allocator>::emplace(Args &&...args)
{
mElements.emplace_back(std::forward<Args>(args)...);
if (mElements.size() == 1)
{
return std::make_pair(mElements.begin(), true);
}
auto pos = std::lower_bound(mElements.begin(), std::prev(mElements.end()));
if (pos != mElements.end())
{
mElements.pop_back();
return std::make_pair(pos, false);
}
return std::make_pair(bubble_last(), true);
}
template <class Key, class Compare, class Allocator>
template <class... Args>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::emplace_hint(
const_iterator hint [[maybe_unused]], Args &&...args)
{
// check if the element is at the hint?
// not possible the hint is invalidated after the emplace_back.
return emplace(std::forward<Args>(args)...).first;
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::erase(iterator pos)
{
mElements.erase(pos);
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::erase(
const_iterator first, const_iterator last)
{
mElements.erase(first, last);
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::size_type
SUBool::VectorSet<Key, Compare, Allocator>::erase(const Key &key)
{
auto pos = find(key);
if (pos == mElements.end())
{
return 0;
}
mElements.erase(pos);
return 1;
}
#endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment