/* ---- This file is part of SECONDO. Copyright (C) 2017, University in Hagen, Department of Computer Science, Database Systems for New Applications. SECONDO is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. SECONDO 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 SECONDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ---- This is a fake version of DbArray working only in main memory. This can be used to build main memory versions of temporarly objects. */ #ifndef MMDBARRAY_H #define MMDBARRAY_H #include #include #include #include #include "Flob.h" #include "DbArray.h" template class AComparator{ public: AComparator( int (*cmp__)( const void *a, const void *b) ): cmp(cmp__){} bool operator()(B a, B b){ return cmp(&a,&b)<0; } private: int (*cmp)( const void *a, const void *b) ; }; template class MMDbArray : public Flob { public: /* ~Standard constructor~ Does nothing. Should only be used within Cast functions. */ inline MMDbArray() {} /* ~Constructor~ Creates a DbArray with a given capacity. Use this instead of the standard constructor to create a new ~DbArray~! */ inline MMDbArray( const int n ): Flob("dummy"), content() { } /* ~Destructor~ */ inline ~MMDbArray() {} /* ~Resize~ Changes the capacity of the DbArray. */ bool resize( const SmiSize& newSize ){ if(newSize==0){ return clean(); } // decrease size of underlying vector while(content.size()>newSize){ content.pop_back(); } // ignore enlargement return true; } virtual bool clean() { content.clear(); return true; } inline bool Destroy() { content.clear(); return true; } inline bool Append( const DbArrayElement& elem ) { content.push_back(elem); return true; } inline bool Append(const MMDbArray& a){ for(size_t i=0;i& dest, int sourceOffset, int numberOfElems, int destOffset) { for(int i=0;i= 0 && ((size_t)index<=content.size())); if((index>=0) && ((size_t)index(cmp)); return true; } /* ~Find~ Searches (binary search) for a given key in the database array given the ~cmp~ comparison criteria. It is assumed that the array is sorted. The function returns true if the ~key~ is in the array and false otherwise. The position is returned in the ~result~ argument. If ~key~ is not in the array, then ~result~ contains the position where it should be. */ bool Find( const void *key, int (*cmp)( const void *a, const void *b), int& result ) const { DbArrayElement elem; if(content.size() == 0) { result = 0; return false; } // check if key is smaller than the smallest element Get( 0, &elem ); if( cmp( key, &elem ) < 0 ) { result = 0; return false; } // check if key is larger than the largest element Get( content.size() - 1, elem ); if( cmp( key, &elem ) > 0 ) { result = content.size(); return false; } int first = 0, last = content.size() - 1, mid; while (first <= last) { mid = ( first + last ) / 2; Get( mid, &elem ); if( cmp( key, &elem ) > 0 ){ first = mid + 1; } else if( cmp( key, &elem ) < 0 ) { last = mid - 1; } else { result = mid; return true; } } result = first < last ? first + 1 : last + 1; return false; } /* Restricts the DbArray to the interval set of indices passed as argument. */ virtual bool Restrict( const std::vector< std::pair >& intervals, MMDbArray& result ) const{ result.content.clear(); for(size_t i=0;i& src){ content = src.content; return true; } bool copyTo(DbArray& dest){ dest.content = this->content; return true; } bool copyFrom(const Flob& src){ assert(false); return false; } bool copyTo(Flob& dest) const{ assert(false); return false; } class const_iterator{ friend class MMDbArray; public: const_iterator(const const_iterator& other): mmdbarray(other.mmdbarray), position(other.position), elem(other.elem) {} const_iterator& operator=(const const_iterator& other){ mmdbarray = other.mmdbarray; position = other.position; elem = other.elem; return *this; } const_iterator& operator++(){ if(mmdbarray){ if(positionSize()){ position++; if(positionSize()){ mmdbarray->Get(position,elem); } } else { // end() reached position = mmdbarray->Size(); } } else { // single element iterator if(position<=0){ // 0 or -1 allowed position++; } } return *this; } const_iterator& operator++(int dummy){ if(mmdbarray){ if(positionSize()){ position++; if(positionSize()){ mmdbarray->Get(position,elem); } } else { // end() reached position = mmdbarray->Size(); } } else { // single element iterator if(position<=0){ // 0 or -1 allowed position++; } } return *this; } const_iterator& operator--(){ if(mmdbarray){ if(position>=0){ position--; if(position>=0 && positionSize()){ mmdbarray->Get(position, elem); } } else { position = -1; } } else { if(position >=0){ position--; } } return *this; } const_iterator& operator--(int dummy){ if(mmdbarray){ if(position>=0){ position--; if(position>=0 && positionSize()){ mmdbarray->Get(position, elem); } } else { position = -1; } } else { if(position >=0){ position--; } } return *this; } const DbArrayElement& operator*() const{ if(mmdbarray){ assert(position>=0); assert(position < mmdbarray->Size()); return elem; } else { assert(position==0); return elem; } } bool operator==(const const_iterator& other) const{ return position == other.position; } bool operator<(const const_iterator& other) const{ return position < other.position; } bool operator>(const const_iterator& other) const{ return position > other.position; } bool operator!=(const const_iterator& other) const{ return position != other.position; } bool operator<=(const const_iterator& other) const{ return position <= other.position; } bool operator>=(const const_iterator& other) const{ return position >= other.position; } private: const MMDbArray* mmdbarray; int position; DbArrayElement elem; const_iterator(const MMDbArray* mmdbarray_, const int position_ = 0 ): mmdbarray(mmdbarray_), position(position_) { if(position<0){ position=-1; } else if(position < mmdbarray->Size()){ mmdbarray->Get(position,elem); } else { position = mmdbarray->Size(); } } const_iterator(const DbArrayElement& elem_): mmdbarray(0),position(0), elem(elem_) {} }; const_iterator begin() const{ const_iterator it(this); return it; } const_iterator end() const{ const_iterator it(this,Size()); return it; } static const_iterator elem_iter_begin(const DbArrayElement& elem){ const_iterator it(elem); return it; } static const_iterator elem_iter_end(const DbArrayElement& elem){ const_iterator it(elem); it++; return it; } private: std::vector content; }; template< class DbArrayElement> void convertDbArrays(const DbArray& src, MMDbArray& result) { result.resize(src.Size()); DbArrayElement elem; for(int i=0;i void convertDbArrays(const DbArray& src, DbArray& result) { result.resize(src.Size()); DbArrayElement elem; for(int i=0;i void convertDbArrays(const MMDbArray& src, DbArray& result) { result.resize(src.Size()); DbArrayElement elem; for(int i=0;i void convertDbArrays(const MMDbArray& src, MMDbArray& result) { result.resize(src.Size()); DbArrayElement elem; for(int i=0;i