/* =========================================================================== Copyright (C) 2000 - 2013, Raven Software, Inc. Copyright (C) 2001 - 2013, Activision, Inc. Copyright (C) 2013 - 2015, OpenJK contributors This file is part of the OpenJK source code. OpenJK is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, see . =========================================================================== */ //////////////////////////////////////////////////////////////////////////////////////// // RAVEN STANDARD TEMPLATE LIBRARY // (c) 2002 Activision // // // Vector // ------ // The vector class is a simple addition to the array. It supports some useful additions // like sort and binary search, as well as keeping track of the number of objects // contained within. // // // // // // NOTES: // // //////////////////////////////////////////////////////////////////////////////////////// #if !defined(RATL_VECTOR_VS_INC) #define RATL_VECTOR_VS_INC //////////////////////////////////////////////////////////////////////////////////////// // Includes //////////////////////////////////////////////////////////////////////////////////////// #if !defined(RATL_COMMON_INC) #include "ratl_common.h" #endif namespace ratl { //////////////////////////////////////////////////////////////////////////////////////// // The Vector Class //////////////////////////////////////////////////////////////////////////////////////// template class vector_base : public ratl_base { public: typedef T TStorageTraits; typedef typename T::TValue TTValue; //////////////////////////////////////////////////////////////////////////////////// // Capacity Enum //////////////////////////////////////////////////////////////////////////////////// static const int CAPACITY = T::CAPACITY; private: array_base mArray; // The Memory int mSize; public: //////////////////////////////////////////////////////////////////////////////////// // Constructor //////////////////////////////////////////////////////////////////////////////////// vector_base() { mSize = 0; } //////////////////////////////////////////////////////////////////////////////////// // Copy Constructor //////////////////////////////////////////////////////////////////////////////////// vector_base(const vector_base &B) { for (int i=0; i=0&&mSize<=CAPACITY); return (CAPACITY); } //////////////////////////////////////////////////////////////////////////////////// // How Many Objects Have Been Added To This Vector? //////////////////////////////////////////////////////////////////////////////////// int size() const { assert(mSize>=0&&mSize<=CAPACITY); return (mSize); } //////////////////////////////////////////////////////////////////////////////////// // Have Any Objects Have Been Added To This Vector? //////////////////////////////////////////////////////////////////////////////////// bool empty() const { assert(mSize>=0&&mSize<=CAPACITY); return (!mSize); } //////////////////////////////////////////////////////////////////////////////////// // Have Any Objects Have Been Added To This Vector? //////////////////////////////////////////////////////////////////////////////////// bool full() const { assert(mSize>=0&&mSize<=CAPACITY); return (mSize==CAPACITY); } //////////////////////////////////////////////////////////////////////////////////// // Clear Out Entire Array //////////////////////////////////////////////////////////////////////////////////// void clear() { mArray.clear(); mSize = 0; } // Constant Access Operator //////////////////////////////////////////////////////////////////////////////////// const TTValue& operator[](int index) const { assert(index>=0&&index=0&&index=0&&mSize=0&&mSize=0&&mSize0); mSize--; mArray.destruct(mSize); } //////////////////////////////////////////////////////////////////////////////////// // Resizes The Array. If New Elements Are Needed, It Uses The (value) Param //////////////////////////////////////////////////////////////////////////////////// void resize(int nSize, const TTValue& value) { int i; for (i=(mSize-1); i>=nSize; i--) { mArray.destruct(i); mSize--; } for (i=mSize; i=nSize; i--) { mArray.destruct(i); mSize--; } for (i=mSize; i=0 && Index0; HeapSize--) { // Swap The End And Front Of The "Heap" Half Of The Array //-------------------------------------------------------- mArray.swap(0, HeapSize); // We Now Have A Bogus Element At The Root, So Fix The Heap //---------------------------------------------------------- Pos = 0; Compare = largest_child(Pos, HeapSize); while (mArray[Pos]; friend class const_iterator; // Data //------ int mLoc; vector_base* mOwner; public: // Constructors //-------------- iterator() : mOwner(0), mLoc(0) {} iterator(vector_base* p, int t) : mOwner(p), mLoc(t) {} iterator(const iterator &t) : mOwner(t.mOwner), mLoc(t.mLoc) {} // Assignment Operator //--------------------- void operator= (const iterator &t) { mOwner = t.mOwner; mLoc = t.mLoc; } // Equality Operators //-------------------- bool operator!=(const iterator &t) const { return (mLoc!=t.mLoc || mOwner!=t.mOwner); } bool operator==(const iterator &t) const { return (mLoc==t.mLoc && mOwner==t.mOwner); } // DeReference Operator //---------------------- TTValue& operator* () const { assert(mLoc>=0 && mLocmSize); return (mOwner->mArray[mLoc]); } // DeReference Operator //---------------------- TTValue& value() const { assert(mLoc>=0 && mLocmSize); return (mOwner->mArray[mLoc]); } // DeReference Operator //---------------------- TTValue* operator-> () const { assert(mLoc>=0 && mLocmSize); return (&mOwner->mArray[mLoc]); } // Inc Operator //-------------- iterator operator++(int) //postfix { assert(mLoc>=0 && mLocmSize); iterator old(*this); mLoc ++; return old; } // Inc Operator //-------------- iterator operator++() { assert(mLoc>=0 && mLocmSize); mLoc ++; return *this; } }; //////////////////////////////////////////////////////////////////////////////////// // Constant Iterator //////////////////////////////////////////////////////////////////////////////////// class const_iterator { friend class vector_base; int mLoc; const vector_base* mOwner; public: // Constructors //-------------- const_iterator() : mOwner(0), mLoc(0) {} const_iterator(const vector_base* p, int t) : mOwner(p), mLoc(t) {} const_iterator(const const_iterator &t) : mOwner(t.mOwner), mLoc(t.mLoc) {} const_iterator(const iterator &t) : mOwner(t.mOwner), mLoc(t.mLoc) {} // Assignment Operator //--------------------- void operator= (const const_iterator &t) { mOwner = t.mOwner; mLoc = t.mLoc; } // Assignment Operator //--------------------- void operator= (const iterator &t) { mOwner = t.mOwner; mLoc = t.mLoc; } // Equality Operators //-------------------- bool operator!=(const iterator &t) const { return (mLoc!=t.mLoc || mOwner!=t.mOwner); } bool operator==(const iterator &t) const { return (mLoc==t.mLoc && mOwner==t.mOwner); } // Equality Operators //-------------------- bool operator!=(const const_iterator &t) const { return (mLoc!=t.mLoc || mOwner!=t.mOwner); } bool operator==(const const_iterator &t) const { return (mLoc==t.mLoc && mOwner==t.mOwner); } // DeReference Operator //---------------------- const TTValue& operator* () const { assert(mLoc>=0 && mLocmSize); return (mOwner->mArray[mLoc]); } // DeReference Operator //---------------------- const TTValue& value() const { assert(mLoc>=0 && mLocmSize); return (mOwner->mArray[mLoc]); } // DeReference Operator //---------------------- const TTValue* operator-> () const { assert(mLoc>=0 && mLocmSize); return (&mOwner->mArray[mLoc]); } // Inc Operator //-------------- const_iterator operator++(int) { assert(mLoc>=0 && mLocmSize); const_iterator old(*this); mLoc ++; return old; } // Inc Operator //-------------- const_iterator operator++() { assert(mLoc>=0 && mLocmSize); mLoc ++; return *this; } }; friend class iterator; friend class const_iterator; //////////////////////////////////////////////////////////////////////////////////// // Iterator Begin (Starts At Address 0) //////////////////////////////////////////////////////////////////////////////////// iterator begin() { return iterator(this, 0); } //////////////////////////////////////////////////////////////////////////////////// // Iterator End (Set To Address mSize) //////////////////////////////////////////////////////////////////////////////////// iterator end() { return iterator(this, mSize); } //////////////////////////////////////////////////////////////////////////////////// // Iterator Begin (Starts At Address 0) //////////////////////////////////////////////////////////////////////////////////// const_iterator begin() const { return const_iterator(this, 0); } //////////////////////////////////////////////////////////////////////////////////// // Iterator End (Set To Address mSize) //////////////////////////////////////////////////////////////////////////////////// const_iterator end() const { return const_iterator(this, mSize); } //////////////////////////////////////////////////////////////////////////////////// // Iterator Find (If Fails To Find, Returns iterator end() //////////////////////////////////////////////////////////////////////////////////// iterator find(const TTValue& value) { int index = find_index(value); // Call Find By Index if (index=0 && it.mLocmSize); if (it.mLoc != mSize - 1) { mArray.swap(it.mLoc, mSize - 1); } pop_back(); return it; } template CAST_TO *verify_alloc(CAST_TO *p) const { return mArray.verify_alloc(p); } }; template class vector_vs : public vector_base > { public: typedef typename storage::value_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; static const int CAPACITY = ARG_CAPACITY; vector_vs() {} }; template class vector_os : public vector_base > { public: typedef typename storage::object_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; static const int CAPACITY = ARG_CAPACITY; vector_os() {} }; template class vector_is : public vector_base > { public: typedef typename storage::virtual_semantics TStorageTraits; typedef typename TStorageTraits::TValue TTValue; static const int CAPACITY = ARG_CAPACITY; static const int MAX_CLASS_SIZE = ARG_MAX_CLASS_SIZE; vector_is() {} }; } #endif