#pragma once /* array.hpp Шаблон работы с массивом TArray Array; // Object должен иметь конструктор по умолчанию и следующие операторы // bool operator==(const Object &) const // bool operator<(const Object &) const // const Object& operator=(const Object &) TPointerArray Array; Object должен иметь конструктор по умолчанию. Класс для тупой но прозрачной работы с массивом понтеров на класс Object */ /* Copyright (c) 1996 Eugene Roshal Copyright (c) 2000 Far Group All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the authors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include // for std::nothrow #include #include "farrtl.hpp" #ifdef __GNUC__ typedef int __cdecl(*TARRAYCMPFUNC)(const void *el1,const void *el2); #else typedef int (*TARRAYCMPFUNC)(const void *el1,const void *el2); #endif template class TArray { private: size_t internalCount, Count, Delta; Object **items; private: static int __cdecl CmpItems(const Object **el1, const Object **el2); bool deleteItem(size_t index); public: TArray(size_t Delta=8); TArray(const TArray &rhs); ~TArray() { Free(); } public: TArray& operator=(const TArray &rhs); public: void Free(); void setDelta(size_t newDelta); bool setSize(size_t newSize); Object *setItem(size_t index, const Object &newItem); Object *getItem(size_t index); const Object *getConstItem(size_t index) const; int getIndex(const Object &item, int start=-1); // сортировка массива. Offset - сколько первых пунктов пропустить void Sort(TARRAYCMPFUNC user_cmp_func=nullptr,size_t Offset=0); // упаковать массив - вместо нескольких одинаковых элементов, /* идущих подряд, оставить только один. Возвращает, false, если изменений массива не производилось. Вызов Pack() после Sort(nullptr) приведет к устранению дубликатов */ bool Pack(); public: // inline size_t getSize() const { return Count; } Object *addItem(const Object &newItem) { return setItem(Count,newItem); } }; template TArray::TArray(size_t delta): internalCount(0), Count(0), items(nullptr) { setDelta(delta); } template void TArray::Free() { if (items) { for (unsigned i=0; i Object *TArray::setItem(size_t index, const Object &newItem) { bool set=true; if (index Object *TArray::getItem(size_t index) { return (index const Object *TArray::getConstItem(size_t index) const { return (index void TArray::Sort(TARRAYCMPFUNC user_cmp_func,size_t Offset) { if (Count) { if (!user_cmp_func) user_cmp_func=reinterpret_cast(CmpItems); far_qsort(reinterpret_cast(items+Offset),Count-Offset,sizeof(Object*),user_cmp_func); } } template bool TArray::Pack() { bool was_changed=false; for (size_t index=1; index bool TArray::deleteItem(size_t index) { if (index bool TArray::setSize(size_t newSize) { bool rc=false; if (newSize < Count) // уменьшение размера { for (size_t i=newSize; i(malloc(newCount*sizeof(Object*))); if (newItems) { if (items) { memcpy(newItems,items,Count*sizeof(Object*)); free(items); } items=newItems; internalCount=newCount; for (size_t i=Count; i int __cdecl TArray::CmpItems(const Object **el1, const Object **el2) { if (el1==el2) return 0; else if (**el1==**el2) return 0; else if (**el1<**el2) return -1; else return 1; } template TArray::TArray(const TArray &rhs): items(nullptr), Count(0), internalCount(0) { *this=rhs; } template TArray& TArray::operator=(const TArray &rhs) { if (this == &rhs) return *this; setDelta(rhs.Delta); if (setSize(rhs.Count)) { for (unsigned i=0; i void TArray::setDelta(size_t newDelta) { if (newDelta<4) newDelta=4; Delta=newDelta; } template int TArray::getIndex(const Object &item, int start) { int rc=-1; if (start==-1) start=0; for (size_t i=start; i class TPointerArray { private: size_t internalCount, Count, Delta; Object **items; private: bool setSize(size_t newSize) { bool rc=false; if (newSize < Count) // уменьшение размера { Count=newSize; rc=true; } else if (newSize < internalCount) // увеличение, но в рамках имеющегося { for (size_t i=Count; i(realloc(items,newCount*sizeof(Object*))); if (newItems) { items=newItems; internalCount=newCount; for (size_t i=Count; iCount) return nullptr; Object *newItem = new(std::nothrow) Object; if (newItem && setSize(Count+1)) { for (size_t i=Count-1; i>index; i--) items[i]=items[i-1]; items[index]= newItem; return items[index]; } if (newItem) delete newItem; return nullptr; } bool deleteItem(size_t index) { if (index