1 /* basics.c */
2
3 #include "../I2Ohash.h"
4
5 #define MYDEBUG 0
6
7 /*--------------------------------------------------------------------*/
8 /*
9 ------------------------------------------------------
10 create and return a new instance of the I2Ohash object
11
12 created -- 98jan29, cca
13 ------------------------------------------------------
14 */
15 I2Ohash *
I2Ohash_new(void)16 I2Ohash_new (
17 void
18 ) {
19 I2Ohash *hashtable ;
20
21 ALLOCATE(hashtable, struct _I2Ohash, 1) ;
22
23 I2Ohash_setDefaultFields(hashtable) ;
24
25 return(hashtable) ; }
26
27 /*--------------------------------------------------------------------*/
28 /*
29 --------------------------------------------
30 set the default fields for an I2Ohash object
31
32 created -- 98jan29, cca
33 --------------------------------------------
34 */
35 void
I2Ohash_setDefaultFields(I2Ohash * hashtable)36 I2Ohash_setDefaultFields (
37 I2Ohash *hashtable
38 ) {
39 if ( hashtable == NULL ) {
40 fprintf(stderr, "\n fatal error in I2Ohash_setDefaultFields(%p)"
41 "\n hashtable is NULL\n", hashtable) ;
42 exit(-1) ;
43 }
44 hashtable->nlist = 0 ;
45 hashtable->grow = 0 ;
46 hashtable->nitem = 0 ;
47 hashtable->baseI2OP = NULL ;
48 hashtable->freeI2OP = NULL ;
49 hashtable->heads = NULL ;
50
51 return ; }
52
53 /*--------------------------------------------------------------------*/
54 /*
55 -----------------------
56 clear the data fields
57
58 created -- 98jan29, cca
59 -----------------------
60 */
61 void
I2Ohash_clearData(I2Ohash * hashtable)62 I2Ohash_clearData (
63 I2Ohash *hashtable
64 ) {
65 I2OP *i2op ;
66
67 if ( hashtable == NULL ) {
68 fprintf(stderr, "\n fatal error in I2Ohash_clearData(%p)"
69 "\n hashtable is NULL\n", hashtable) ;
70 exit(-1) ;
71 }
72 #if MYDEBUG > 0
73 fprintf(stdout, "\n\n I2Ohash_clearData") ;
74 fflush(stdout) ;
75 #endif
76 while ( (i2op = hashtable->baseI2OP) != NULL ) {
77 #if MYDEBUG > 0
78 fprintf(stdout, "\n baseI2OP = %p", i2op) ;
79 fflush(stdout) ;
80 #endif
81 hashtable->baseI2OP = i2op->next ;
82 I2OP_free(i2op) ;
83 }
84 if ( hashtable->heads != NULL ) {
85 FREE(hashtable->heads) ;
86 }
87 I2Ohash_setDefaultFields(hashtable) ;
88
89 return ; }
90
91 /*--------------------------------------------------------------------*/
92 /*
93 -----------------------
94 free the I2Ohash object
95
96 created -- 98jan29, cca
97 -----------------------
98 */
99 void
I2Ohash_free(I2Ohash * hashtable)100 I2Ohash_free (
101 I2Ohash *hashtable
102 ) {
103 if ( hashtable == NULL ) {
104 fprintf(stderr, "\n fatal error in I2Ohash_free(%p)"
105 "\n hashtable is NULL\n", hashtable) ;
106 exit(-1) ;
107 }
108 I2Ohash_clearData(hashtable) ;
109 FREE(hashtable) ;
110
111 return ; }
112
113 /*--------------------------------------------------------------------*/
114