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_VECTOR_IMPL_H michael@0: #define ANDROID_VECTOR_IMPL_H michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: // No user serviceable parts in here... michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: namespace android { michael@0: michael@0: /*! michael@0: * Implementation of the guts of the vector<> class michael@0: * this ensures backward binary compatibility and michael@0: * reduces code size. michael@0: * For performance reasons, we expose mStorage and mCount michael@0: * so these fields are set in stone. michael@0: * michael@0: */ michael@0: michael@0: class VectorImpl michael@0: { michael@0: public: michael@0: enum { // flags passed to the ctor michael@0: HAS_TRIVIAL_CTOR = 0x00000001, michael@0: HAS_TRIVIAL_DTOR = 0x00000002, michael@0: HAS_TRIVIAL_COPY = 0x00000004, michael@0: }; michael@0: michael@0: VectorImpl(size_t itemSize, uint32_t flags); michael@0: VectorImpl(const VectorImpl& rhs); michael@0: virtual ~VectorImpl(); michael@0: michael@0: /*! must be called from subclasses destructor */ michael@0: void finish_vector(); michael@0: michael@0: VectorImpl& operator = (const VectorImpl& rhs); michael@0: michael@0: /*! C-style array access */ michael@0: inline const void* arrayImpl() const { return mStorage; } michael@0: void* editArrayImpl(); michael@0: michael@0: /*! vector stats */ michael@0: inline size_t size() const { return mCount; } michael@0: inline bool isEmpty() const { return mCount == 0; } michael@0: size_t capacity() const; michael@0: ssize_t setCapacity(size_t size); michael@0: ssize_t resize(size_t size); michael@0: michael@0: /*! append/insert another vector or array */ michael@0: ssize_t insertVectorAt(const VectorImpl& vector, size_t index); michael@0: ssize_t appendVector(const VectorImpl& vector); michael@0: ssize_t insertArrayAt(const void* array, size_t index, size_t length); michael@0: ssize_t appendArray(const void* array, size_t length); michael@0: michael@0: /*! add/insert/replace items */ michael@0: ssize_t insertAt(size_t where, size_t numItems = 1); michael@0: ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); michael@0: void pop(); michael@0: void push(); michael@0: void push(const void* item); michael@0: ssize_t add(); michael@0: ssize_t add(const void* item); michael@0: ssize_t replaceAt(size_t index); michael@0: ssize_t replaceAt(const void* item, size_t index); michael@0: michael@0: /*! remove items */ michael@0: ssize_t removeItemsAt(size_t index, size_t count = 1); michael@0: void clear(); michael@0: michael@0: const void* itemLocation(size_t index) const; michael@0: void* editItemLocation(size_t index); michael@0: michael@0: typedef int (*compar_t)(const void* lhs, const void* rhs); michael@0: typedef int (*compar_r_t)(const void* lhs, const void* rhs, void* state); michael@0: status_t sort(compar_t cmp); michael@0: status_t sort(compar_r_t cmp, void* state); michael@0: michael@0: protected: michael@0: size_t itemSize() const; michael@0: void release_storage(); michael@0: michael@0: virtual void do_construct(void* storage, size_t num) const = 0; michael@0: virtual void do_destroy(void* storage, size_t num) const = 0; michael@0: virtual void do_copy(void* dest, const void* from, size_t num) const = 0; michael@0: virtual void do_splat(void* dest, const void* item, size_t num) const = 0; michael@0: virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0; michael@0: virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0; michael@0: michael@0: private: michael@0: void* _grow(size_t where, size_t amount); michael@0: void _shrink(size_t where, size_t amount); michael@0: michael@0: inline void _do_construct(void* storage, size_t num) const; michael@0: inline void _do_destroy(void* storage, size_t num) const; michael@0: inline void _do_copy(void* dest, const void* from, size_t num) const; michael@0: inline void _do_splat(void* dest, const void* item, size_t num) const; michael@0: inline void _do_move_forward(void* dest, const void* from, size_t num) const; michael@0: inline void _do_move_backward(void* dest, const void* from, size_t num) const; michael@0: michael@0: // These 2 fields are exposed in the inlines below, michael@0: // so they're set in stone. michael@0: void * mStorage; // base address of the vector michael@0: size_t mCount; // number of items michael@0: michael@0: const uint32_t mFlags; michael@0: const size_t mItemSize; michael@0: }; michael@0: michael@0: michael@0: michael@0: class SortedVectorImpl : public VectorImpl michael@0: { michael@0: public: michael@0: SortedVectorImpl(size_t itemSize, uint32_t flags); michael@0: SortedVectorImpl(const VectorImpl& rhs); michael@0: virtual ~SortedVectorImpl(); michael@0: michael@0: SortedVectorImpl& operator = (const SortedVectorImpl& rhs); michael@0: michael@0: //! finds the index of an item michael@0: ssize_t indexOf(const void* item) const; michael@0: michael@0: //! finds where this item should be inserted michael@0: size_t orderOf(const void* item) const; michael@0: michael@0: //! add an item in the right place (or replaces it if there is one) michael@0: ssize_t add(const void* item); michael@0: michael@0: //! merges a vector into this one michael@0: ssize_t merge(const VectorImpl& vector); michael@0: ssize_t merge(const SortedVectorImpl& vector); michael@0: michael@0: //! removes an item michael@0: ssize_t remove(const void* item); michael@0: michael@0: protected: michael@0: virtual int do_compare(const void* lhs, const void* rhs) const = 0; michael@0: michael@0: private: michael@0: ssize_t _indexOrderOf(const void* item, size_t* order = 0) const; michael@0: michael@0: // these are made private, because they can't be used on a SortedVector michael@0: // (they don't have an implementation either) michael@0: ssize_t add(); michael@0: void pop(); michael@0: void push(); michael@0: void push(const void* item); michael@0: ssize_t insertVectorAt(const VectorImpl& vector, size_t index); michael@0: ssize_t appendVector(const VectorImpl& vector); michael@0: ssize_t insertArrayAt(const void* array, size_t index, size_t length); michael@0: ssize_t appendArray(const void* array, size_t length); michael@0: ssize_t insertAt(size_t where, size_t numItems = 1); michael@0: ssize_t insertAt(const void* item, size_t where, size_t numItems = 1); michael@0: ssize_t replaceAt(size_t index); michael@0: ssize_t replaceAt(const void* item, size_t index); michael@0: }; michael@0: michael@0: }; // namespace android michael@0: michael@0: michael@0: // --------------------------------------------------------------------------- michael@0: michael@0: #endif // ANDROID_VECTOR_IMPL_H michael@0: