/** * Yudit Unicode Editor Source File * * GNU Copyright (C) 1997-2006 Gaspar Sinai * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License, version 2, * dated June 1991. See file COPYYING for details. * * 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef SBinVector_h #define SBinVector_h /** * @author: Gaspar Sinai * @version: 2000-04-23 */ #include "SBVector.h" #include "SExcept.h" #include /** * This is my vector. stoolkit as that. There is no code, * just this. This vector is for basic types like int, char e.t.c. */ template class SBinVector : public SBVector { public: SBinVector (void) : SBVector () { } virtual ~SBinVector () {} SBinVector (const SBinVector& v) : SBVector(v) {}; virtual SObject* clone() const { SBinVector* n = new SBinVector (*this); CHECK_NEW(n); return n;} int replace (const SBinVector&e, const SBinVector&with, unsigned int from=0) { return SBVector::replace((char*)e.array(), e.size()*sizeof (Type), (char*)with.array(), with.size()*sizeof(Type), from, sizeof(Type))/sizeof(Type); } int replaceAll (const SBinVector&e, const SBinVector&with, unsigned int from=0) { return SBVector::replaceAll((char*)e.array(), e.size()*sizeof (Type), (char*)with.array(), with.size()*sizeof(Type), from, sizeof(Type)); } void append (const SBinVector& v) { insert (size(), v.array(), v.size()); } void append (const Type& v) { insert (size(), &v); } void append (const Type* v, unsigned int len) { insert (size(), v, len); } void insert (unsigned int ind, const Type& v) { insert (ind, &v, 1); } void insert (unsigned int ind, const SBinVector& v) { insert (ind, v.array(), v.size()); } unsigned int appendSorted (const Type& v) { unsigned int top = findSorted (v); // Append at the end... for (unsigned int i=top; i bottom) { mid = (top+bottom)/2; Type m = peek(mid); if (v == m) { top = mid; break; } if (v < m) { top = mid; continue; } bottom = mid + 1; } return top; } Type peek (unsigned int index) const { return *((Type*)SBVector::peek(index * sizeof (Type))); } void truncate (unsigned int _size) { remove (_size, size() - _size); } void ensure (unsigned int _size) { SBVector::ensure (_size * sizeof (Type)); } unsigned int size () const { return SBVector::size() / sizeof (Type); } Type operator[] (unsigned int index) const { return (Type) (*(Type*)SBVector::peek(index * sizeof (Type))); } const Type* array () const { return (Type*) SBVector::array(); } void insert (unsigned int index, const Type* e, unsigned int len=1) { SBVector::insert (index*sizeof(Type), (const char*) e, len * sizeof (Type)); } void replace (unsigned int index, Type e) { SBVector::replace (index*sizeof (Type), (const char*) &e, sizeof (Type)); } void remove (unsigned int index, unsigned int len=1) { SBVector::remove (index*sizeof (Type), len * sizeof (Type)); } int find (const SBinVector& v, unsigned int from=0) const { int start = SBVector::find ((char*) v.array(), v.size(), from * sizeof(Type), sizeof (Type)); return (start<0) ? -1 : start/sizeof(Type); } int find (const Type v, unsigned int from=0) const { const Type tmpV = v; int start = SBVector::find ((const char*) (&tmpV), sizeof (Type), from * sizeof(Type), sizeof (Type)); return (start<0) ? -1 : start/sizeof(Type); } }; #endif /* SBinVector _h*/