From 88f13db35ead73ea714992884457dfd26d1c6fe0 Mon Sep 17 00:00:00 2001 From: hamed Date: Wed, 14 Jun 2017 12:52:43 -0700 Subject: [PATCH 1/1] Adding set and other types --- src/Makefile | 2 +- src/set.cc | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/set.h | 67 +++++++++++++++++++++++++++++++++++++ src/types.h | 34 +++++++++++++++++++ 4 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 src/set.cc create mode 100644 src/set.h create mode 100644 src/types.h diff --git a/src/Makefile b/src/Makefile index dfc0a8f..b7a06f8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -4,7 +4,7 @@ PHONY += directories MKDIR_P = mkdir -p OBJ_DIR = bin -CPP_SOURCES := constraint.cc inc_solver.cc +CPP_SOURCES := constraint.cc inc_solver.cc set.cc OBJECTS := $(CPP_SOURCES:%.cc=$(OBJ_DIR)/%.o) $(C_SOURCES:%.c=$(OBJ_DIR)/%.o) diff --git a/src/set.cc b/src/set.cc new file mode 100644 index 0000000..2d1c04f --- /dev/null +++ b/src/set.cc @@ -0,0 +1,94 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +#include "set.h" +#include +#include + +Node::Node(uint64 val, Node* r, Node* l){ + range=false; + beginOrVal=val; + left = l; + right=r; +} + +Node::Node(uint64 val):Node(val, NULL, NULL){} + +Node::Node(uint64 begin, uint64 end, Node* r, Node* l){ + range=true; + beginOrVal=begin; + this->end= end; + right=r; + left=l; +} + +Node::Node(uint64 begin, uint64 end):Node(begin, end, NULL, NULL){} + +Comparison Node::compare(uint64 val){ + if(range){ + if(val> end) + return GREATER; + else if(val < beginOrVal) + return LESS; + else + return EQUAL; + }else{ + if(val> beginOrVal) + return GREATER; + else if(val< beginOrVal) + return LESS; + else + return EQUAL; + } +} + +void Node::addNode(Node* n){ + assert(!n->isRange()); + Comparison comp = compare(n->getBeginOrRange()); + if(comp == GREATER){ + if(right!=NULL) + right->addNode(n); + else + right=n; + }else if(comp == LESS){ + if(left!= NULL) + left->addNode(n); + else + left = n; + } + +} + +Set::Set(Type t, uint64* elements, int num){ + type=t; + size=num; + for(int i=0; ilowrange); + type =t; + size = highrange-lowrange+1; + root = new Node(lowrange,highrange); +} + +Set::Set(Type t): type(t), + size(0), + root(NULL) +{ +} + +void Set::addItem(uint64 element){ + Node* n = new Node(element); + if(root==NULL) + root=n; + else + root->addNode(n); +} + + \ No newline at end of file diff --git a/src/set.h b/src/set.h new file mode 100644 index 0000000..81b2dcc --- /dev/null +++ b/src/set.h @@ -0,0 +1,67 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: set.h + * Author: hamed + * + * Created on June 13, 2017, 3:01 PM + */ + +#ifndef SET_H +#define SET_H + +#include "types.h" + +enum Comparison{LESS, EQUAL, GREATER}; + +class Node{ +private: + bool range; + // If it isn't a range, begin contains the actual value of the element + uint64 beginOrVal; + uint64 end; + Node* right; + Node* left; +public: + Node(uint64 val, Node* r, Node* l); + Node(uint64 val); + Node(uint64 begin, uint64 end, Node* r, Node* l); + Node(uint64 begin, uint64 end); + bool isRange(){return range;} + uint64 getBeginOrRange(){return beginOrVal;} + Comparison compare(uint64 val); + /** + * Searches the tree, if new node exists in the tree ( whether its value + * is in range of another node, or there is another node with the value of + * node n) this function just returns! + * @param n + */ + void addNode(Node* n); +}; +// For now, we can consider it as a simple binary tree, but we can have fancier +// trees for future +class Set{ + Type type; + uint64 size; + Node* root; + Set(Type t, uint64 * elements, int num); + Set(Type t, uint64 lowrange, uint64 highrange); + Set(Type t); + /** + * For know all sets are considered to be mutable, we can change it later on + * if it was necessary. + * @param set + * @param element + */ + void addItem(uint64 element); + ELEMENT* createUniqueItem(Set * set); + +}; + + +#endif /* SET_H */ + diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..487fb5f --- /dev/null +++ b/src/types.h @@ -0,0 +1,34 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/* + * File: Type.h + * Author: hamed + * + * Created on June 13, 2017, 1:33 PM + */ + +#ifndef TYPE_H +#define TYPE_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned long int uint64; +typedef uint64 Type; + +struct Element{ + uint64 value; +}; + +typedef struct Element ELEMENT; +#ifdef __cplusplus +} +#endif + +#endif /* TYPE_H */ + -- 2.34.1