diff -r 202e74157de5 -r eb4c25284a19 sources/list.h --- a/sources/list.h Tue Dec 16 23:50:56 2014 +0200 +++ b/sources/list.h Mon May 04 15:51:03 2015 +0300 @@ -49,81 +49,309 @@ using ConstReverseIterator = typename C::const_reverse_iterator; using Self = Container; - Container(); - Container (int numvalues); - Container (const C& a); - Container (std::initializer_list&& a); + Container(){} + + Container (int numvalues) : + m_container (numvalues) {} + + Container (const C& other) : + m_container (other) {} + + Container (std::initializer_list&& a) : + m_container (a) {} + + T& append (const T& value) + { + m_container.push_back (value); + return m_container[m_container.size() - 1]; + } + + Iterator begin() + { + return m_container.begin(); + } + + ConstIterator begin() const + { + return m_container.cbegin(); + } + + void clear() + { + m_container.clear(); + } + + bool contains (const T& a) const + { + return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); + } + + ConstReverseIterator crbegin() const + { + return m_container.crbegin(); + } + + ConstReverseIterator crend() const + { + return m_container.crbegin(); + } + + const C& container() const + { + return m_container; + } + + Iterator end() + { + return m_container.end(); + } + + ConstIterator end() const + { + return m_container.cend(); + } + + 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.cbegin(), m_container.cend(), needle); + + if (it == m_container.cend()) + return end(); + + return it; + } + + Iterator find (Function func) + { + for (Iterator it = begin(); it != end(); ++it) + { + if (func (*it)) + return it; + } + + return end(); + } + + ConstIterator find (Function func) 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); + } - auto append (const T& value) -> T&; - auto begin() -> Iterator; - auto begin() const -> ConstIterator; - auto clear() -> void; - auto contains (const T& a) const -> bool; - auto crbegin() const -> ConstReverseIterator; - auto crend() const -> ConstReverseIterator; - auto container() const -> const C&; - auto end() -> Iterator; - auto end() const -> ConstIterator; - auto find (const T& needle) -> Iterator; - auto find (const T& needle) const -> ConstIterator; - auto find (Function func) -> Iterator; - auto find (Function func) const -> ConstIterator; - auto first() -> T&; - auto first() const -> const T&; - auto insert (int pos, const T& value) -> void; - auto is_empty() const -> bool; - auto last() -> T&; - auto last() const -> const T&; - auto merge (const Self& other) -> void; - auto pop (T& val) -> bool; - auto prepend (const T& value) -> T&; - auto rbegin() -> ReverseIterator; - auto remove_at (int pos) -> void; - auto remove_duplicates() -> void; - auto remove_one (const T& it) -> void; - auto rend() -> ReverseIterator; - auto resize (int size) -> void; - auto reverse() const -> Self; - auto size() const -> int; - auto sort() -> void; - auto splice (int a, int b) const -> Self; - auto splice (const Range& a) const -> Self; + 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); + } - auto operator<< (const T& value) -> Self&; - auto operator<< (const Self& vals) -> Self&; - auto operator[] (int n) -> T&; - auto operator[] (int n) const -> const T&; - auto operator[] (Range const& n) const -> Self; - auto operator+ (const Self& other) const -> Self; + 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 a, int b) const + { + if (a < 0 or b >= size() or b < a) + return Self(); + + Self result; + + for (int i = a; i <= b; ++i) + result << operator[] (i); + + return result; + } + + Self splice (const Range& a) const + { + return splice (a.min(), a.max()); + } + + 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 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 -Container& operator>> (const T& value, Container& haystack); +Container& operator>> (const T& value, Container& haystack) +{ + haystack.prepend (value); + return haystack; +} + +// ------------------------------------------------------------------------------------------------- +// template using List = Container>; +// ------------------------------------------------------------------------------------------------- +// + template class Vector : public Container> { public: using Super = Container>; + using Super::Container; - Vector() {} - Vector (int numvalues) : Super (numvalues) {} - Vector (const Vector& a) : Super (a) {} - Vector (std::initializer_list&& a) : Super (a) {} - Vector (T* data, size_t length) : Super (std::vector (data, data + length)) {} + Vector(){} - auto data() -> T* + Vector (T* data, size_t length) : + Super (std::vector (data, data + length)) {} + + T* data() { return Super::m_container.data(); } - auto data() const -> const T* + const T* data() const { return Super::m_container.data(); } @@ -132,412 +360,4 @@ { return data(); } -}; - -// -// ------------------------------------------------------------------------------------------------- -// -// IMPLEMENTATIONS -// - -template -Container::Container() {} - -// ------------------------------------------------------------------------------------------------- -// -template -Container::Container (const C& other) : - m_container (other) {} - -// ------------------------------------------------------------------------------------------------- -// -template -Container::Container (std::initializer_list && a) : - m_container (a) {} - -// ------------------------------------------------------------------------------------------------- -// -template -Container::Container (int numvalues) : - m_container (numvalues) {} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::begin() -> Iterator -{ - return m_container.begin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::begin() const -> ConstIterator -{ - return m_container.cbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::end() -> Iterator -{ - return m_container.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::end() const -> ConstIterator -{ - return m_container.cend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::rbegin() -> ReverseIterator -{ - return m_container.rbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::crbegin() const -> ConstReverseIterator -{ - return m_container.crbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::rend() -> ReverseIterator -{ - return m_container.rend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::crend() const -> ConstReverseIterator -{ - return m_container.crend(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::remove_at (int pos) -> void -{ - assert (pos < size()); - m_container.erase (m_container.begin() + pos); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::prepend (const T& value) -> T& -{ - m_container.push_front (value); - return m_container[0]; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::append (const T& value) -> T& -{ - m_container.push_back (value); - return m_container[m_container.size() - 1]; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::merge (const Self& other) -> void -{ - int oldsize = size(); - resize (size() + other.size()); - std::copy (other.begin(), other.end(), begin() + oldsize); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::pop (T& val) -> bool -{ - if (is_empty()) - return false; - - val = m_container[size() - 1]; - m_container.erase (m_container.end() - 1); - return true; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator<< (const T& value) -> Self& -{ - append (value); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator<< (const Self& vals) -> Self& -{ - merge (vals); - return *this; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::reverse() const -> Self -{ - Self rev; - std::copy (rbegin(), rend(), rev.begin()); - return rev; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::clear() -> void -{ - m_container.clear(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::insert (int pos, const T& value) -> void -{ - m_container.insert (m_container.begin() + pos, value); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::remove_duplicates() -> void -{ - sort(); - resize (std::distance (begin(), std::unique (begin(), end()))); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::size() const -> int -{ - return m_container.size(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator[] (int n) -> T& -{ - assert (n < size()); - return m_container[n]; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator[] (int n) const -> const T& -{ - assert (n < size()); - return m_container[n]; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator[] (const Range& n) const -> Self -{ - return splice (n); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::resize (int size) -> void -{ - m_container.resize (size); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::sort() -> void -{ - std::sort (begin(), end()); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::find (const T& needle) -> Iterator -{ - auto it = std::find (m_container.begin(), m_container.end(), needle); - - if (it == m_container.end()) - return end(); - - return it; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::find (const T& needle) const -> ConstIterator -{ - auto it = std::find (m_container.cbegin(), m_container.cend(), needle); - - if (it == m_container.cend()) - return end(); - - return it; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::find (Function func) -> Iterator -{ - for (Iterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::find (Function func) const -> ConstIterator -{ - for (ConstIterator it = begin(); it != end(); ++it) - { - if (func (*it)) - return it; - } - - return end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::remove_one (const T& a) -> void -{ - auto it = std::find (m_container.begin(), m_container.end(), a); - - if (it != m_container.end()) - m_container.erase (it); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::is_empty() const -> bool -{ - return size() == 0; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::splice (int a, int b) const -> Self -{ - if (a < 0 or b >= size() or b < a) - return Self(); - - Self result; - - for (int i = a; i <= b; ++i) - result << operator[] (i); - - return result; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::splice (const Range& a) const -> Self -{ - return splice (a.min(), a.max()); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::container() const -> const C& -{ - return m_container; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::first() -> T& -{ - return *m_container.begin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::first() const -> const T& -{ - return *m_container.cbegin(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::last() -> T& -{ - return *(m_container.end() - 1); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::last() const -> const T& -{ - return *(m_container.cend() - 1); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::contains (const T& a) const -> bool -{ - return std::find (m_container.cbegin(), m_container.cend(), a) != m_container.end(); -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto Container::operator+ (const Self& other) const -> Self -{ - Self out (*this); - out.merge (other); - return out; -} - -// ------------------------------------------------------------------------------------------------- -// -template -auto operator>> (const T& value, Container& haystack) -> Container& -{ - haystack.prepend (value); - return haystack; -} +}; \ No newline at end of file