-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
91 lines (71 loc) · 2.88 KB
/
tree.h
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
Testbed for empirical evaluation of KP-ABE schemes, according to Crampton, Pinto (CSF2014).
Code by: Alexandre Miranda Pinto
This file declares the classes necessary to implement a tree data structure specific for the secret sharing.
The basic class is TreeNode, which defines the framework for the interaction between nodes of the tree.
The actual data of the tree is held in tree nodes, which are implemented by the class NodeContent.
*/
#define DEF_TREE
#ifndef DEF_UTILS
#include "utils.h"
#endif
enum InnerNodeType {OR, AND, THR};
enum NodeContentType {inner, leaf, nil};
class NodeContent {
NodeContent();
NodeContent(int leafValue);
NodeContent(InnerNodeType nodeType, int arg1);
NodeContent(InnerNodeType nodeType, int arg1, int arg2);
NodeContentType m_type;
union{
int m_leafValue;
struct{ // inner nodes usually need some more values besides their type
InnerNodeType type;
int arg1; // This represents arity. It is used for AND, OR and Threshold nodes.
int arg2; // This represents threshold. It is used only for threshold nodes.
} m_innerNode;
};
public:
static std::shared_ptr<NodeContent> makeNILNode();
static std::shared_ptr<NodeContent> makeOrNode(int arity);
static std::shared_ptr<NodeContent> makeAndNode(int arity);
static std::shared_ptr<NodeContent> makeThreshNode(int arity, int threshold);
static std::shared_ptr<NodeContent> makeLeafNode(int leafValue);
bool operator==(const NodeContent& rhs) const;
NodeContentType getType();
int getLeafValue();
InnerNodeType getInnerNodeType();
unsigned int getThreshold();
unsigned int getArity();
string to_string();
};
//===========================================
class TreeNode {
std::shared_ptr<NodeContent> m_node;
std::string m_nodeID;
// m_parent;
vector<std::shared_ptr<TreeNode> > m_children; // vector of pointers for TreeNodes.
TreeNode(std::shared_ptr<NodeContent> node);
std::string initNodeID(shared_ptr<NodeContent> node);
public:
TreeNode();
static std::shared_ptr<TreeNode> makeTree(std::shared_ptr<NodeContent> node);
std::shared_ptr<NodeContent> getNode();
std::shared_ptr<TreeNode> getChild(unsigned int i);
bool operator==(const TreeNode& rhs) const;
TreeNode& operator=(const TreeNode& rhs);
bool appendChild(std::shared_ptr<NodeContent> node);
bool appendTree(std::shared_ptr<TreeNode> tree);
std::string full_to_string();
std::string to_string();
unsigned int getNumLeaves();
unsigned int getNumChildren();
void updateID(std::string parentID, int count);
std::string getNodeID() const;
void setNodeID(std::string nodeID);
bool isLeaf();
bool isInner();
bool isNil();
static std::string findIDForNode(const std::string& parent, int childno, shared_ptr<NodeContent> node);
static std::string findIDForNode(const std::string& parent, int childno, NodeContentType type, const std::string& value = "");
};