Adding set and other types
authorhamed <hamed@ubuntu>
Wed, 14 Jun 2017 19:52:43 +0000 (12:52 -0700)
committerhamed <hamed@ubuntu>
Wed, 14 Jun 2017 19:52:43 +0000 (12:52 -0700)
src/Makefile
src/set.cc [new file with mode: 0644]
src/set.h [new file with mode: 0644]
src/types.h [new file with mode: 0644]

index dfc0a8f10242bb98b4d1e9cf6a162628097a14e1..b7a06f8bff3ebc1c04904130690a0682eb88419b 100644 (file)
@@ -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 (file)
index 0000000..2d1c04f
--- /dev/null
@@ -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 <stddef.h>
+#include <cassert>
+
+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; i<num; i++){
+        addItem(elements[i]);
+    }
+}
+
+Set::Set(Type t, uint64 lowrange, uint64 highrange){
+    assert(highrange>lowrange);
+    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 (file)
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 (file)
index 0000000..487fb5f
--- /dev/null
@@ -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 */
+