DAARRAY 22-SEP-1995 18:30:50 VAX C V3.2-044 Page 1 V1.0 26-MAR-1993 13:52:10 [GOPHER.G2.VMS2_13.OBJECT]DAARRAY.C;1 (1) 1 /******************************************************************** 2 * lindner 3 * 3.2 4 * 1993/03/26 19:52:10 5 * /home/arcwelder/GopherSrc/CVS/gopher+/object/DAarray.c,v 6 * Exp 7 * 8 * Paul Lindner, University of Minnesota CIS. 9 * 10 * Copyright 1991, 1992 by the Regents of the University of Minnesota 11 * see the file "Copyright" in the distribution for conditions of use. 12 ********************************************************************* 13 * MODULE: DAarray.c 14 * Dynamic Array Implementation 15 ********************************************************************* 16 * Revision History: 17 * DAarray.c,v 18 * Revision 3.2 1993/03/26 19:52:10 lindner 19 * Fixed off by one prob in DApop 20 * 21 * Revision 3.1.1.1 1993/02/11 18:03:01 lindner 22 * Gopher+1.2beta release 23 * 24 * Revision 1.3 1992/12/21 21:14:57 lindner 25 * Fixed bug in DAcpy, initfn wasn't being copied right. 26 * 27 * Revision 1.2 1992/12/21 20:04:04 lindner 28 * Added DAcpy() 29 * 30 * Revision 1.1 1992/12/10 23:27:52 lindner 31 * gopher 1.1 release 32 * 33 * 34 *********************************************************************/ 35 36 37 #include "DAarray.h" 106 107 #include "Malloc.h" 1130 1131 /* 1132 * Create a new dynamic array 1133 * 1134 * size -- the initial number of elements 1135 * newfn -- creates new object 1136 * initfn -- initializes the object 1137 * destroyfn -- performs clean up and frees objs memory 1138 * copyfn -- copys one obj to the other, like strcpy 1139 */ 1140 1141 DynArray * 1142 DAnew(size, newfn, initfn, destroyfn, copyfn) 1143 int size; 1144 char * (*newfn)(); 1145 void (*initfn)(); DAARRAY 22-SEP-1995 18:30:50 VAX C V3.2-044 Page 2 V1.0 26-MAR-1993 13:52:10 [GOPHER.G2.VMS2_13.OBJECT]DAARRAY.C;1 (1) 1146 void (*destroyfn)(); 1147 char * (*copyfn)(); 1148 { 1149 1 DynArray *temp; 1150 1 int i; 1151 1 1152 1 temp = (DynArray*) malloc(sizeof(DynArray)); 1153 1 temp->objects = (char **) malloc(size * sizeof(char *)); 1154 1 temp->newfn = newfn; 1155 1 temp->initfn = initfn; 1156 1 temp->destroyfn = destroyfn; 1157 1 temp->copyfn = copyfn; 1158 1 temp->Top = 0; 1159 1 1160 1 if (copyfn == NULL) 1161 1 /** Can't work without this!!! ***/ 1162 1 perror("Egad, no copy function in DAnew()!!"); 1163 1 1164 1 if (newfn != NULL) 1165 1 for (i = 0; i < size; i++) 1166 1 temp->objects[i] = (char *) newfn(); 1167 1 1168 1 temp->maxsize = size; 1169 1 return(temp); 1170 1 } 1171 1172 1173 void DAdestroy(da) 1174 DynArray *da; 1175 { 1176 1 int i; 1177 1 1178 1 if (da->destroyfn != NULL) 1179 1 for (i = 0; i< da->maxsize; i++) 1180 1 da->destroyfn(da->objects[i]); 1181 1 1182 1 free(da->objects); 1183 1 free(da); 1184 1 } 1185 1186 1187 void 1188 DAinit(da) 1189 DynArray *da; 1190 { 1191 1 int i; 1192 1 1193 1 if (da->initfn!=NULL) 1194 1 for (i=0; imaxsize; i++) 1195 1 da->initfn(DAgetEntry(da, i)); 1196 1 1197 1 DAsetTop(da, 0); 1198 1 } 1199 1200 void 1201 DAgrow(da, size) 1202 DynArray *da; DAARRAY 22-SEP-1995 18:30:50 VAX C V3.2-044 Page 3 V1.0 26-MAR-1993 13:52:10 [GOPHER.G2.VMS2_13.OBJECT]DAARRAY.C;1 (1) 1203 int size; 1204 { 1205 1 char **temp; 1206 1 int i; 1207 1 1208 1 if (size < da->maxsize) 1209 1 return; /** Size is smaller than requested **/ 1210 1 1211 1 temp = (char **) realloc(da->objects, size*sizeof(char*)); 1212 1 1213 1 if (temp == NULL) 1214 1 perror("Out of memory!!!\n"), exit(-1); 1215 1 1216 1 if (temp != da->objects) { 1217 2 da->objects = temp; 1218 2 } 1219 1 1220 1 /** Initialize the new objects. **/ 1221 1 1222 1 for (i= da->maxsize; i< size; i++) 1223 1 da->objects[i] = da->newfn(); 1224 1 1225 1 da->maxsize = size; 1226 1 return; 1227 1 } 1228 1229 /* 1230 * Tacks an item on the end of the array, grows it if necessary.. 1231 */ 1232 1233 void 1234 DApush(da, obj) 1235 DynArray *da; 1236 char *obj; 1237 { 1238 1 int top; 1239 1 1240 1 top = DAgetTop(da); 1241 1 1242 1 if (top == da->maxsize) 1243 1 DAgrow(da, da->maxsize*2); 1244 1 1245 1 da->copyfn(DAgetEntry(da, top), obj); 1246 1 1247 1 DAsetTop(da, top+1); /* update end of list */ 1248 1 } 1249 1250 char * 1251 DApop(da) 1252 DynArray *da; 1253 { 1254 1 int top; 1255 1 char *newobj; 1256 1 1257 1 top = DAgetTop(da); 1258 1 1259 1 if (top == 0) DAARRAY 22-SEP-1995 18:30:50 VAX C V3.2-044 Page 4 V1.0 26-MAR-1993 13:52:10 [GOPHER.G2.VMS2_13.OBJECT]DAARRAY.C;1 (1) 1260 1 return(NULL); /** Nothing to pop! **/ 1261 1 1262 1 newobj = da->newfn(); 1263 1 da->initfn(newobj); 1264 1 1265 1 top--; 1266 1 da->copyfn(newobj, DAgetEntry(da, top)); 1267 1 1268 1 DAsetTop(da, top); 1269 1 1270 1 return(newobj); 1271 1 } 1272 1273 void 1274 DAsort(da, sortfn) 1275 DynArray *da; 1276 int (*sortfn)(); 1277 { 1278 1 char *moo; 1279 1 1280 1 moo = (char *) &((da->objects)[0]); 1281 1 1282 1 qsort(moo, da->Top, 1283 1 sizeof(char *), sortfn); 1284 1 } 1285 1286 1287 void 1288 DAcpy(dest, orig) 1289 DynArray *dest; 1290 DynArray *orig; 1291 { 1292 1 int i; 1293 1 1294 1 DAsetTop(dest, 0); 1295 1 dest->newfn = orig->newfn; 1296 1 dest->initfn = orig->initfn; 1297 1 dest->destroyfn = orig->destroyfn; 1298 1 dest->copyfn = dest->copyfn; 1299 1 1300 1 for (i=0; i 1307 X #include 1308 X 1309 X char *makespace() 1310 X { 1311 X return(malloc(256)); 1312 X } 1313 X 1314 X int DEBUG = 1; 1315 X 1316 X int DAARRAY 22-SEP-1995 18:30:50 VAX C V3.2-044 Page 5 V1.0 26-MAR-1993 13:52:10 [GOPHER.G2.VMS2_13.OBJECT]DAARRAY.C;1 (1) 1317 X moostrcmp(s1, s2) 1318 X char **s1, **s2; 1319 X { 1320 X printf("Comparing %s, %s\n", *s1,*s2); 1321 X return(strcmp(*s1,*s2)); 1322 X } 1323 X 1324 X main() 1325 X { 1326 X DynArray *test; 1327 X int i; 1328 X char tempstr[100]; 1329 X 1330 X printf("Testing Dynamic Arrays...\n\n"); 1331 X 1332 X test = DAnew(5, makespace, NULL, NULL, strcpy); 1333 X 1334 X DApush(test, "ziggy\n"); 1335 X DApush(test, "iggy\n"); 1336 X 1337 X for (i= 10; i >0; i--) { 1338 X sprintf(tempstr, "Moocow #%d\n", i); 1339 X printf(tempstr); 1340 X DApush(test, tempstr); 1341 X } 1342 X 1343 X for (i=0; i< 10; i++) { 1344 X printf(DAgetEntry(test, i)); 1345 X } 1346 X 1347 X DAsort(test, moostrcmp); 1348 X 1349 X for (i=0; i< 10; i++) { 1350 X printf(DAgetEntry(test, i)); 1351 X } 1352 X 1353 X } 1354 X 1355 #endif Command Line ------------ CC/INCL=([-],[-.OBJECT])/DEFINE=(MULTINET=1,DEBUGGING,__VMS)/DEBUG/NOOPT/OBJ=[.VAX.DBG]/LIS=[.VAX.LIS] DAARRAY.C .