00001
00002
00003
00004
00005 #define POINTERHASH_C
00006 #include "PointerSetHash.h"
00007 #undef POINTERHASH_C
00008 #include <stdlib.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <assert.h>
00012
00013
00014 PointerSetHash *PointerSetHash_new(void)
00015 {
00016 PointerSetHash *self = (PointerSetHash *)io_calloc(1, sizeof(PointerSetHash));
00017 PointerSetHash_setSize_(self, 8);
00018 return self;
00019 }
00020
00021 void PointerSetHash_copy_(PointerSetHash *self, const PointerSetHash *other)
00022 {
00023 io_free(self->records);
00024 memcpy(self, other, sizeof(PointerSetHash));
00025 self->records = malloc(self->size * sizeof(PointerSetHashRecord));
00026 memcpy(self->records, other->records, self->size * sizeof(PointerSetHashRecord));
00027 }
00028
00029 PointerSetHash *PointerSetHash_clone(PointerSetHash *self)
00030 {
00031 PointerSetHash *other = PointerSetHash_new();
00032 PointerSetHash_copy_(other, self);
00033 return other;
00034 }
00035
00036 void PointerSetHash_setSize_(PointerSetHash *self, size_t size)
00037 {
00038 self->records = realloc(self->records, size * sizeof(PointerSetHashRecord));
00039
00040 if(size > self->size)
00041 {
00042 memset(self->records + self->size * sizeof(PointerSetHashRecord),
00043 0x0, (size - self->size) * sizeof(PointerSetHashRecord));
00044 }
00045
00046 self->size = size;
00047
00048 PointerSetHash_updateMask(self);
00049 }
00050
00051 void PointerSetHash_updateMask(PointerSetHash *self)
00052 {
00053 self->mask = (intptr_t)(self->size - 1);
00054 }
00055
00056 void PointerSetHash_show(PointerSetHash *self)
00057 {
00058 size_t i;
00059
00060 printf("PointerSetHash records:\n");
00061 for(i = 0; i < self->size; i++)
00062 {
00063 PointerSetHashRecord *r = PointerSetHashRecords_recordAt_(self->records, i);
00064 printf(" %i: %p\n", (int)i, r->k);
00065 }
00066 }
00067
00068 void PointerSetHash_free(PointerSetHash *self)
00069 {
00070 io_free(self->records);
00071 io_free(self);
00072 }
00073
00074 void PointerSetHash_insert_(PointerSetHash *self, PointerSetHashRecord *x)
00075 {
00076 int n;
00077
00078 for (n = 0; n < POINTERHASH_MAXLOOP; n ++)
00079 {
00080 PointerSetHashRecord *r;
00081
00082 r = PointerSetHash_record1_(self, x->k);
00083 PointerSetHashRecord_swapWith_(x, r);
00084 if(x->k == 0x0) { self->keyCount ++; return; }
00085
00086 r = PointerSetHash_record2_(self, x->k);
00087 PointerSetHashRecord_swapWith_(x, r);
00088 if(x->k == 0x0) { self->keyCount ++; return; }
00089 }
00090
00091 PointerSetHash_grow(self);
00092 PointerSetHash_at_put_(self, x->k);
00093 }
00094
00095 void PointerSetHash_insertRecords(PointerSetHash *self, unsigned char *oldRecords, size_t oldSize)
00096 {
00097 size_t i;
00098
00099 for (i = 0; i < oldSize; i ++)
00100 {
00101 PointerSetHashRecord *r = PointerSetHashRecords_recordAt_(oldRecords, i);
00102
00103 if (r->k)
00104 {
00105 PointerSetHash_at_put_(self, r->k);
00106 }
00107 }
00108 }
00109
00110 void PointerSetHash_resizeTo_(PointerSetHash *self, size_t newSize)
00111 {
00112 unsigned char *oldRecords = self->records;
00113 size_t oldSize = self->size;
00114 self->size = newSize;
00115 self->records = io_calloc(1, sizeof(PointerSetHashRecord) * self->size);
00116 self->keyCount = 0;
00117 PointerSetHash_updateMask(self);
00118 PointerSetHash_insertRecords(self, oldRecords, oldSize);
00119 io_free(oldRecords);
00120 }
00121
00122 void PointerSetHash_grow(PointerSetHash *self)
00123 {
00124 PointerSetHash_resizeTo_(self, self->size * 2);
00125 }
00126
00127 void PointerSetHash_shrink(PointerSetHash *self)
00128 {
00129 PointerSetHash_resizeTo_(self, self->size / 2);
00130 }
00131
00132 void PointerSetHash_removeKey_(PointerSetHash *self, void *k)
00133 {
00134 PointerSetHashRecord *r;
00135
00136 r = PointerSetHash_record1_(self, k);
00137 if(r->k == k)
00138 {
00139 r->k = 0x0;
00140 self->keyCount --;
00141 PointerSetHash_shrinkIfNeeded(self);
00142 return;
00143 }
00144
00145 r = PointerSetHash_record2_(self, k);
00146 if(r->k == k)
00147 {
00148 r->k = 0x0;
00149 self->keyCount --;
00150 PointerSetHash_shrinkIfNeeded(self);
00151 return;
00152 }
00153 }
00154
00155 size_t PointerSetHash_size(PointerSetHash *self)
00156 {
00157 return self->keyCount;
00158 }
00159
00160
00161
00162 size_t PointerSetHash_memorySize(PointerSetHash *self)
00163 {
00164 return sizeof(PointerSetHash) + self->size * sizeof(PointerSetHashRecord);
00165 }
00166
00167 void PointerSetHash_compact(PointerSetHash *self)
00168 {
00169 }