-
Notifications
You must be signed in to change notification settings - Fork 0
/
adt.c
49 lines (42 loc) · 1.11 KB
/
adt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "adt.h"
// This implements a base abstract data type.
// Think of this as class Object.
// forward declaration of method implementations
uint32_t lfsrHash(void);
uint32_t adtHash(csAdtRef self);
bool adtEquals(csAdtRef self, csAdtRef other);
char * adtDescription(csAdtRef self);
// declare the method structure for csAdtMethods
csAdtMethods adtMethods = {
adtHash,
adtEquals,
adtDescription,
};
// declare csAdt constructor function
csAdtRef newCsAdt(){
csAdtRef anAdtRef = (csAdtRef)malloc(sizeof(csAdt));
if(anAdtRef == (csAdtRef)NULL){
return (csAdtRef)NULL;
}
anAdtRef->methods = &adtMethods;
anAdtRef->hash = lfsrHash();
return anAdtRef;
}
// nextHash()
// Generate 32 bit hash using a linear feedback shift register.
// feedback polynomial: x^32 + x^31 + x^29 + x + 1
// taps: 32 31 29 1
uint32_t lfsrHash(void){
static uint32_t lfsr = 1;
lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xD0000001u);
return lfsr;
}
uint32_t adtHash(csAdtRef self){
return self->hash;
}
bool adtEquals(csAdtRef self, csAdtRef other){
return self == other;
}
char * adtDescription(csAdtRef self){
return "an ADT";
}