2569ed28b6c140e9868f4df5f2e7087370f2c72b
[satune.git] / src / AST / set.c
1 #include "set.h"
2 #include <stddef.h>
3
4 Set * allocSet(VarType t, uint64_t* elements, uint num) {
5         Set * This=(Set *)ourmalloc(sizeof(Set));
6         This->type=t;
7         This->isRange=false;
8         This->low=0;
9         This->high=0;
10         This->members=allocVectorArrayInt(num, elements);
11         return This;
12 }
13
14 Set * allocSetRange(VarType t, uint64_t lowrange, uint64_t highrange) {
15         Set * This=(Set *)ourmalloc(sizeof(Set));
16         This->type=t;
17         This->isRange=true;
18         This->low=lowrange;
19         This->high=highrange;
20         This->members=NULL;
21         return This;
22 }
23
24 bool existsInSet(Set* This, uint64_t element){
25         if(This->isRange){
26                 return element >= This->low && element <= This->high;
27         } else {
28                 uint size = getSizeVectorInt(This->members);
29                 for(uint i=0; i< size; i++){
30                         if(element == getVectorInt(This->members, i))
31                                 return true;
32                 }
33                 return false;
34         }
35 }
36
37 uint64_t getSetElement(Set * This, uint index) {
38         if (This->isRange)
39                 return This->low+index;
40         else
41                 return getVectorInt(This->members, index);
42 }
43
44 uint getSetSize(Set* This){
45         if(This->isRange){
46                 return This->high - This->low+1;
47         }else{
48                 return getSizeVectorInt(This->members);
49         }
50 }
51
52 void deleteSet(Set * This) {
53         if (!This->isRange)
54                 deleteVectorInt(This->members);
55         ourfree(This);
56 }