From cc54c7723cbfb7caaf770fce338c75c13a27d4b9 Mon Sep 17 00:00:00 2001 From: Eugene Sandulenko Date: Sun, 22 May 2016 20:47:24 +0200 Subject: COMMON: Implement SortedArray --- common/array.h | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/common/array.h b/common/array.h index db1a62ba34..320b367809 100644 --- a/common/array.h +++ b/common/array.h @@ -361,6 +361,57 @@ protected: }; +/** + * Double linked list with sorted nodes. + */ +template +class SortedArray : public Array { +public: + typedef T *iterator; + typedef uint size_type; + + SortedArray(int (*comparator)(const void *, const void *)) { + _comparator = comparator; + } + + /** + * Inserts element at the sorted position. + */ + void insert(const T &element) { + T *where = (T *)bsearch(element, this->front(), this->_size, sizeof(T), _comparator); + insert(where, element); + } + + T &operator[](size_type idx) { + error("Operation not allowed with SortedArray"); + } + + void insert_at(size_type idx, const T &element) { + error("Operation not allowed with SortedArray"); + } + + void insert_at(size_type idx, const Array &array) { + error("Operation not allowed with SortedArray"); + } + + void insert(iterator pos, const T &element) { + error("Operation not allowed with SortedArray"); + } + + void push_back(const T &element) { + error("Operation not allowed with SortedArray"); + } + + void push_back(const Array &array) { + error("Operation not allowed with SortedArray"); + } + + + +private: + int (*_comparator)(const void *, const void *); +}; + } // End of namespace Common #endif -- cgit v1.2.3