File: XVector.h

package info (click to toggle)
postman 2.0-9
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 8,036 kB
  • ctags: 3,078
  • sloc: cpp: 31,131; ansic: 1,948; sh: 787; makefile: 285
file content (543 lines) | stat: -rw-r--r-- 13,004 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*                               
XVector.h
*/

#ifndef __XVECTOR_H
#define __XVECTOR_H

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

typedef int UINT;

template <class Etype>
class XVector
{
public:
        XVector( UINT initialCapacity = 10, UINT capacityIncrement = 10 );
        XVector( const XVector& rhv );
        virtual ~XVector();

        //Inspectors (additional exception throwing inspectors below)
        //===========================================================
        UINT capacity() const;
        int contains( const Etype &elem ) const;
        const Etype & firstElement() const;
        int indexOf( const Etype &elem ) const;
        int isEmpty() const;
        const Etype & lastElement() const;
        int lastIndexOf( const Etype &elem ) const;
        UINT size() const;
        UINT Count() const;
        void copyInto( Etype* array ) const;

        //Modifiers (additional exception throwing inspectors below)
        //==========================================================
        void addElement( const Etype &obj );
        void Add ( const Etype &obj );  //ALIAS AL ANTERIOR
        void ensureCapacity( UINT minCapacity );
        void removeAllElements();
        void Clear();  //ALIAS AL ANTERIOR
        int removeElement( const Etype &obj );
        void setSize( UINT newSize );
        void trimToSize();

        //Exceptions are thrown at run time with the following functions if
        //the index parameter is not within a valid range.  If the data
        //is uncertain (i.e. user inputed), then you should wrap these function
        //calls with the try/catch blocks and handle them appropriately.
        //===============================================
        Etype & elementAt( UINT index ) const;            //inspector
        Etype & getString( UINT index ) const;   //ALIAS AL ANTERIOR
        Etype & getStr( UINT index );         //COMO EL ANTERIOR PERO NO DEVUELVE CONSTANTE CHAR
        void insertElementAt( const Etype &obj, UINT index ); //modifier
        void removeElementAt( UINT index );                                //modifier
        void setElementAt( const Etype &obj, UINT index );      //modifier

        void SORT (int (*compare)(const void *, const void *), void (*flip)(const void *, const void *));
        void SORTNew (int (*compare)(const void *, const void *), void (*flip)(const void *, const void *));                              

        //C++ specific operations
        //=======================
        Etype & operator[]( UINT index );

        int SaveToFile (const char *fname, bool PutCR);
        int LoadFromFile (const char *fname);
        char *toPChar (bool PutCR);

protected:
        int min( UINT left, UINT right ) const;
        void verifyIndex( UINT index, char *debug ) const;
        void ajustIndex( UINT &index) const;
        UINT m_size;
        UINT m_capacity;
        UINT m_increment;
        Etype** m_pData;
};

//===============================================================
//Implementation of constructor, destructor, and member functions
//Necessary location for appropriate template enstantiation.
//===============================================================
template <class Etype>
XVector<Etype>::XVector( UINT initialCapacity, UINT capacityIncrement ) 
{
        m_size = 0;
        m_capacity = initialCapacity;
        m_pData = new Etype*[ m_capacity ];
        m_increment = capacityIncrement;
}

template <class Etype>
XVector<Etype>::XVector( const XVector<Etype>& rhv )
{
        m_size = rhv.m_size;
        m_capacity = rhv.m_capacity;
        m_pData = new Etype*[ m_capacity ];
        m_increment = rhv.m_increment;

        for( UINT i = 0; i < m_size; i++ )
        {
                m_pData[i] = new Etype( *(rhv.m_pData[i]) );
        }
}

template <class Etype>
XVector<Etype>::~XVector()
{
        removeAllElements();
        delete [] m_pData;
}

template <class Etype>
UINT
XVector<Etype>::capacity() const
{
        return m_capacity;
}

template <class Etype>
int
XVector<Etype>::contains( const Etype &elem ) const
{
        for ( UINT i = 0; i < m_size; i++ )
        {
                if ( *m_pData[i] == elem )
                        return 1;
        }

        return 0;
}

template <class Etype>
void
XVector<Etype>::copyInto( Etype* array ) const
{
        for( UINT i = 0; i < m_size; i++ )
                array[i] = *m_pData[i];
}


template <class Etype>
Etype &
XVector<Etype>::elementAt( UINT index ) const
{
        //verifyIndex( index, "elementAt" );
        ajustIndex (index);
        return *m_pData[index];
}

template <class Etype>
Etype &
XVector<Etype>::getString( UINT index ) const
{
        //verifyIndex( index, "getString" );
        ajustIndex (index);
        return *m_pData[index];
}

template <class Etype>
Etype &
XVector<Etype>::getStr( UINT index )
{
        verifyIndex( index, "getStr" );
        return *m_pData[index];
}

template <class Etype>
const Etype &
XVector<Etype>::firstElement() const
{
        if ( m_size == 0 )
        {
                //throw "Empty XVector Exception";
                printf ("firstElement() called on empty XVector\n");
                exit(1);
        }

        return *m_pData[ 0 ];
}

template <class Etype>
int
XVector<Etype>::indexOf( const Etype &elem ) const
{
        for ( UINT i = 0; i < m_size; i++ )
        {
                if ( *m_pData[ i ] == elem )
                        return i;
        }

        return -1;
}

template <class Etype>
int
XVector<Etype>::isEmpty() const
{
        return m_size == 0;
}

template <class Etype>
const Etype &
XVector<Etype>::lastElement() const
{
        if ( m_size == 0 )
        {
                //throw "Empty XVector Exception"
                printf("lastElement() called on empty XVector\n");
                exit(1);
        }

        return *m_pData[ m_size - 1 ];
}

template <class Etype>
int
XVector<Etype>::lastIndexOf( const Etype &elem ) const
{
        //check for empty vector
        if ( m_size == 0 )
                return -1;

        UINT i = m_size;
        
        do
        {
                i -= 1;
                if ( *m_pData[i] == elem )
                        return i;

        }
        while ( i != 0 );

        return -1;
}

template <class Etype>
UINT
XVector<Etype>::size() const
{
        return m_size;
}

template <class Etype>
UINT
XVector<Etype>::Count() const
{
        return m_size;
}

template <class Etype>
void
XVector<Etype>::addElement( const Etype &obj )
{
        if ( m_size == m_capacity )
                ensureCapacity( m_capacity + m_increment );

        m_pData[ m_size++ ] = new Etype( obj );
}

template <class Etype>
void
XVector<Etype>::Add( const Etype &obj )
{
        if ( m_size == m_capacity )
                ensureCapacity( m_capacity + m_increment );

        m_pData[ m_size++ ] = new Etype( obj );
}

template <class Etype>
void
XVector<Etype>::ensureCapacity( UINT minCapacity )
{
        if ( minCapacity > m_capacity )
        {
                UINT i;
                m_capacity = minCapacity;

                Etype** temp = new Etype*[ m_capacity ];

                //copy all the elements over upto newsize
                for ( i = 0; i < m_size; i++ )
                        temp[i] = m_pData[i];

                delete [] m_pData;
                m_pData = temp;
        }
}

template <class Etype>
void
XVector<Etype>::insertElementAt( const Etype &obj, UINT index )
{
        if ( index > m_size )
                verifyIndex( index, "insertElementAt" );   //this will throw if true

        if ( m_size == m_capacity )
                ensureCapacity( m_capacity + m_increment);

        Etype* newItem = new Etype(obj);        //pointer to new item
        Etype* tmp;                                                     //temp to hold item to be moved over.
        for( UINT i = index; i < m_size; i++ )
        {
                tmp = m_pData[i];
                m_pData[i] = newItem;

                if ( i + 1 != m_size )
                        newItem = tmp;
                else
                {
                        m_pData[i+1] = tmp;
                        break;
                }
        }

        m_size++;
}

template <class Etype>
void
XVector<Etype>::removeAllElements()
  {
  //avoid memory leak
  for (UINT i = 0; i < m_size; i++ ) {delete m_pData[i];}
  m_size = 0;
  }

template <class Etype>
void
XVector<Etype>::Clear()
{
        //avoid memory leak
        for ( UINT i = 0; i < m_size; i++ )
                delete m_pData[i];

        m_size = 0;
}

template <class Etype>
int
XVector<Etype>::removeElement( const Etype &obj )
{
        for ( UINT i = 0; i < m_size; i++ )
        {
                if ( *m_pData[i] == obj )
                {
                        removeElementAt( i );
                        return 1;
                }
        }

        return 0;
}

template <class Etype>
void
XVector<Etype>::removeElementAt( UINT index )
{
        verifyIndex( index, "removeElementAt" );

        delete m_pData[ index ];

        for ( UINT i = index+1; i < m_size; i++ )
                m_pData[ i - 1 ] = m_pData[ i ];

        m_size--;
}

template <class Etype>
void
XVector<Etype>::setElementAt( const Etype &obj, UINT index )
{
        verifyIndex( index, "setElementAt" );

        *m_pData[ index ] = obj;
}

template <class Etype>
void
XVector<Etype>::setSize( UINT newSize )
{
        if ( newSize > m_capacity )
                ensureCapacity( newSize );
        else if ( newSize < m_size )
        {
                for( UINT i = newSize; i < m_size; i++ )
                        delete m_pData[i];

                m_size = newSize;
        }
}

template <class Etype>
void
XVector<Etype>::trimToSize()
{
        if ( m_size != m_capacity )
        {
                Etype** temp = new Etype*[ m_size ];
                UINT i;

                for ( i = 0; i < m_size; i++ )
                        temp[i] = m_pData[i];

                delete [] m_pData;

                m_pData = temp;
                m_capacity = m_size;
        }
}

template <class Etype>
int
XVector<Etype>::min( UINT left, UINT right ) const
{
        return left < right ? left : right;
}

template <class Etype>
void
XVector<Etype>::SORT (int (*compare)(const void *, const void *), void (*flip)(const void *, const void *))
  {
  int changed, res;
  do 
    {
    changed = 0;
    for (int i = 0; i < m_size - 1; ++i)
      {
      res = compare (m_pData[i + 1], m_pData[i]);
      if (res < 0)  
        {
        flip (m_pData[i], m_pData[i + 1]);
        changed = 1;
        }
      }
    } 
  while (changed);
  }

template <class Etype>
void
XVector<Etype>::verifyIndex( UINT index, char *debug ) const
{
        if ( index >= m_size )
        {
                //throw "Index Out Of Bounds";
                fprintf (stderr, "XVector.h: Index Out Of Bounds. Function='%s'. Total=%d, index=%d\n", debug, m_size, index);
                exit(1);
        }
}

template <class Etype>
void
XVector<Etype>::ajustIndex( UINT &index) const
{
        if ( index >= m_size ) {index = m_size - 1;}
        if ( index < 0 ) {index = 0;}
}       

template <class Etype>
Etype &
XVector<Etype>::operator[]( UINT index )
{
        verifyIndex( index, "operator[]" );
        return *m_pData[ index ];
}

template <class Etype>
int
XVector<Etype>::SaveToFile (const char *fname, bool PutCR)
        
{
        int res, lockres, fdFW; 
        FILE *FW;
        FW = fopen (fname, "w");
        if (FW == NULL) {return -1;}
        fdFW = fileno (FW);
        lockres = lockf (fdFW, F_LOCK, 0);
        if (lockres != 0) {fclose (FW); return -1;}
        for ( UINT i = 0; i < m_size; i++ )
          {
          if (PutCR == true)
            {
            res = fprintf (FW, "%s\n", (char *) (*m_pData[i]).cstr());
            }
          else
            {
            res = fprintf (FW, "%s", (char *) (*m_pData[i]).cstr());
            }          
          if (res <= 0) {lockf (fdFW, F_ULOCK, 0); fclose (FW); return -1;}
          }      
        lockres = lockf (fdFW, F_ULOCK, 0);
        if (fclose (FW) != 0) {return -1;}
        return 0;
}

template <class Etype>
int
XVector<Etype>::LoadFromFile (const char *fname)
        
{
        char abuf[2001];
        FILE *FR;
        Clear();
        FR = fopen (fname, "r");
        if (FR == NULL) {return -1;}
        
        while (fscanf (FR, "%2000s\n", abuf) != EOF)
          {
          Add (abuf);
          }
                               
        if (fclose (FR) != 0) {return -1;}
        return 0;
}

template <class Etype>
char *
XVector<Etype>::toPChar (bool PutCR)
{
   XString xs;
   xs = XString("");
   for ( UINT i = 0; i < m_size; i++ )
     {
     if (PutCR == true)
       {
       xs += (*m_pData[i]).cstr();
       xs += "\n";
       }
     else
       {
       xs += (*m_pData[i]).cstr();
       }
     }
   return xstrdup(xs.cstr());                  
}

#endif