media/omx-plugin/include/gb/utils/VectorImpl.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright (C) 2005 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef ANDROID_VECTOR_IMPL_H
michael@0 18 #define ANDROID_VECTOR_IMPL_H
michael@0 19
michael@0 20 #include <assert.h>
michael@0 21 #include <stdint.h>
michael@0 22 #include <sys/types.h>
michael@0 23 #include <utils/Errors.h>
michael@0 24
michael@0 25 // ---------------------------------------------------------------------------
michael@0 26 // No user serviceable parts in here...
michael@0 27 // ---------------------------------------------------------------------------
michael@0 28
michael@0 29 namespace android {
michael@0 30
michael@0 31 /*!
michael@0 32 * Implementation of the guts of the vector<> class
michael@0 33 * this ensures backward binary compatibility and
michael@0 34 * reduces code size.
michael@0 35 * For performance reasons, we expose mStorage and mCount
michael@0 36 * so these fields are set in stone.
michael@0 37 *
michael@0 38 */
michael@0 39
michael@0 40 class VectorImpl
michael@0 41 {
michael@0 42 public:
michael@0 43 enum { // flags passed to the ctor
michael@0 44 HAS_TRIVIAL_CTOR = 0x00000001,
michael@0 45 HAS_TRIVIAL_DTOR = 0x00000002,
michael@0 46 HAS_TRIVIAL_COPY = 0x00000004,
michael@0 47 };
michael@0 48
michael@0 49 VectorImpl(size_t itemSize, uint32_t flags);
michael@0 50 VectorImpl(const VectorImpl& rhs);
michael@0 51 virtual ~VectorImpl();
michael@0 52
michael@0 53 /*! must be called from subclasses destructor */
michael@0 54 void finish_vector();
michael@0 55
michael@0 56 VectorImpl& operator = (const VectorImpl& rhs);
michael@0 57
michael@0 58 /*! C-style array access */
michael@0 59 inline const void* arrayImpl() const { return mStorage; }
michael@0 60 void* editArrayImpl();
michael@0 61
michael@0 62 /*! vector stats */
michael@0 63 inline size_t size() const { return mCount; }
michael@0 64 inline bool isEmpty() const { return mCount == 0; }
michael@0 65 size_t capacity() const;
michael@0 66 ssize_t setCapacity(size_t size);
michael@0 67
michael@0 68 /*! append/insert another vector or array */
michael@0 69 ssize_t insertVectorAt(const VectorImpl& vector, size_t index);
michael@0 70 ssize_t appendVector(const VectorImpl& vector);
michael@0 71 ssize_t insertArrayAt(const void* array, size_t index, size_t length);
michael@0 72 ssize_t appendArray(const void* array, size_t length);
michael@0 73
michael@0 74 /*! add/insert/replace items */
michael@0 75 ssize_t insertAt(size_t where, size_t numItems = 1);
michael@0 76 ssize_t insertAt(const void* item, size_t where, size_t numItems = 1);
michael@0 77 void pop();
michael@0 78 void push();
michael@0 79 void push(const void* item);
michael@0 80 ssize_t add();
michael@0 81 ssize_t add(const void* item);
michael@0 82 ssize_t replaceAt(size_t index);
michael@0 83 ssize_t replaceAt(const void* item, size_t index);
michael@0 84
michael@0 85 /*! remove items */
michael@0 86 ssize_t removeItemsAt(size_t index, size_t count = 1);
michael@0 87 void clear();
michael@0 88
michael@0 89 const void* itemLocation(size_t index) const;
michael@0 90 void* editItemLocation(size_t index);
michael@0 91
michael@0 92 typedef int (*compar_t)(const void* lhs, const void* rhs);
michael@0 93 typedef int (*compar_r_t)(const void* lhs, const void* rhs, void* state);
michael@0 94 status_t sort(compar_t cmp);
michael@0 95 status_t sort(compar_r_t cmp, void* state);
michael@0 96
michael@0 97 protected:
michael@0 98 size_t itemSize() const;
michael@0 99 void release_storage();
michael@0 100
michael@0 101 virtual void do_construct(void* storage, size_t num) const = 0;
michael@0 102 virtual void do_destroy(void* storage, size_t num) const = 0;
michael@0 103 virtual void do_copy(void* dest, const void* from, size_t num) const = 0;
michael@0 104 virtual void do_splat(void* dest, const void* item, size_t num) const = 0;
michael@0 105 virtual void do_move_forward(void* dest, const void* from, size_t num) const = 0;
michael@0 106 virtual void do_move_backward(void* dest, const void* from, size_t num) const = 0;
michael@0 107
michael@0 108 // take care of FBC...
michael@0 109 virtual void reservedVectorImpl1();
michael@0 110 virtual void reservedVectorImpl2();
michael@0 111 virtual void reservedVectorImpl3();
michael@0 112 virtual void reservedVectorImpl4();
michael@0 113 virtual void reservedVectorImpl5();
michael@0 114 virtual void reservedVectorImpl6();
michael@0 115 virtual void reservedVectorImpl7();
michael@0 116 virtual void reservedVectorImpl8();
michael@0 117
michael@0 118 private:
michael@0 119 void* _grow(size_t where, size_t amount);
michael@0 120 void _shrink(size_t where, size_t amount);
michael@0 121
michael@0 122 inline void _do_construct(void* storage, size_t num) const;
michael@0 123 inline void _do_destroy(void* storage, size_t num) const;
michael@0 124 inline void _do_copy(void* dest, const void* from, size_t num) const;
michael@0 125 inline void _do_splat(void* dest, const void* item, size_t num) const;
michael@0 126 inline void _do_move_forward(void* dest, const void* from, size_t num) const;
michael@0 127 inline void _do_move_backward(void* dest, const void* from, size_t num) const;
michael@0 128
michael@0 129 // These 2 fields are exposed in the inlines below,
michael@0 130 // so they're set in stone.
michael@0 131 void * mStorage; // base address of the vector
michael@0 132 size_t mCount; // number of items
michael@0 133
michael@0 134 const uint32_t mFlags;
michael@0 135 const size_t mItemSize;
michael@0 136 };
michael@0 137
michael@0 138
michael@0 139
michael@0 140 class SortedVectorImpl : public VectorImpl
michael@0 141 {
michael@0 142 public:
michael@0 143 SortedVectorImpl(size_t itemSize, uint32_t flags);
michael@0 144 SortedVectorImpl(const VectorImpl& rhs);
michael@0 145 virtual ~SortedVectorImpl();
michael@0 146
michael@0 147 SortedVectorImpl& operator = (const SortedVectorImpl& rhs);
michael@0 148
michael@0 149 //! finds the index of an item
michael@0 150 ssize_t indexOf(const void* item) const;
michael@0 151
michael@0 152 //! finds where this item should be inserted
michael@0 153 size_t orderOf(const void* item) const;
michael@0 154
michael@0 155 //! add an item in the right place (or replaces it if there is one)
michael@0 156 ssize_t add(const void* item);
michael@0 157
michael@0 158 //! merges a vector into this one
michael@0 159 ssize_t merge(const VectorImpl& vector);
michael@0 160 ssize_t merge(const SortedVectorImpl& vector);
michael@0 161
michael@0 162 //! removes an item
michael@0 163 ssize_t remove(const void* item);
michael@0 164
michael@0 165 protected:
michael@0 166 virtual int do_compare(const void* lhs, const void* rhs) const = 0;
michael@0 167
michael@0 168 // take care of FBC...
michael@0 169 virtual void reservedSortedVectorImpl1();
michael@0 170 virtual void reservedSortedVectorImpl2();
michael@0 171 virtual void reservedSortedVectorImpl3();
michael@0 172 virtual void reservedSortedVectorImpl4();
michael@0 173 virtual void reservedSortedVectorImpl5();
michael@0 174 virtual void reservedSortedVectorImpl6();
michael@0 175 virtual void reservedSortedVectorImpl7();
michael@0 176 virtual void reservedSortedVectorImpl8();
michael@0 177
michael@0 178 private:
michael@0 179 ssize_t _indexOrderOf(const void* item, size_t* order = 0) const;
michael@0 180
michael@0 181 // these are made private, because they can't be used on a SortedVector
michael@0 182 // (they don't have an implementation either)
michael@0 183 ssize_t add();
michael@0 184 void pop();
michael@0 185 void push();
michael@0 186 void push(const void* item);
michael@0 187 ssize_t insertVectorAt(const VectorImpl& vector, size_t index);
michael@0 188 ssize_t appendVector(const VectorImpl& vector);
michael@0 189 ssize_t insertArrayAt(const void* array, size_t index, size_t length);
michael@0 190 ssize_t appendArray(const void* array, size_t length);
michael@0 191 ssize_t insertAt(size_t where, size_t numItems = 1);
michael@0 192 ssize_t insertAt(const void* item, size_t where, size_t numItems = 1);
michael@0 193 ssize_t replaceAt(size_t index);
michael@0 194 ssize_t replaceAt(const void* item, size_t index);
michael@0 195 };
michael@0 196
michael@0 197 }; // namespace android
michael@0 198
michael@0 199
michael@0 200 // ---------------------------------------------------------------------------
michael@0 201
michael@0 202 #endif // ANDROID_VECTOR_IMPL_H

mercurial