Newer
Older
/*
* Utility functions for cat_model.
*
* Stuart Levy, slevy@ncsa.uiuc.edu
* National Center for Supercomputing Applications,
* University of Illinois 2001.
* This file is part of partiview, released under the
* Illinois Open Source License; see the file LICENSE.partiview for details.
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
#include <stdlib.h>
#include "cat_modelutil.h"
int Shmem::totalloc;
/*
* Variable-length arrays.
*/
template <class T>
vvec<T>::vvec() {
init();
}
template <class T>
void vvec<T>::init() {
count = room = ours = 0;
v = NULL;
}
template <class T>
vvec<T>::vvec( int space ) {
init(space);
}
template <class T>
void vvec<T>::init( int space ) {
count = 0;
if(space <= 0) space = 15;
room = space;
v = NewN( T, space );
ours = 1;
}
template <class T>
void vvec<T>::use( T *buf, int space ) {
count = 0;
if(ours) Free( v );
room = space;
v = buf;
ours = 0;
}
template <class T>
void vvec<T>::trim( int excess ) {
if(v != NULL) {
if(ours) {
v = RenewN( v, T, count+excess );
} else {
T *tv;
tv = NewN( T, count+excess );
memcpy( tv, v, count*sizeof(T) );
v = tv;
}
room = count + excess;
ours = 1;
}
}
template <class T>
vvec<T> & vvec<T>::operator= ( const vvec<T> & src ) {
room = src.room;
count = src.count;
v = src.v;
ours = 0;
trim( 0 );
return *this;
}
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
template <class T>
T *vvec<T>::needs( int nitems ) {
if(room < nitems) {
int newroom = (2*room < nitems) ? nitems + (nitems/2) : 2*room;
T *newv = NewN( T, newroom );
if(count>0 && v != NULL)
memcpy( newv, v, count*sizeof(T) );
if(ours && v) Free( v );
ours = 1;
room = newroom;
v = newv;
}
return v;
}
template <class T>
T *vvec<T>::append() {
T *p = val(count);
return p;
}
template <class T>
T *vvec<T>::val( int index ) {
needs( index+1 );
if(count <= index)
count = index+1;
return &v[index];
}
template <class T>
vvec<T>::~vvec() {
if(ours && v)
Free( v );
}
#endif /*USE_MODEL*/