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

vector_set

parent 5cbd8e5a
......@@ -29,6 +29,8 @@ namespace SUBool
InputIterator it, InputIterator begin, InputIterator end);
template <class E> void sort_unique(std::vector<E> &vec);
template <class E, class Compare>
void sort_unique(std::vector<E> &vec, Compare comp);
template <class E>
std::vector<E> vector_subtract(
const std::vector<E> &a, const std::vector<E> &b);
......@@ -152,6 +154,15 @@ SUBool::sort_unique(std::vector<E> &vec)
vec.erase(end, vec.end());
}
template <class E, class Compare>
void
SUBool::sort_unique(std::vector<E> &vec, Compare comp)
{
std::sort(vec.begin(), vec.end(), comp);
auto end = std::unique(vec.begin(), vec.end());
vec.erase(end, vec.end());
}
template <class E>
std::vector<E>
SUBool::vector_subtract(const std::vector<E> &a, const std::vector<E> &b)
......
......@@ -9,9 +9,10 @@
#ifndef __VECTOR_SET_H
#define __VECTOR_SET_H
#include "util.h"
#include <vector>
namespace SUBol
namespace SUBool
{
// API based on std library, so that it can be used in templates wherever
......@@ -34,12 +35,16 @@ namespace SUBol
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer =
typename std::allocator_traits<Allocator>::const_pointer;
using iterator = typename vector_type::iterator;
using iterator = typename vector_type::const_iterator;
using const_iterator = typename vector_type::const_iterator;
using reverse_iterator = typename vector_type::reverse_iterator;
using reverse_iterator = typename vector_type::const_reverse_iterator;
using const_reverse_iterator =
typename vector_type::const_reverse_iterator;
private:
vector_type mElements;
Compare mCompare;
public:
VectorSet();
explicit VectorSet(
......@@ -49,10 +54,7 @@ namespace SUBol
VectorSet(InputIt first, InputIt last, const Compare &comp = Compare(),
const Allocator &alloc = Allocator());
template <class InputIt>
VectorSet(InputIt first, InputIt last, const Allocator &alloc)
: VectorSet(first, last, Compare(), alloc)
{
}
VectorSet(InputIt first, InputIt last, const Allocator &alloc);
VectorSet(const VectorSet &other);
VectorSet(const VectorSet &other, const Allocator &alloc);
VectorSet(VectorSet &&other);
......@@ -60,10 +62,7 @@ namespace SUBol
VectorSet(std::initializer_list<value_type> init,
const Compare &comp = Compare(),
const Allocator &alloc = Allocator());
VectorSet(std::initializer_list<value_type> init, const Allocator &alloc)
: VectorSet(init, Compare(), alloc)
{
}
VectorSet(std::initializer_list<value_type> init, const Allocator &alloc);
~VectorSet() = default;
......@@ -86,6 +85,9 @@ namespace SUBol
bool empty() const noexcept;
size_type size() const noexcept;
size_type max_size() const noexcept;
void reserve(size_type new_cap);
size_type capacity() const noexcept;
void shrink_to_fit();
void clear() noexcept;
......@@ -102,7 +104,7 @@ namespace SUBol
iterator emplace_hint(const_iterator hint, Args &&...args);
iterator erase(iterator pos);
iterator erase(const_iterator pos);
// iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
size_type erase(const Key &key);
......@@ -142,6 +144,182 @@ namespace SUBol
key_compare key_comp() const;
value_compare value_comp() const;
};
} // namespace SUBol
template <class Key, class Compare, class Alloc>
bool operator==(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
template <class Key, class Compare, class Alloc>
bool operator!=(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
template <class Key, class Compare, class Alloc>
bool operator<(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
template <class Key, class Compare, class Alloc>
bool operator<=(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
template <class Key, class Compare, class Alloc>
bool operator>(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
template <class Key, class Compare, class Alloc>
bool operator>=(const VectorSet<Key, Compare, Alloc> &lhs,
const VectorSet<Key, Compare, Alloc> &rhs);
// noexcept(/* see below */); C++17, if it makes sense
template <class Key, class Compare, class Alloc>
void swap(VectorSet<Key, Compare, Alloc> &lhs,
VectorSet<Key, Compare, Alloc> &rhs);
} // namespace SUBool
// Implementation
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet()
: mElements(), mCompare()
{
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
const Compare &comp, const Allocator &alloc)
: mElements(alloc), mCompare(comp)
{
}
template <class Key, class Compare, class Allocator>
template <class InputIt>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
InputIt first, InputIt last, const Compare &comp, const Allocator &alloc)
: mElements(first, last, alloc), mCompare(comp)
{
sort_unique(mElements, mCompare);
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(const VectorSet &other)
: mElements(other.mElements), mCompare(other.mCompare)
{
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
const VectorSet &other, const Allocator &alloc)
: mElements(other.mElements, alloc), mCompare(other.mCompare)
{
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(VectorSet &&other)
: mElements(std::move(other.mElements)), mCompare(std::move(other.mCompare))
{
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
VectorSet &&other, const Allocator &alloc)
: mElements(std::move(other.mElements), alloc),
mCompare(std::move(other.mCompare))
{
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
std::initializer_list<value_type> init, const Compare &comp,
const Allocator &alloc)
: mElements(init, alloc), mCompare(comp)
{
sort_unique(mElements, mCompare);
}
template <class Key, class Compare, class Allocator>
SUBool::VectorSet<Key, Compare, Allocator>::VectorSet(
std::initializer_list<value_type> init, const Allocator &alloc)
: VectorSet(init, Compare(), alloc)
{
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::begin() noexcept
{
return mElements.begin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_iterator
SUBool::VectorSet<Key, Compare, Allocator>::begin() const noexcept
{
return mElements.begin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_iterator
SUBool::VectorSet<Key, Compare, Allocator>::cbegin() const noexcept
{
return mElements.cbegin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::iterator
SUBool::VectorSet<Key, Compare, Allocator>::end() noexcept
{
return mElements.end();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_iterator
SUBool::VectorSet<Key, Compare, Allocator>::end() const noexcept
{
return mElements.end();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_iterator
SUBool::VectorSet<Key, Compare, Allocator>::cend() const noexcept
{
return mElements.cend();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::rbegin() noexcept
{
return mElements.rbegin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::rbegin() const noexcept
{
return mElements.rbegin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::crbegin() const noexcept
{
return mElements.crbegin();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::rend() noexcept
{
return mElements.rend();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::rend() const noexcept
{
return mElements.rend();
}
template <class Key, class Compare, class Allocator>
typename SUBool::VectorSet<Key, Compare, Allocator>::const_reverse_iterator
SUBool::VectorSet<Key, Compare, Allocator>::crend() const noexcept
{
return mElements.crend();
}
#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