--- a/sources/list.h Wed Jan 27 13:08:51 2021 +0200 +++ b/sources/list.h Wed Jan 27 13:17:11 2021 +0200 @@ -29,357 +29,19 @@ */ #pragma once +#include <vector> #include "basics.h" -#include <algorithm> -#include <deque> -#include <functional> -#include <cassert> -#include <vector> #include "range.h" BEGIN_ZFC_NAMESPACE -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -class Container -{ -public: - typedef typename C::iterator Iterator; - typedef typename C::const_iterator ConstIterator; - typedef typename C::reverse_iterator ReverseIterator; - typedef typename C::const_reverse_iterator ConstReverseIterator; - typedef Container<T, C> Self; - - Container(){} - - Container (int numvalues) : - m_container (numvalues) {} - - Container (const C& other) : - m_container (other) {} - - Container(std::initializer_list<T> initializerList) : - m_container(initializerList) {} - - T& append (const T& value) - { - m_container.push_back (value); - return m_container[m_container.size() - 1]; - } - - void append(const T* values, size_t numValues) - { - size_t i0 = size(); - resize(size() + numValues); - - for (size_t i : range(numValues)) - (*this)[i0 + i] = values[i]; - } - - Iterator begin() - { - return m_container.begin(); - } - - ConstIterator begin() const - { - return m_container.begin(); - } - - void clear() - { - m_container.clear(); - } - - bool contains (const T& a) const - { - return std::find (m_container.begin(), m_container.end(), a) != m_container.end(); - } - - ConstReverseIterator crbegin() const - { - return m_container.rbegin(); - } - - ConstReverseIterator crend() const - { - return m_container.rend(); - } - - const C& container() const - { - return m_container; - } - - Iterator end() - { - return m_container.end(); - } - - ConstIterator end() const - { - return m_container.end(); - } - - Iterator find (const T& needle) - { - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; - } - - ConstIterator find (const T& needle) const - { - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; - } - - Iterator find (bool (*func)(T const&)) - { - for (Iterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); - } - - ConstIterator find (bool (*func)(T const&)) const - { - for (ConstIterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); - } - - T& first() - { - return *begin(); - } - - const T& first() const - { - return *begin(); - } - - void insert (int pos, const T& value) - { - m_container.insert (m_container.begin() + pos, value); - } - - bool is_empty() const - { - return size() == 0; - } - - T& last() - { - return *(end() - 1); - } - - const T& last() const - { - return *(end() - 1); - } - - void merge (const Self& other) - { - int oldsize = size(); - resize (size() + other.size()); - std::copy (other.begin(), other.end(), begin() + oldsize); - } - - bool pop (T& val) - { - if (is_empty()) - return false; - - val = m_container[size() - 1]; - m_container.erase (m_container.end() - 1); - return true; - } - - T& prepend (const T& value) - { - m_container.push_front (value); - return m_container[0]; - } - - ReverseIterator rbegin() - { - return m_container.rbegin(); - } - - void remove_at (int pos) - { - assert (pos < size()); - m_container.erase (m_container.begin() + pos); - } - - void remove_duplicates() - { - sort(); - resize (std::distance (begin(), std::unique (begin(), end()))); - } - - void remove_one (const T& valueToRemove) - { - auto it = std::find (m_container.begin(), m_container.end(), valueToRemove); - - if (it != m_container.end()) - m_container.erase (it); - } - - ReverseIterator rend() - { - return m_container.rend(); - } - - void resize (int size) - { - m_container.resize (size); - } - - Self reverse() const - { - Self rev; - std::copy (rbegin(), rend(), rev.begin()); - return rev; - } - - int size() const - { - return m_container.size(); - } - - void sort() - { - std::sort (begin(), end()); - } - - Self splice(int start, int end, int step = 1) const - { - start = clamp(start, 0, size()); - end = clamp(end, 0, size()); - - if (end <= start) - { - return Self(); - } - else - { - Self result; - - for (int i : range(start, end, step)) - result << operator[] (i); - - return result; - } - } - - Self splice(Range<int>& range) const - { - return splice(range.min(), range.max(), range.step()); - } - - Self& operator<< (const T& value) - { - append (value); - return *this; - } - - Self& operator<< (const Self& vals) - { - merge (vals); - return *this; - } - - T& operator[] (int n) - { - assert (n < size()); - return m_container[n]; - } - - const T& operator[] (int n) const - { - assert (n < size()); - return m_container[n]; - } - - Self operator[] (Range<int> const& n) const - { - return splice (n); - } - - Self operator+ (const Self& other) const - { - Self out (*this); - out.merge (other); - return out; - } - -protected: - C m_container; -}; - -// ------------------------------------------------------------------------------------------------- -// -template<typename T, typename C> -Container<T, C>& operator>> (const T& value, Container<T, C>& haystack) -{ - haystack.prepend (value); - return haystack; -} - -// ------------------------------------------------------------------------------------------------- -// +template<typename T> +using Vector = std::vector<T>; template<typename T> -class Vector : public Container<T, std::vector<T> > +auto& last(T& container) { -public: - typedef Container<T, std::vector<T> > Super; - - Vector(){} - - Vector(int numvalues) : - Super(numvalues){} - - Vector (T* data, size_t length) : - Super (std::vector<T> (data, data + length)) {} - - Vector(std::initializer_list<T> initializerList) : - Super(initializerList) {} - - Vector(const Super& other) : - Super(other) {} - - T* data() - { - return Super::m_container.data(); - } - - const T* data() const - { - return Super::m_container.data(); - } - - operator const T*() const - { - return data(); - } -}; + return *(container.begin() + container.size() - 1); +} using ByteArray = std::vector<unsigned char>; class String quote(const ByteArray& bytes);