SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TPredicate Class Reference

Predicate - encapsulates comparison operations. More...

#include <table.h>

Public Member Functions

 TPredicate ()
 Default constructor. More...
 
 TPredicate (TPredicateNode *R)
 Construct predicate with given root node R. More...
 
 TPredicate (const TPredicate &Pred)
 Copy constructor. More...
 
void GetVariables (TStrV &Variables)
 Get variables in current predicate. More...
 
void SetIntVal (TStr VarName, TInt VarVal)
 Set int variable value in the predicate or all the children that use it. More...
 
void SetFltVal (TStr VarName, TFlt VarVal)
 Set flt variable value in the predicate or all the children that use it. More...
 
void SetStrVal (TStr VarName, TStr VarVal)
 Set str variable value in the predicate or all the children that use it. More...
 
TBool Eval ()
 Return the result of evaluating current predicate. More...
 
TBool EvalAtomicPredicate (const TAtomicPredicate &Atom)
 Evaluate the give atomic predicate. More...
 

Static Public Member Functions

template<class T >
static TBool EvalAtom (T Val1, T Val2, TPredComp Cmp)
 Compare atomic values Val1 and Val2 using predicate Cmp. More...
 
static TBool EvalStrAtom (const TStr &Val1, const TStr &Val2, TPredComp Cmp)
 Compare atomic string values Val1 and Val2 using predicate Cmp. More...
 

Protected Attributes

THash< TStr, TIntIntVars
 Int variables in the current predicate tree. More...
 
THash< TStr, TFltFltVars
 Float variables in the current predicate tree. More...
 
THash< TStr, TStrStrVars
 String variables in the current predicate tree. More...
 
TPredicateNodeRoot
 Rood node of the current predicate tree. More...
 

Detailed Description

Predicate - encapsulates comparison operations.

Definition at line 82 of file table.h.

Constructor & Destructor Documentation

TPredicate::TPredicate ( )
inline

Default constructor.

Definition at line 90 of file table.h.

90 : IntVars(), FltVars(), StrVars() {}
THash< TStr, TInt > IntVars
Int variables in the current predicate tree.
Definition: table.h:84
THash< TStr, TStr > StrVars
String variables in the current predicate tree.
Definition: table.h:86
THash< TStr, TFlt > FltVars
Float variables in the current predicate tree.
Definition: table.h:85
TPredicate::TPredicate ( TPredicateNode R)
inline

Construct predicate with given root node R.

Definition at line 92 of file table.h.

92 : IntVars(), FltVars(), StrVars(), Root(R) {}
THash< TStr, TInt > IntVars
Int variables in the current predicate tree.
Definition: table.h:84
THash< TStr, TStr > StrVars
String variables in the current predicate tree.
Definition: table.h:86
TPredicateNode * Root
Rood node of the current predicate tree.
Definition: table.h:87
THash< TStr, TFlt > FltVars
Float variables in the current predicate tree.
Definition: table.h:85
TPredicate::TPredicate ( const TPredicate Pred)
inline

Copy constructor.

Definition at line 94 of file table.h.

94 : IntVars(Pred.IntVars), FltVars(Pred.FltVars), StrVars(Pred.StrVars), Root(Pred.Root) {}
THash< TStr, TInt > IntVars
Int variables in the current predicate tree.
Definition: table.h:84
THash< TStr, TStr > StrVars
String variables in the current predicate tree.
Definition: table.h:86
TPredicateNode * Root
Rood node of the current predicate tree.
Definition: table.h:87
THash< TStr, TFlt > FltVars
Float variables in the current predicate tree.
Definition: table.h:85

Member Function Documentation

TBool TPredicate::Eval ( )

Return the result of evaluating current predicate.

Definition at line 14 of file table.cpp.

14  {
15  TPredicateNode* Curr = Root;
16  TPredicateNode* Prev = NULL;
17  while (!(Curr == NULL && Prev == Root)) {
18  // going down the tree
19  if (Prev == NULL || Prev == Curr->Parent) {
20  // left child exists and was not yet evaluated
21  if (Curr->Left != NULL) {
22  Prev = Curr;
23  Curr = Curr->Left;
24  } else if (Curr->Right != NULL) {
25  Prev = Curr;
26  Curr = Curr->Right;
27  } else {
28  Curr->Result = EvalAtomicPredicate(Curr->Atom);
29  Prev = Curr;
30  Curr = Curr->Parent;
31  }
32  } else if (Prev == Curr->Left) {
33  // going back up through left (first) child
34  switch (Curr->Op) {
35  case NOT: {
36  Assert(Curr->Right == NULL);
37  Curr->Result = !(Prev->Result);
38  Prev = Curr;
39  Curr = Curr->Parent;
40  break;
41  }
42  case AND: {
43  Assert(Curr->Right != NULL);
44  if (!Prev->Result) {
45  Curr->Result = false;
46  Prev = Curr;
47  Curr = Curr->Parent;
48  } else {
49  Prev = Curr;
50  Curr = Curr->Right;
51  }
52  break;
53  }
54  case OR: {
55  Assert(Curr->Right != NULL);
56  if (Prev->Result) {
57  Curr->Result = true;
58  Prev = Curr;
59  Curr = Curr->Parent;
60  } else {
61  Prev = Curr;
62  Curr = Curr->Right;
63  }
64  break;
65  }
66  case NOP: {
67  break;
68  }
69  }
70  } else {
71  // going back up the tree from right (second) child
72  Assert(Prev == Curr->Right);
73  switch (Curr->Op) {
74  case NOT: {
75  Assert(Curr->Left == NULL);
76  Curr->Result = !(Prev->Result);
77  break;
78  }
79  case AND: {
80  Assert(Curr->Left != NULL);
81  Assert(Curr->Left->Result);
82  Curr->Result = Prev->Result;
83  break;
84  }
85  case OR: {
86  Assert(Curr->Left != NULL);
87  Assert(!Curr->Left->Result);
88  Curr->Result = Prev->Result;
89  break;
90  }
91  case NOP: {
92  break;
93  }
94  }
95  Prev = Curr;
96  Curr = Curr->Parent;
97  }
98  }
99  return Root->Result;
100 }
TPredicateNode * Left
Left child of this node.
Definition: table.h:57
TBool Result
Result of evaulating the predicate rooted at this node.
Definition: table.h:54
TBool EvalAtomicPredicate(const TAtomicPredicate &Atom)
Evaluate the give atomic predicate.
Definition: table.cpp:102
#define Assert(Cond)
Definition: bd.h:251
Definition: table.h:5
TPredicateNode * Right
Definition: table.h:58
TPredOp Op
Logical op represented by this node.
Definition: table.h:53
TPredicateNode * Root
Rood node of the current predicate tree.
Definition: table.h:87
Definition: table.h:5
Definition: table.h:5
Predicate node - represents a binary predicate operation on two predicate nodes.
Definition: table.h:51
TPredicateNode * Parent
Parent node of this node.
Definition: table.h:56
Definition: table.h:5
TAtomicPredicate Atom
Atomic predicate at this node.
Definition: table.h:55
template<class T >
static TBool TPredicate::EvalAtom ( Val1,
Val2,
TPredComp  Cmp 
)
inlinestatic

Compare atomic values Val1 and Val2 using predicate Cmp.

Definition at line 110 of file table.h.

110  {
111  switch (Cmp) {
112  case LT: return Val1 < Val2;
113  case LTE: return Val1 <= Val2;
114  case EQ: return Val1 == Val2;
115  case NEQ: return Val1 != Val2;
116  case GTE: return Val1 >= Val2;
117  case GT: return Val1 > Val2;
118  default: return false;
119  }
120  };
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
Definition: table.h:7
TBool TPredicate::EvalAtomicPredicate ( const TAtomicPredicate Atom)

Evaluate the give atomic predicate.

Definition at line 102 of file table.cpp.

102  {
103  switch (Atom.Type) {
104  case atInt: {
105  if (Atom.IsConst) {
106  return EvalAtom<TInt>(IntVars.GetDat(Atom.Lvar), Atom.IntConst, Atom.Compare);
107  }
108  return EvalAtom<TInt>(IntVars.GetDat(Atom.Lvar), IntVars.GetDat(Atom.Rvar), Atom.Compare);
109  }
110  case atFlt: {
111  if (Atom.IsConst) {
112  return EvalAtom<TFlt>(FltVars.GetDat(Atom.Lvar), Atom.FltConst, Atom.Compare);
113  }
114  return EvalAtom<TFlt>(FltVars.GetDat(Atom.Lvar), FltVars.GetDat(Atom.Rvar), Atom.Compare);
115  }
116  case atStr: {
117  if (Atom.IsConst) {
118  return EvalAtom<TStr>(StrVars.GetDat(Atom.Lvar), Atom.StrConst, Atom.Compare);
119  }
120  return EvalAtom<TStr>(StrVars.GetDat(Atom.Lvar), StrVars.GetDat(Atom.Rvar), Atom.Compare);
121  }
122  }
123  return false;
124 }
TStr Rvar
Right variable of the comparison op.
Definition: table.h:21
TAttrType Type
Type of the predicate variables.
Definition: table.h:17
THash< TStr, TInt > IntVars
Int variables in the current predicate tree.
Definition: table.h:84
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:220
THash< TStr, TStr > StrVars
String variables in the current predicate tree.
Definition: table.h:86
Definition: gbase.h:23
TStr StrConst
Str const value if this object is a string constant.
Definition: table.h:24
TStr Lvar
Left variable of the comparison op.
Definition: table.h:20
TPredComp Compare
Comparison op represented by this node.
Definition: table.h:19
TInt IntConst
Int const value if this object is an integer constant.
Definition: table.h:22
Definition: gbase.h:23
TBool IsConst
Flag if this atomic node represents a constant value.
Definition: table.h:18
Definition: gbase.h:23
TFlt FltConst
Flt const value if this object is a float constant.
Definition: table.h:23
THash< TStr, TFlt > FltVars
Float variables in the current predicate tree.
Definition: table.h:85
static TBool TPredicate::EvalStrAtom ( const TStr Val1,
const TStr Val2,
TPredComp  Cmp 
)
inlinestatic

Compare atomic string values Val1 and Val2 using predicate Cmp.

Definition at line 123 of file table.h.

123  {
124  switch (Cmp) {
125  case LT: return Val1 < Val2;
126  case LTE: return Val1 <= Val2;
127  case EQ: return Val1 == Val2;
128  case NEQ: return Val1 != Val2;
129  case GTE: return Val1 >= Val2;
130  case GT: return Val1 > Val2;
131  case SUBSTR: return Val2.IsStrIn(Val1);
132  case SUPERSTR: return Val1.IsStrIn(Val2);
133  default: return false;
134  }
135  }
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
Definition: table.h:7
bool Cmp(const int &RelOp, const TRec &Rec1, const TRec &Rec2)
Definition: bd.h:426
Definition: table.h:7
bool IsStrIn(const TStr &Str) const
Definition: dt.h:554
void TPredicate::GetVariables ( TStrV Variables)

Get variables in current predicate.

Definition at line 10 of file table.cpp.

10  {
11  Root->GetVariables(Variables);
12 }
TPredicateNode * Root
Rood node of the current predicate tree.
Definition: table.h:87
void GetVariables(TStrV &Variables)
Get variables in the predicate tree rooted at this node.
Definition: table.cpp:1
void TPredicate::SetFltVal ( TStr  VarName,
TFlt  VarVal 
)
inline

Set flt variable value in the predicate or all the children that use it.

Definition at line 100 of file table.h.

100 { FltVars.AddDat(VarName, VarVal); }
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
THash< TStr, TFlt > FltVars
Float variables in the current predicate tree.
Definition: table.h:85
void TPredicate::SetIntVal ( TStr  VarName,
TInt  VarVal 
)
inline

Set int variable value in the predicate or all the children that use it.

Definition at line 98 of file table.h.

98 { IntVars.AddDat(VarName, VarVal); }
THash< TStr, TInt > IntVars
Int variables in the current predicate tree.
Definition: table.h:84
TDat & AddDat(const TKey &Key)
Definition: hash.h:196
void TPredicate::SetStrVal ( TStr  VarName,
TStr  VarVal 
)
inline

Set str variable value in the predicate or all the children that use it.

Definition at line 102 of file table.h.

102 { StrVars.AddDat(VarName, VarVal); }
THash< TStr, TStr > StrVars
String variables in the current predicate tree.
Definition: table.h:86
TDat & AddDat(const TKey &Key)
Definition: hash.h:196

Member Data Documentation

THash<TStr, TFlt> TPredicate::FltVars
protected

Float variables in the current predicate tree.

Definition at line 85 of file table.h.

THash<TStr, TInt> TPredicate::IntVars
protected

Int variables in the current predicate tree.

Definition at line 84 of file table.h.

TPredicateNode* TPredicate::Root
protected

Rood node of the current predicate tree.

Definition at line 87 of file table.h.

THash<TStr, TStr> TPredicate::StrVars
protected

String variables in the current predicate tree.

Definition at line 86 of file table.h.


The documentation for this class was generated from the following files: