| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#include "set.h" |
| 14 |
|
| 15 |
|
| 16 |
#include "lists.h" |
| 17 |
#include "syserr.h" |
| 18 |
#include "memory.h" |
| 19 |
#include "instance.h" |
| 20 |
|
| 21 |
#define EXTENT 5 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Set *newSet(int allocation) { |
| 26 |
Set *theSet = NEW(Set); |
| 27 |
|
| 28 |
if (allocation) { |
| 29 |
theSet->members = allocate(allocation*sizeof(theSet->members[0])); |
| 30 |
theSet->size = 0; |
| 31 |
theSet->allocated = allocation; |
| 32 |
} |
| 33 |
return theSet; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
void initSets(SetInitEntry *initTable) |
| 39 |
{ |
| 40 |
SetInitEntry *init; |
| 41 |
int i; |
| 42 |
|
| 43 |
for (init = initTable; !isEndOfArray(init); init++) { |
| 44 |
Set *set = newSet(init->size); |
| 45 |
Aword *member = pointerTo(init->setAddress); |
| 46 |
for (i = 0; i < init->size; i++, member++) |
| 47 |
addToSet(set, *member); |
| 48 |
setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(set)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
int setSize(Set *theSet) { |
| 55 |
return theSet->size; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void clearSet(Set *theSet) { |
| 61 |
theSet->size = 0; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Set *copySet(Set *theSet) { |
| 67 |
Set *new = newSet(theSet->size); |
| 68 |
int i; |
| 69 |
|
| 70 |
for (i = 1; i <= theSet->size; i++) |
| 71 |
addToSet(new, getSetMember(theSet, i)); |
| 72 |
return new; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
Aword getSetMember(Set *theSet, Aint theMember) { |
| 78 |
if (theMember > theSet->size || theMember < 1) |
| 79 |
apperr("Accessing nonexisting member in a set"); |
| 80 |
return theSet->members[theMember-1]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
bool inSet(Set *theSet, Aword member) |
| 86 |
{ |
| 87 |
int i; |
| 88 |
|
| 89 |
for (i = 1; i <= theSet->size; i++) |
| 90 |
if (getSetMember(theSet, i) == member) |
| 91 |
return true; |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
Set *setUnion(Set *set1, Set *set2) |
| 98 |
{ |
| 99 |
Set *theUnion = newSet(set1->size+set2->size); |
| 100 |
int i; |
| 101 |
|
| 102 |
for (i = 0; i < set1->size; i++) |
| 103 |
addToSet(theUnion, set1->members[i]); |
| 104 |
for (i = 0; i < set2->size; i++) |
| 105 |
addToSet(theUnion, set2->members[i]); |
| 106 |
return theUnion; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
void addToSet(Set *theSet, Aword newMember) |
| 112 |
{ |
| 113 |
if (inSet(theSet, newMember)) return; |
| 114 |
if (theSet->size == theSet->allocated) { |
| 115 |
theSet->allocated += EXTENT; |
| 116 |
theSet->members = realloc(theSet->members, theSet->allocated*sizeof(theSet->members[0])); |
| 117 |
} |
| 118 |
theSet->members[theSet->size] = newMember; |
| 119 |
theSet->size++; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void removeFromSet(Set *theSet, Aword member) |
| 125 |
{ |
| 126 |
int i, j; |
| 127 |
|
| 128 |
if (!inSet(theSet, member)) return; |
| 129 |
|
| 130 |
for (i = 0; i < theSet->size; i++) { |
| 131 |
if ((Aword)theSet->members[i] == member) { |
| 132 |
for (j = i; j < theSet->size-1; j++) |
| 133 |
theSet->members[j] = theSet->members[j+1]; |
| 134 |
theSet->size--; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
bool equalSets(Set *set1, Set *set2) |
| 143 |
{ |
| 144 |
int i; |
| 145 |
|
| 146 |
if (set1->size != set2->size) return false; |
| 147 |
|
| 148 |
for (i = 0; i < set1->size; i++) { |
| 149 |
if (!inSet(set2, set1->members[i])) |
| 150 |
return false; |
| 151 |
} |
| 152 |
return true; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
void freeSet(Set *theSet) { |
| 158 |
if (theSet != NULL) { |
| 159 |
if (theSet->members != NULL) |
| 160 |
deallocate(theSet->members); |
| 161 |
deallocate(theSet); |
| 162 |
} |
| 163 |
} |