File: SBVector.cpp

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 (296 lines) | stat: -rw-r--r-- 6,022 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
286
287
288
289
290
291
292
293
294
295
296
/** 
 *  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.
 */
 
#include "SBVector.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>




/**
 * Set the debug level
 * @param level 0 means no debug printouts.
 * @return the previous level
 */
int
SBVector::debug (int level)
{
  return SShared::debug(level);
}

/**
 * Create a vector from a const vector. This is 
 * used when you do
 *      SBVector v = old;
 * @param v is the old
 */
SBVector::SBVector (const SBVector& v)
{
  // Assign the common buffer and  increment the reference count
  buffer = (SShared*) v.buffer;
  buffer->count++;
}

/**
 * Assign a vector from a const vector. This is 
 * used when you do
 *      SBVector v;
 *      v = old;
 * @param v is the old
 */
SBVector&
SBVector::operator=(const SBVector& v)
{
  refer (v);
  return *this;
}

/**
 * Assign a vector from a const vector by changing the reference.
 * @param v is the input vector
 */
void
SBVector::refer(const SBVector& v)
{
  // Are we the same objects
  if (&v==this) return;
  cleanup ();
  buffer = (SShared*) v.buffer;
  buffer->count++;
  return;
}


/**
 * Replace 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::replace (unsigned int index, const char* in, unsigned int len)
{
  derefer ();
  const char* e = in;
  SShared* ns =0;
  
  if (len==0) return;
  if (len > 0 && buffer->array < in &&  &buffer->array[buffer->arraySize] > in)
  {
    fprintf (stderr, "SBVector::replace copying into itself.\n");
    ns = new SShared (in, len);
    e = ns->array; 
  }
  memcpy (&buffer->array[index], e, len);
  if (ns) delete ns;
}

/**
 * replace one occurance of buffer with another.
 */
int
SBVector::replace (const char* e, unsigned int len, const char* with, unsigned int withLen, unsigned int from, unsigned int align)
{
  derefer ();
  int i = find (e, len, from, align);
  if (i<0) return i; // Max element size
  remove (i, len);
  insert (i, with, withLen);
  return i+withLen;
}

/**
 * replace one occurance of buffer with another.
 */
int
SBVector::replaceAll (const char* e, unsigned int len, const char* with, unsigned int  withLen, unsigned int from, unsigned int align)
{
  derefer ();
  int count = 0;
  int begin =from;
  while (begin>=0)
  {
    begin=replace (e, len, with, withLen, begin, align);
    if (begin > 0) count++;
  }
  return count;
}


/**
 * Clear the array and make a new SShared.
 */
void
SBVector::clear ()
{
  derefer ();
  cleanup ();
  buffer = new SShared();
}



/**
 * Find a subvector.
 */
int
SBVector::find (const char* e, unsigned int len, unsigned int from, unsigned int align) const
{
  //fprintf (stderr, "Address=%lx\n", (unsigned long) (e));
  unsigned int sz = size();
  register char* _array = buffer->array;
  register unsigned j;
  for (register unsigned int i=from; i+len <= sz; i+=(unsigned int)align)
  {
    for (j=0; j<len; j++)
    {
  //fprintf (stderr, "array[%u + %u]=%d, e[%u] =%d\n", i, j, (int) buffer->array[i+j], j, (int) e[j]);
      if (_array[i+j] != e[j]) break;
    }
    if (j>=len) return (int) i;
  }
  return -1024;
}


/**
 * This is a replay of the obove for SObject collections
 */
SOVector::SOVector(void) : SBVector()
{
}

/**
 * This is a replay of the obove for SObject collections
 */
SOVector::SOVector(unsigned int _size) : SBVector(_size * sizeof (SObject*))
{
}

SOVector::SOVector(const SOVector& v) : SBVector(v)
{
}

SOVector::~SOVector ()
{
  cleanup();
}

SOVector&
SOVector::operator=(const SOVector& v)
{
  refer (v);
  return *this;
}

SObject*
SOVector::clone() const
{
  return new SOVector (*this);
}

void
SOVector::replace (unsigned int index, const SObject& v)
{
  derefer();
  SObject* r = (SObject*) peek (index); 
  replaceQuetly (index, &v);
  delete r;
}

void
SOVector::insert (unsigned int index, const SObject& v)
{
  derefer();
  SObject* ref= (SObject*) v.clone();
  SBVector::insert (index*sizeof(SObject*), (char*)&ref, sizeof (SObject*));
}

void
SOVector::remove (unsigned int index)
{
  derefer();
  SObject* old = (SObject*) peek (index); if (old) delete old;
  SBVector::remove (index * sizeof (SObject*), sizeof (SObject*));
}

unsigned int
SOVector::size () const
{
  return SBVector::size()/sizeof(SObject*);
}

void
SOVector::clear ()
{
  cleanup ();
  SBVector::clear();
}

void
SOVector::refer (const SOVector& v)
{
  if (&v != this)
  {
    cleanup();
    SBVector::refer(v);
  } 
}

const SObject*
SOVector::peek (unsigned int index) const
{
   return *(SObject**)SBVector::peek (index * sizeof (SObject*));
}

void
SOVector::derefer()
{
  if (count()==1) return;
  SBVector::derefer();

  for (unsigned int i=0; i<size(); i++)
  {
    SObject* r = (SObject*) peek (i); 
    replaceQuetly (i, r);
  }
}

void
SOVector::replaceQuetly (unsigned int index, const SObject* r)
{
  SObject* n = r->clone();
  SBVector::replace (index*sizeof(SObject*), (char*) &n, sizeof (SObject*));
}

void
SOVector::cleanup()
{
  if (count()!=1) return;
  for (unsigned int i=0; i<size(); i++)
  {
    SObject* r = (SObject*) peek (i); if (r==0) continue;
    delete r;
  }
}