File: SBVector.h

package info (click to toggle)
yudit 2.5.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,528 kB
  • ctags: 8,403
  • sloc: cpp: 59,394; ansic: 2,585; perl: 2,398; makefile: 864; sh: 321
file content (285 lines) | stat: -rw-r--r-- 6,701 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 2002  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2001  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2000  Gaspar Sinai <gsinai@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 SBVector_h
#define SBVector_h

/**
 * @author: Gaspar Sinai <gsinai@yudit.org>
 * @version: 2000-04-23
 */
#include "SObject.h"
#include "SShared.h"
#include "SExcept.h"

#include <string.h>

class SBVector : public SObject
{
public:
  inline SBVector(void);
  inline SBVector(unsigned int size);
  inline virtual ~SBVector ();

  SBVector (const SBVector& v);
  SBVector& operator=(const SBVector& v);
  virtual SObject* clone() const 
  { SBVector* n = new SBVector (*this); CHECK_NEW(n); return n;}
  
  /**
   * insert, replace, replaceAll, remove, clear require that you properly 
   * derefer objects.
   */
  inline void  insert (unsigned int index, const char* e, unsigned int len);
  inline void  remove (unsigned int index, unsigned int len);
  void  replace (unsigned int index, const char* e, unsigned int len);
  void  clear ();

  int   replace (const char* e, unsigned int len, 
             const char* with, unsigned int withLen, unsigned int from=0, unsigned int align = 1);
  int   replaceAll (const char* e, unsigned int len, 
             const char* with, unsigned int  withLen, unsigned int from=0, unsigned int align = 1);

  inline char* peek (unsigned int index) const;
  int   find (const char* e, unsigned int len, unsigned int from, unsigned int align=1) const;

  inline unsigned int  size () const;
  static int    debug (int level);
  inline bool isNull () const;
  inline bool equals (const SBVector& v) const;
  inline const char* array() const;

protected:
  inline unsigned int count () const;
  inline void  derefer ();
  void  refer(const SBVector& v);
  inline void  ensure (unsigned int more);
private:
  SShared*  buffer;
  inline void  cleanup ();
};

/**
 * This is the base for all object vectors
 */
class SOVector : public SBVector
{
public:
  SOVector (void);
  SOVector (unsigned int size);
  SOVector (const SOVector& v);
  virtual ~SOVector ();
  virtual SObject* clone() const;
  SOVector& operator=(const SOVector& v);

  void replace (unsigned int index, const SObject& v);
  void insert (unsigned int index, const SObject& v);
  void remove (unsigned int index);
  inline void  ensure (unsigned int more)
   { SBVector::ensure (more * sizeof (SObject*)); }

  unsigned int size () const;

  void clear ();
  void refer (const SOVector& v);
  const SObject* peek (unsigned int index) const;

protected:
  void derefer();

private:
  void replaceQuetly (unsigned int index, const SObject* r);
  void cleanup();
};


//PUBLIC/INLINE
/**
 * This is the vector class. I dont prefer using STL.
 */
SBVector::SBVector(void)
{
    buffer = new SShared();
}
/**
 * This is the vector class. I dont prefer using STL.
 */
SBVector::SBVector(unsigned int _size)
{
    buffer = new SShared(_size);
}


//PUBLIC/INLINE
/**
 * Return the size of the char array
 */
unsigned int
SBVector::size() const
{
  return buffer->vectorSize;
}

//PUBLIC/INLINE
bool
SBVector::isNull () const
{
  return buffer->arraySize==0;
}

//PUBLIC/INLINE
const char*
SBVector::array() const
{
  return buffer->array;
}

//PUBLIC/INLINE
/**
 * Return the element at index.
 * @param index is the reference index of len blocks
 */
char*
SBVector::peek (unsigned int index) const
{
  if (index > buffer->vectorSize) return 0;
  return &buffer->array[index];
}


//PROTECTED/INLINE
unsigned int
SBVector::count() const
{
  return buffer->count;
}

//PROFILE - made inline, moved on top of source file
/**
 * The array will change. If the buffer is shared copy the buffer.
 */
void
SBVector::derefer()
{
  if (buffer->count==1) return;
  buffer->count--;
  buffer = new SShared (*buffer);
}

//PROFILE - made inline, moved to the top of source file
/**
 * clean up. usually called before delete.
 * remove the buffer or its reference.
 */
void
SBVector::cleanup ()
{
  if (buffer->count==1) {
    delete buffer;
  } else {
    buffer->count--;
  }
}
//PUBLIC
/**
 * The destructor
 */
SBVector::~SBVector()
{
  cleanup();
}

/**
 * Insert char's with len length at index. 
 * @param index is the reference index of len blocks
 * @param e is the pointer to the char array to be saved
 * @param len is the length of the block.
 */
void 
SBVector::insert (unsigned int index, const char* in, unsigned int len)
{
  derefer ();
  if (len==0 && buffer->arraySize!=0) return;
  ensure (len); /* Allocate at leas len  bytes*/
#ifdef NO_MEMMOVE 
  register char* _array = buffer->array;
  for (register unsigned int i=buffer->vectorSize; i>index; i--)
  {
    _array[i+len-1] = _array[i-1];
  }
#else /*NO_MEMMOVE*/
  if (index<buffer->vectorSize)
  {
    memmove (&buffer->array[index+len], &buffer->array[index], 
      buffer->vectorSize-index);
  }
#endif /*NO_MEMMOVE*/
  memcpy (&buffer->array[index], in, len);
  buffer->vectorSize += len;
}

/**
 * Remove char's with len length at index 
 * @param index is the reference index of len blocks
 * @param len is the length of the block.
 */
void 
SBVector::remove (unsigned int index, unsigned int len)
{
  derefer ();
  if (len ==0) return;
  // Move the elements down.
#ifdef NO_MEMMOVE 
  register char* _array = buffer->array;
  for (register unsigned int i=index+len; i<buffer->vectorSize; i++)
  {
    _array[i-len] = _array[i];
  }
#else
  memmove (&buffer->array[index], &buffer->array[index+len], 
    buffer->vectorSize-index-len);
#endif
  buffer->vectorSize -= len;
}

/**
 * Ensure that we have enough capacity.
 * Before this, the reference count should be one!
 * int more - the elements in bytes that we need.
 */
void
SBVector::ensure(unsigned int more)
{
  buffer->ensure (more);
}


bool
SBVector::equals (const SBVector& e) const
{
  //if (e.array() == array()) return true;

  unsigned int cmplen = e.size();
  if (size() != cmplen) return false;
  return (memcmp (array(), e.array(), cmplen) == 0);

}
#endif /* SBVector_h */