1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* 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 <gaspar@yudit.org>
* @version: 2000-04-23
*/
#include "SBVector.h"
#include "SExcept.h"
#include <stdio.h>
/**
* 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 Type> class SBinVector : public SBVector
{
public:
SBinVector (void) : SBVector () { }
virtual ~SBinVector () {}
SBinVector (const SBinVector<Type>& 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<Type>& 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<Type>& 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<size() && v == peek(i); i++)
{
top++;
}
insert (top, v);
return top;
}
/*
* It just returns the smallest not necessarily the same
*/
unsigned int findSorted (const Type& v)
{
unsigned int top, bottom, mid;
top = size(); bottom = 0;
while (top > 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<Type>& 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*/
|