File: array.h

package info (click to toggle)
openhpi 3.8.0-2.3
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 31,888 kB
  • sloc: ansic: 225,326; cpp: 63,687; java: 16,472; cs: 15,161; python: 11,884; sh: 11,508; makefile: 4,945; perl: 1,529; xml: 36; asm: 13
file content (288 lines) | stat: -rw-r--r-- 4,959 bytes parent folder | download | duplicates (4)
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
/** 
 * @file    array.h
 *
 * The file includes a template for arrays.
 * 
 * @author  Thomas Kanngieser <thomas.kanngieser@fci.com>
 * @author  Lars Wetzel <larswetzel@users.sourceforge.net> (documentation only)
 * @version 1.0
 * @date    2004
 *
 * 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.  This
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Copyright (c) 2004 by FORCE Computers.  
 */

#ifndef dArray_h
#define dArray_h


#include <assert.h>

/**
 * Template for using arrays
 **/
template <class T> class cArray
{
  T **m_array;
  int m_num;
  int m_size;
  int m_resize;

public:
/**
 * Constructur
 **/
  cArray( int r = 1 )
     : m_array( 0 ), m_num( 0 ), m_size( 0 ), m_resize( r )
  {
    assert( r > 0 );
  }

/**
 * Copy Constructur
 **/
  cArray( const cArray &array )
    : m_array( 0 ), m_num( 0 ), m_size( 0 ), m_resize( array.m_resize )
  {
  }
  
 /**
 * Destructor
 **/ 
  ~cArray()
  {
    Clear();
  }

  /**
   * Add an element
   **/
  int Add( T *t )
  {
    if ( m_num == m_size )
       {
         T **newa = new T *[m_size+m_resize];

         if ( m_num )
              memcpy( newa, m_array, sizeof( T * ) * m_num );

         delete[] m_array;
         m_array = newa;
         m_size += m_resize;
       }

    m_array[m_num++] = t;

    return m_num-1;
  }

  /**
   * Remove an element
   **/
  T *Rem( int idx )
  {
    assert( idx >= 0 && idx < m_num );

    T *rv = m_array[idx];
    m_num--;

    if ( m_num == 0 )
	 return rv;

    int n = m_num/m_resize*m_resize+m_resize-1;

    if ( m_size > n )
       {
         m_size = n;

         T **newa = new T *[n];

         if ( idx != 0 )
              memcpy( newa, m_array, sizeof( T * ) * idx );

         if ( idx != m_num )
              memcpy( newa+idx, m_array+idx+1, (m_num - idx)*sizeof( T * ) );

         delete[] m_array;
         m_array = newa;

         return rv;
       }

    if ( idx != m_num )
         memmove( m_array+idx, m_array+idx+1, (m_num - idx)*sizeof( T * ) );

    return rv;
  }
  
  /**
   * Remove all elements
   **/
  void RemAll()
  {
    if ( m_array )
       {
         delete[] m_array;
         m_num   = 0;
         m_array = 0;
         m_size  = 0;
       }
  }
  
  /**
   * [] operator definition
   **/
  T *operator[]( int idx ) const
  {
    assert( idx >= 0 && idx < m_num );

    return m_array[idx];
  }

  /**
   * [] address operator definition
   **/
  T * & operator[]( int idx )
  {
    assert( idx >= 0 && idx < m_num );

    return m_array[idx];
  }

  /**
   * + operator definition
   **/
  cArray<T> &operator+=( T *t )
  {
    Add( t );
    return *this;
  }

  /**
   * - operator definition
   **/
  cArray<T> &operator-=( T *t )
  {
    int idx = Find( t );
    assert( idx != -1 );

    if ( idx != -1 )
         Rem( idx );

    return *this;
  }

  /**
   * Get number of elements
   **/
  int Num() const { return m_num; }

  /**
   * Find element
   **/
  int Find( T *t ) const
  {
    for( int i = 0; i < m_num; i++ )
	 if ( m_array[i] == t )
	      return i;

    return -1;
  }

  /**
   * Sort the elements
   **/
  void Sort( int (*cmp)( T **t1, T **t2 ) )
  {
    qsort( m_array, m_num, sizeof( T * ), (int (*)(const void *, const void *) )cmp );
  }

  /**
   * Search in the array for an element
   **/
  int Search( T *key, int (*cmp)( T **t1, T **t2 ), int mmax = -1 ) const
  {
    int n = m_num;
    
    if ( mmax >= 0 && mmax < m_num )
	 n = mmax;
    
    T **e = (T **)bsearch( &key, m_array, n, sizeof( T * ), (int (*)(const void *, const void *) )cmp );

    if ( e == 0 )
         return -1;

    int idx = (e - m_array);

    assert( idx >= 0 && idx < n );

    return idx;
  }

  /**
   * Clear the array
   **/
  void Clear()
  {
    if ( m_array )
       {
         for( int i = 0; i < m_num; i++ )  delete m_array[i];

         delete[] m_array;
         m_num   = 0;
         m_array = 0;
         m_size  = 0;
       }
  }

  /**
   * Insert an element into the array on a position
   **/
  int Insert( int befor, T *t )
  {
    assert( befor <= m_num );
    
    if ( befor == -1 || befor == m_num )
         return Add( t );

    if ( m_num == m_size )
       {
         T **newa = new T *[m_size+m_resize];

         if ( m_num )
              memcpy( newa, m_array, sizeof( T * ) * m_num );

         delete[] m_array;
         m_array = newa;
         m_size += m_resize;
       }

    for( int i = m_num-1; i >= befor; i-- )
         m_array[i+1] = m_array[i];

    m_num++;
    m_array[befor] = t;

    return befor;
  }

  /**
   * () operator definition
   **/
  cArray &operator=( const cArray & /*array*/ )
  {
    // this is not a real copy operator !
    Clear();

    return *this;
  }
};


#endif