summaryrefslogtreecommitdiff
path: root/src/expr/node_manager.cpp
blob: ab52b9f4046953b62f281f27a21f1811a2d2aa79 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*********************                                           -*- C++ -*-  */
/** node_manager.cpp
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009 The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.
 **
 ** Expression manager implementation.
 **/

#include "node_builder.h"
#include "node_manager.h"
#include "expr/node.h"

namespace CVC4 {

__thread NodeManager* NodeManager::s_current = 0;

Node NodeManager::lookup(uint64_t hash, NodeValue* ev) {
  Assert(this != NULL, "Whoops, we have a bad expression manager!");
  hash_t::iterator i = d_hash.find(hash);
  if(i == d_hash.end()) {
    // insert
    std::vector<Node> v;
    Node e(ev);
    v.push_back(e);
    d_hash.insert(std::make_pair(hash, v));
    return e;
  } else {
    for(std::vector<Node>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
      if(ev->getKind() != j->getKind()) {
        continue;
      }

      if(ev->numChildren() != j->numChildren()) {
        continue;
      }

      NodeValue::const_iterator c1 = ev->ev_begin();
      NodeValue::iterator c2 = j->d_ev->ev_begin();
      for(; c1 != ev->ev_end() && c2 != j->d_ev->ev_end(); ++c1, ++c2) {
        if((*c1).d_ev != (*c2).d_ev) {
          break;
        }
      }

      if(c1 != ev->ev_end() || c2 != j->end()) {
        continue;
      }

      return *j;
    }
    // didn't find it, insert
    std::vector<Node> v;
    Node e(ev);
    v.push_back(e);
    d_hash.insert(std::make_pair(hash, v));
    return e;
  }
}

NodeValue* NodeManager::lookupNoInsert(uint64_t hash, NodeValue* ev) {
  Assert(this != NULL, "Whoops, we have a bad expression manager!");
  hash_t::iterator i = d_hash.find(hash);
  if(i == d_hash.end()) {
    return NULL;
  } else {
    for(std::vector<Node>::iterator j = i->second.begin(); j != i->second.end(); ++j) {
      if(ev->getKind() != j->getKind()) {
        continue;
      }

      if(ev->numChildren() != j->numChildren()) {
        continue;
      }

      NodeValue::const_iterator c1 = ev->ev_begin();
      NodeValue::iterator c2 = j->d_ev->ev_begin();
      for(; c1 != ev->ev_end() && c2 != j->d_ev->ev_end(); ++c1, ++c2) {
        if((*c1).d_ev != (*c2).d_ev) {
          break;
        }
      }

      if(c1 != ev->ev_end() || c2 != j->end()) {
        continue;
      }

      return j->d_ev;
    }
    // didn't find it
    return 0;
  }
}

// general expression-builders

Node NodeManager::mkExpr(Kind kind) {
  return NodeBuilder<>(this, kind);
}

Node NodeManager::mkExpr(Kind kind, const Node& child1) {
  return NodeBuilder<>(this, kind) << child1;
}

Node NodeManager::mkExpr(Kind kind, const Node& child1, const Node& child2) {
  return NodeBuilder<>(this, kind) << child1 << child2;
}

Node NodeManager::mkExpr(Kind kind, const Node& child1, const Node& child2, const Node& child3) {
  return NodeBuilder<>(this, kind) << child1 << child2 << child3;
}

Node NodeManager::mkExpr(Kind kind, const Node& child1, const Node& child2, const Node& child3, const Node& child4) {
  return NodeBuilder<>(this, kind) << child1 << child2 << child3 << child4;
}

Node NodeManager::mkExpr(Kind kind, const Node& child1, const Node& child2, const Node& child3, const Node& child4, const Node& child5) {
  return NodeBuilder<>(this, kind) << child1 << child2 << child3 << child4 << child5;
}

// N-ary version
Node NodeManager::mkExpr(Kind kind, const std::vector<Node>& children) {
  return NodeBuilder<>(this, kind).append(children);
}

Node NodeManager::mkVar() {
  return NodeBuilder<>(this, VARIABLE);
}

}/* CVC4 namespace */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback