1 /* init.c */
2
3 #include "../IVL.h"
4
5 #define MYDEBUG 0
6
7 /*--------------------------------------------------------------------*/
8 /*
9 ------------------------------------------------------
10 initialize given the type and maximum number of lists.
11 used for type IVL_CHUNKED, IVL_SOLO or IVL_UNKNOWN
12
13 created -- 95sep22, cca
14 ------------------------------------------------------
15 */
16 void
IVL_init1(IVL * ivl,int type,int maxnlist)17 IVL_init1 (
18 IVL *ivl,
19 int type,
20 int maxnlist
21 ) {
22 /*
23 -------------------
24 check for bad input
25 -------------------
26 */
27 if ( ivl == NULL
28 || (type != IVL_CHUNKED && type != IVL_SOLO && type != IVL_UNKNOWN)
29 || maxnlist < 0 ) {
30 fprintf(stderr, "\n fatal error in IVL_init1(%p,%d,%d)"
31 "\n bad input", ivl, type, maxnlist) ;
32 exit(-1) ;
33 }
34 /*
35 --------------------------
36 clear the data, if present
37 --------------------------
38 */
39 IVL_clearData(ivl) ;
40 /*
41 ----------
42 initialize
43 ----------
44 */
45 ivl->type = type ;
46 ivl->maxnlist = maxnlist ;
47 ivl->nlist = maxnlist ;
48 if ( maxnlist > 0 ) {
49 ivl->sizes = IVinit(maxnlist, 0) ;
50 ivl->p_vec = PIVinit(maxnlist) ;
51 }
52
53 return ; }
54
55 /*--------------------------------------------------------------------*/
56 /*
57 ---------------------------------------------------------------
58 initialize given the type, number of lists and their total size
59 only used when type is IVL_CHUNKED.
60
61 created -- 95sep22, cca
62 ---------------------------------------------------------------
63 */
64 void
IVL_init2(IVL * ivl,int type,int maxnlist,int tsize)65 IVL_init2 (
66 IVL *ivl,
67 int type,
68 int maxnlist,
69 int tsize
70 ) {
71 /*
72 -------------------
73 check for bad input
74 -------------------
75 */
76 if ( ivl == NULL || type != IVL_CHUNKED || maxnlist < 0 ) {
77 fprintf(stderr, "\n fatal error in IVL_init2(%p,%d,%d,%d)"
78 "\n bad input", ivl, type, maxnlist, tsize) ;
79 exit(-1) ;
80 }
81 /*
82 ---------------------------------
83 initialize via IVL_init1() method
84 ---------------------------------
85 */
86 IVL_init1(ivl, type, maxnlist) ;
87 /*
88 ----------------------------------
89 create chunk to hold tsize entries
90 ----------------------------------
91 */
92 if ( tsize > 0 ) {
93 ALLOCATE(ivl->chunk, struct _Ichunk, 1) ;
94 ivl->chunk->size = tsize ;
95 ivl->chunk->inuse = 0 ;
96 ivl->chunk->base = IVinit(tsize, -1) ;
97 ivl->chunk->next = NULL ;
98 }
99 return ; }
100
101 /*--------------------------------------------------------------------*/
102 /*
103 --------------------------------------
104 initialize from a vector of list sizes.
105 used with IVL_SOLO or IVL_CHUNKED.
106
107 created -- 95sep22, cca
108 --------------------------------------
109 */
110 void
IVL_init3(IVL * ivl,int type,int maxnlist,int sizes[])111 IVL_init3 (
112 IVL *ivl,
113 int type,
114 int maxnlist,
115 int sizes[]
116 ) {
117 int ilist ;
118 /*
119 -------------------
120 check for bad input
121 -------------------
122 */
123 if ( ivl == NULL || (type != IVL_CHUNKED && type != IVL_SOLO)
124 || maxnlist < 0 || sizes == NULL ) {
125 fprintf(stderr,
126 "\n fatal error in IVL_init3(%p,%d,%d,%p)"
127 "\n bad input", ivl, type, maxnlist, sizes) ;
128 exit(-1) ;
129 }
130 switch ( type ) {
131 case IVL_SOLO :
132 /*
133 ---------------------------------
134 initialize via IVL_init1() method
135 ---------------------------------
136 */
137 IVL_init1(ivl, type, maxnlist) ;
138 break ;
139 case IVL_CHUNKED :
140 /*
141 ---------------------------------
142 initialize via IVL_init2() method
143 ---------------------------------
144 */
145 IVL_init2(ivl, type, maxnlist, IVsum(maxnlist, sizes)) ;
146 break ;
147 }
148 /*
149 -------------------------
150 set the size of each list
151 -------------------------
152 */
153 for ( ilist = 0 ; ilist < maxnlist ; ilist++ ) {
154 IVL_setList(ivl, ilist, sizes[ilist], NULL) ;
155 }
156
157 return ; }
158
159 /*--------------------------------------------------------------------*/
160 /*
161 ------------------------------------------------
162 this method resizes the maximum number of lists,
163 replacing the old sizes[] and p_vec[] vectors
164 as necessary. the nlist value is NOT reset.
165
166 created -- 96dec05, cca
167 ------------------------------------------------
168 */
169 void
IVL_setMaxnlist(IVL * ivl,int newmaxnlist)170 IVL_setMaxnlist (
171 IVL *ivl,
172 int newmaxnlist
173 ) {
174 if ( ivl == NULL || newmaxnlist < 0 ) {
175 fprintf(stderr, "\n fatal error in IVL_setMaxnlist(%p,%d)"
176 "\n bad input\n", ivl, newmaxnlist) ;
177 exit(-1) ;
178 }
179 if ( newmaxnlist != ivl->maxnlist ) {
180 int *ivec, **pivec ;
181 /*
182 --------------------------------------------
183 allocate (and fill) the new sizes[] array
184 --------------------------------------------
185 */
186 ivec = IVinit(newmaxnlist, 0) ;
187 if ( ivl->sizes != NULL ) {
188 if ( ivl->nlist > newmaxnlist ) {
189 IVcopy(newmaxnlist, ivec, ivl->sizes) ;
190 } else if ( ivl->nlist > 0 ) {
191 IVcopy(ivl->nlist, ivec, ivl->sizes) ;
192 }
193 IVfree(ivl->sizes) ;
194 }
195 ivl->sizes = ivec ;
196 /*
197 --------------------------------------------
198 allocate (and fill) the larger p_vec[] array
199 --------------------------------------------
200 */
201 pivec = PIVinit(newmaxnlist) ;
202 if ( ivl->p_vec != NULL ) {
203 if ( ivl->nlist > newmaxnlist ) {
204 PIVcopy(newmaxnlist, pivec, ivl->p_vec) ;
205 } else if ( ivl->nlist > 0 ) {
206 PIVcopy(ivl->nlist, pivec, ivl->p_vec) ;
207 }
208 PIVfree(ivl->p_vec) ;
209 }
210 ivl->p_vec = pivec ;
211 /*
212 -----------------------------------
213 set the new maximum number of lists
214 -----------------------------------
215 */
216 ivl->maxnlist = newmaxnlist ;
217 if ( ivl->nlist > newmaxnlist ) {
218 ivl->nlist = newmaxnlist ;
219 }
220 }
221 return ; }
222
223 /*--------------------------------------------------------------------*/
224 /*
225 ------------------------------------------------
226 this method resizes the number of lists,
227 replacing the old sizes[] and p_vec[] vectors
228 as necessary.
229
230 created -- 96dec05, cca
231 ------------------------------------------------
232 */
233 void
IVL_setNlist(IVL * ivl,int newnlist)234 IVL_setNlist (
235 IVL *ivl,
236 int newnlist
237 ) {
238 if ( ivl == NULL || newnlist < 0 ) {
239 fprintf(stderr, "\n fatal error in IVL_setNlist(%p,%d)"
240 "\n bad input\n", ivl, newnlist) ;
241 exit(-1) ;
242 }
243 if ( newnlist > ivl->maxnlist ) {
244 /*
245 ------------------------------------
246 increase the maximum number of lists
247 ------------------------------------
248 */
249 IVL_setMaxnlist(ivl, newnlist) ;
250 }
251 /*
252 -------------------
253 set the nlist field
254 -------------------
255 */
256 ivl->nlist = newnlist ;
257
258 return ; }
259
260 /*--------------------------------------------------------------------*/
261