1 /*  init.C  */
2 
3 #include "../IV.h"
4 
5 /*--------------------------------------------------------------------*/
6 /*
7    ---------------------------------------------
8    simplest initialization method
9 
10    if entries != NULL
11       the object does not own the entries,
12       it just points to the entries base address
13    else if size > 0
14       the object will own the entries,
15       it allocates a vector of size int's.
16    else
17       nothing happens
18    endif
19 
20    created -- 96aug28, cca
21    ---------------------------------------------
22 */
23 void
IV_init(IV * iv,int size,int * entries)24 IV_init (
25    IV    *iv,
26    int   size,
27    int   *entries
28 ) {
29 if ( iv == NULL || size < 0 ) {
30    fprintf(stderr, "\n fatal error in IV_init(%p,%d,%p)"
31            "\n bad input\n", iv, size, entries) ;
32    exit(-1) ;
33 }
34 /*
35    --------------
36    clear any data
37    --------------
38 */
39 IV_clearData(iv) ;
40 /*
41    -----------------------------
42    set the size and maximum size
43    -----------------------------
44 */
45 iv->maxsize = iv->size = size ;
46 /*
47    -------------------------
48    set vector and owner flag
49    -------------------------
50 */
51 if ( entries != NULL ) {
52    iv->owned = 0 ;
53    iv->vec   = entries ;
54 } else if ( size > 0 ) {
55    iv->owned = 1 ;
56    iv->vec   = IVinit(size, -1) ;
57 }
58 /*
59 fprintf(stdout,
60         "\n %% leaving IV_init, iv %p, size %d, maxsize %d, entries %p",
61         iv, iv->size, iv->maxsize, iv->vec) ;
62 fflush(stdout) ;
63 */
64 
65 return ; }
66 
67 /*--------------------------------------------------------------------*/
68 /*
69    -------------------------
70    basic initializion method
71 
72    created -- 95oct06, cca
73    -------------------------
74 */
75 void
IV_init1(IV * iv,int size)76 IV_init1 (
77    IV    *iv,
78    int   size
79 ) {
80 IV_init(iv, size, NULL) ;
81 
82 return ; }
83 
84 /*--------------------------------------------------------------------*/
85 /*
86    -------------------------
87    total initializion method
88 
89    created -- 95oct06, cca
90    -------------------------
91 */
92 void
IV_init2(IV * iv,int size,int maxsize,int owned,int * vec)93 IV_init2 (
94    IV    *iv,
95    int   size,
96    int   maxsize,
97    int   owned,
98    int   *vec
99 ) {
100 /*
101    ---------------
102    check the input
103    ---------------
104 */
105 if ( iv == NULL ) {
106    fprintf(stderr, "\n fatal error in IV_init2(%p,%d,%d,%d,%p)"
107            "\n bad input\n", iv, size, maxsize, owned, vec) ;
108    exit(-1) ;
109 }
110 if ( size < 0 || maxsize < size ) {
111    fprintf(stderr, "\n fatal error in IV_init2(%p,%d,%d,%d,%p)"
112            "\n size = %d, maxsize = %d \n",
113            iv, size, maxsize, owned, vec, size, maxsize) ;
114    exit(-1) ;
115 }
116 if ( owned < 0 || 1 < owned ) {
117    fprintf(stderr, "\n fatal error in IV_init2(%p,%d,%d,%d,%p)"
118            "\n owned = %d\n", iv, size, maxsize, owned, vec, owned) ;
119    exit(-1) ;
120 }
121 if ( owned == 1 && vec == NULL ) {
122    fprintf(stderr, "\n fatal error in IV_init2(%p,%d,%d,%d,%p)"
123            "\n owned = %d and vec = %p",
124            iv, size, maxsize, owned, vec, owned, vec) ;
125    exit(-1) ;
126 }
127 /*
128    --------------
129    clear any data
130    --------------
131 */
132 IV_clearData(iv) ;
133 
134 if ( vec == NULL ) {
135 /*
136    ----------------------------------------------
137    no entries input, use the simplest initializer
138    ----------------------------------------------
139 */
140    IV_init(iv, size, NULL) ;
141 } else {
142 /*
143    ---------------------------------
144    entries are input, set the fields
145    ---------------------------------
146 */
147    iv->size    = size    ;
148    iv->maxsize = maxsize ;
149    iv->owned   = owned   ;
150    iv->vec     = vec     ;
151 }
152 return ; }
153 
154 /*--------------------------------------------------------------------*/
155 /*
156    ----------------------------------
157    set the maximum size of the vector
158 
159    created -- 96dec08, cca
160    ----------------------------------
161 */
162 void
IV_setMaxsize(IV * iv,int newmaxsize)163 IV_setMaxsize (
164    IV    *iv,
165    int   newmaxsize
166 ) {
167 /*
168    ---------------
169    check the input
170    ---------------
171 */
172 if ( iv == NULL || newmaxsize < 0 ) {
173    fprintf(stderr, "\n fatal error in IV_setMaxsize(%p,%d)"
174            "\n bad input\n", iv, newmaxsize) ;
175    exit(-1) ;
176 }
177 if ( iv->maxsize > 0 && iv->owned == 0 ) {
178    fprintf(stderr, "\n fatal error in IV_setMaxsize(%p,%d)"
179            "\n iv->maxsize = %d, iv->owned = %d\n",
180            iv, newmaxsize, iv->maxsize, iv->owned) ;
181    exit(-1) ;
182 }
183 if ( iv->maxsize != newmaxsize ) {
184 /*
185    -----------------------------------
186    allocate new storage for the vector
187    -----------------------------------
188 */
189    int   *vec = IVinit(newmaxsize, -1) ;
190    if ( iv->size > 0 ) {
191 /*
192       ---------------------------------
193       copy old entries into new entries
194       ---------------------------------
195 */
196       if ( iv->vec == NULL ) {
197          fprintf(stderr, "\n fatal error in IV_setMaxsize(%p,%d)"
198                  "\n iv->size = %d, iv->vec is NULL\n",
199                  iv, newmaxsize, iv->size) ;
200          exit(-1) ;
201       }
202       if ( iv->size <= newmaxsize ) {
203 /*
204          -----------------------------------------
205          new maximum size is greater than old size
206          -----------------------------------------
207 */
208          IVcopy(iv->size, vec, iv->vec) ;
209       } else {
210 /*
211          -----------------------
212          note, data is truncated
213          -----------------------
214 */
215          IVcopy(newmaxsize, vec, iv->vec) ;
216          iv->size = newmaxsize ;
217       }
218    }
219    if ( iv->vec != NULL ) {
220 /*
221       ----------------
222       free old entries
223       ----------------
224 */
225       IVfree(iv->vec) ;
226    }
227 /*
228    ----------
229    set fields
230    ----------
231 */
232    iv->maxsize = newmaxsize ;
233    iv->owned   = 1 ;
234    iv->vec     = vec ;
235 }
236 /*
237 fprintf(stdout,
238         "\n %% leaving IV_setMaxsize, iv %p, size %d, maxsize %d, entries %p",
239         iv, iv->size, iv->maxsize, iv->vec) ;
240 fflush(stdout) ;
241 */
242 return ; }
243 
244 /*--------------------------------------------------------------------*/
245 /*
246    --------------------------
247    set the size of the vector
248 
249    created -- 96dec08, cca
250    --------------------------
251 */
252 void
IV_setSize(IV * iv,int newsize)253 IV_setSize (
254    IV    *iv,
255    int   newsize
256 ) {
257 /*
258    ---------------
259    check the input
260    ---------------
261 */
262 if ( iv == NULL || newsize < 0 ) {
263    fprintf(stderr, "\n fatal error in IV_setSize(%p,%d)"
264            "\n bad input\n", iv, newsize) ;
265    exit(-1) ;
266 }
267 if ( 0 < iv->maxsize && iv->maxsize < newsize && iv->owned == 0 ) {
268    fprintf(stderr, "\n fatal error in IV_setSize(%p,%d)"
269            "\n iv->maxsize = %d, newsize = %d, iv->owned = %d\n",
270            iv, newsize, iv->maxsize, newsize, iv->owned) ;
271    exit(-1) ;
272 }
273 if ( iv->maxsize < newsize ) {
274 /*
275    -------------------------------------------------------------
276    new size requested is more than maxsize, set new maximum size
277    -------------------------------------------------------------
278 */
279    IV_setMaxsize(iv, newsize) ;
280 }
281 iv->size = newsize ;
282 /*
283 fprintf(stdout,
284         "\n %% leaving IV_setSize, iv %p, size %d, maxsize %d, entries %p",
285         iv, iv->size, iv->maxsize, iv->vec) ;
286 fflush(stdout) ;
287 */
288 
289 return ; }
290 
291 /*--------------------------------------------------------------------*/
292