michael@0: /* michael@0: * Copyright (C) 2005 The Android Open Source Project michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #ifndef ANDROID_SORTED_VECTOR_H michael@0: #define ANDROID_SORTED_VECTOR_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: namespace android { michael@0: michael@0: template michael@0: class SortedVector : private SortedVectorImpl michael@0: { michael@0: friend class Vector; michael@0: michael@0: public: michael@0: typedef TYPE value_type; michael@0: michael@0: /*! michael@0: * Constructors and destructors michael@0: */ michael@0: michael@0: SortedVector(); michael@0: SortedVector(const SortedVector& rhs); michael@0: virtual ~SortedVector(); michael@0: michael@0: /*! copy operator */ michael@0: const SortedVector& operator = (const SortedVector& rhs) const; michael@0: SortedVector& operator = (const SortedVector& rhs); michael@0: michael@0: /* michael@0: * empty the vector michael@0: */ michael@0: michael@0: inline void clear() { VectorImpl::clear(); } michael@0: michael@0: /*! michael@0: * vector stats michael@0: */ michael@0: michael@0: //! returns number of items in the vector michael@0: inline size_t size() const { return VectorImpl::size(); } michael@0: //! returns wether or not the vector is empty michael@0: inline bool isEmpty() const { return VectorImpl::isEmpty(); } michael@0: //! returns how many items can be stored without reallocating the backing store michael@0: inline size_t capacity() const { return VectorImpl::capacity(); } michael@0: //! setst the capacity. capacity can never be reduced less than size() michael@0: inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } michael@0: michael@0: /*! michael@0: * C-style array access michael@0: */ michael@0: michael@0: //! read-only C-style access michael@0: inline const TYPE* array() const; michael@0: michael@0: //! read-write C-style access. BE VERY CAREFUL when modifying the array michael@0: //! you ust keep it sorted! You usually don't use this function. michael@0: TYPE* editArray(); michael@0: michael@0: //! finds the index of an item michael@0: ssize_t indexOf(const TYPE& item) const; michael@0: michael@0: //! finds where this item should be inserted michael@0: size_t orderOf(const TYPE& item) const; michael@0: michael@0: michael@0: /*! michael@0: * accessors michael@0: */ michael@0: michael@0: //! read-only access to an item at a given index michael@0: inline const TYPE& operator [] (size_t index) const; michael@0: //! alternate name for operator [] michael@0: inline const TYPE& itemAt(size_t index) const; michael@0: //! stack-usage of the vector. returns the top of the stack (last element) michael@0: const TYPE& top() const; michael@0: //! same as operator [], but allows to access the vector backward (from the end) with a negative index michael@0: const TYPE& mirrorItemAt(ssize_t index) const; michael@0: michael@0: /*! michael@0: * modifing the array michael@0: */ michael@0: michael@0: //! add an item in the right place (and replace the one that is there) michael@0: ssize_t add(const TYPE& item); michael@0: michael@0: //! editItemAt() MUST NOT change the order of this item michael@0: TYPE& editItemAt(size_t index) { michael@0: return *( static_cast(VectorImpl::editItemLocation(index)) ); michael@0: } michael@0: michael@0: //! merges a vector into this one michael@0: ssize_t merge(const Vector& vector); michael@0: ssize_t merge(const SortedVector& vector); michael@0: michael@0: //! removes an item michael@0: ssize_t remove(const TYPE&); michael@0: michael@0: //! remove several items michael@0: inline ssize_t removeItemsAt(size_t index, size_t count = 1); michael@0: //! remove one item michael@0: inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } michael@0: michael@0: protected: michael@0: virtual void do_construct(void* storage, size_t num) const; michael@0: virtual void do_destroy(void* storage, size_t num) const; michael@0: virtual void do_copy(void* dest, const void* from, size_t num) const; michael@0: virtual void do_splat(void* dest, const void* item, size_t num) const; michael@0: virtual void do_move_forward(void* dest, const void* from, size_t num) const; michael@0: virtual void do_move_backward(void* dest, const void* from, size_t num) const; michael@0: virtual int do_compare(const void* lhs, const void* rhs) const; michael@0: }; michael@0: michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: // No user serviceable parts from here... michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: template inline michael@0: SortedVector::SortedVector() michael@0: : SortedVectorImpl(sizeof(TYPE), michael@0: ((traits::has_trivial_ctor ? HAS_TRIVIAL_CTOR : 0) michael@0: |(traits::has_trivial_dtor ? HAS_TRIVIAL_DTOR : 0) michael@0: |(traits::has_trivial_copy ? HAS_TRIVIAL_COPY : 0)) michael@0: ) michael@0: { michael@0: } michael@0: michael@0: template inline michael@0: SortedVector::SortedVector(const SortedVector& rhs) michael@0: : SortedVectorImpl(rhs) { michael@0: } michael@0: michael@0: template inline michael@0: SortedVector::~SortedVector() { michael@0: finish_vector(); michael@0: } michael@0: michael@0: template inline michael@0: SortedVector& SortedVector::operator = (const SortedVector& rhs) { michael@0: SortedVectorImpl::operator = (rhs); michael@0: return *this; michael@0: } michael@0: michael@0: template inline michael@0: const SortedVector& SortedVector::operator = (const SortedVector& rhs) const { michael@0: SortedVectorImpl::operator = (rhs); michael@0: return *this; michael@0: } michael@0: michael@0: template inline michael@0: const TYPE* SortedVector::array() const { michael@0: return static_cast(arrayImpl()); michael@0: } michael@0: michael@0: template inline michael@0: TYPE* SortedVector::editArray() { michael@0: return static_cast(editArrayImpl()); michael@0: } michael@0: michael@0: michael@0: template inline michael@0: const TYPE& SortedVector::operator[](size_t index) const { michael@0: assert( index inline michael@0: const TYPE& SortedVector::itemAt(size_t index) const { michael@0: return operator[](index); michael@0: } michael@0: michael@0: template inline michael@0: const TYPE& SortedVector::mirrorItemAt(ssize_t index) const { michael@0: assert( (index>0 ? index : -index) inline michael@0: const TYPE& SortedVector::top() const { michael@0: return *(array() + size() - 1); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::add(const TYPE& item) { michael@0: return SortedVectorImpl::add(&item); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::indexOf(const TYPE& item) const { michael@0: return SortedVectorImpl::indexOf(&item); michael@0: } michael@0: michael@0: template inline michael@0: size_t SortedVector::orderOf(const TYPE& item) const { michael@0: return SortedVectorImpl::orderOf(&item); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::merge(const Vector& vector) { michael@0: return SortedVectorImpl::merge(reinterpret_cast(vector)); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::merge(const SortedVector& vector) { michael@0: return SortedVectorImpl::merge(reinterpret_cast(vector)); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::remove(const TYPE& item) { michael@0: return SortedVectorImpl::remove(&item); michael@0: } michael@0: michael@0: template inline michael@0: ssize_t SortedVector::removeItemsAt(size_t index, size_t count) { michael@0: return VectorImpl::removeItemsAt(index, count); michael@0: } michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: template michael@0: void SortedVector::do_construct(void* storage, size_t num) const { michael@0: construct_type( reinterpret_cast(storage), num ); michael@0: } michael@0: michael@0: template michael@0: void SortedVector::do_destroy(void* storage, size_t num) const { michael@0: destroy_type( reinterpret_cast(storage), num ); michael@0: } michael@0: michael@0: template michael@0: void SortedVector::do_copy(void* dest, const void* from, size_t num) const { michael@0: copy_type( reinterpret_cast(dest), reinterpret_cast(from), num ); michael@0: } michael@0: michael@0: template michael@0: void SortedVector::do_splat(void* dest, const void* item, size_t num) const { michael@0: splat_type( reinterpret_cast(dest), reinterpret_cast(item), num ); michael@0: } michael@0: michael@0: template michael@0: void SortedVector::do_move_forward(void* dest, const void* from, size_t num) const { michael@0: move_forward_type( reinterpret_cast(dest), reinterpret_cast(from), num ); michael@0: } michael@0: michael@0: template michael@0: void SortedVector::do_move_backward(void* dest, const void* from, size_t num) const { michael@0: move_backward_type( reinterpret_cast(dest), reinterpret_cast(from), num ); michael@0: } michael@0: michael@0: template michael@0: int SortedVector::do_compare(const void* lhs, const void* rhs) const { michael@0: return compare_type( *reinterpret_cast(lhs), *reinterpret_cast(rhs) ); michael@0: } michael@0: michael@0: }; // namespace android michael@0: michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: #endif // ANDROID_SORTED_VECTOR_H