Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * This file is part of the cvc5 project. 3 : : * 4 : : * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS 5 : : * in the top-level source directory and their institutional affiliations. 6 : : * All rights reserved. See the file COPYING in the top-level source 7 : : * directory for licensing information. 8 : : * **************************************************************************** 9 : : * 10 : : * A trie class for Nodes and TNodes. 11 : : */ 12 : : 13 : : #include "cvc5_private.h" 14 : : 15 : : #ifndef CVC5__EXPR__NODE_TRIE_H 16 : : #define CVC5__EXPR__NODE_TRIE_H 17 : : 18 : : #include <map> 19 : : 20 : : #include "expr/node.h" 21 : : 22 : : namespace cvc5::internal { 23 : : 24 : : /** NodeTemplate trie class 25 : : * 26 : : * This is a trie data structure whose distinguishing feature is that it has 27 : : * no "data" members and only references to children. The motivation for having 28 : : * no data members is efficiency. 29 : : * 30 : : * One use of this class is to represent "term indices" or a "signature tables" 31 : : * for symbols with fixed arities. In this use case, we "index" terms by the 32 : : * list of representatives of their arguments. 33 : : * 34 : : * For example, consider the equivalence classes : 35 : : * 36 : : * { a, d, f( d, c ), f( a, c ) } 37 : : * { b, f( b, d ) } 38 : : * { c, f( b, b ) } 39 : : * 40 : : * where the first elements ( a, b, c ) are the representatives of these 41 : : * classes. The NodeTemplateTrie t we may build for f is : 42 : : * 43 : : * t : 44 : : * t.d_data[a] : 45 : : * t.d_data[a].d_data[c] : 46 : : * t.d_data[a].d_data[c].d_data[f(d,c)] : (leaf) 47 : : * t.d_data[b] : 48 : : * t.d_data[b].d_data[b] : 49 : : * t.d_data[b].d_data[b].d_data[f(b,b)] : (leaf) 50 : : * t.d_data[b].d_data[a] : 51 : : * t.d_data[b].d_data[a].d_data[f(b,d)] : (leaf) 52 : : * 53 : : * Leaf nodes store the terms that are indexed by the arguments, for example 54 : : * term f(d,c) is indexed by the representative arguments (a,c), and is stored 55 : : * as a the (single) key in the data of t.d_data[a].d_data[c]. 56 : : */ 57 : : template <bool ref_count> 58 : : class NodeTemplateTrie 59 : : { 60 : : public: 61 : : /** The children of this node. */ 62 : : std::map<NodeTemplate<ref_count>, NodeTemplateTrie<ref_count>> d_data; 63 : : /** For leaf nodes : does this node have data? */ 64 : : bool hasData() const { return !d_data.empty(); } 65 : : /** For leaf nodes : get the node corresponding to this leaf. */ 66 : 2339458 : NodeTemplate<ref_count> getData() const { return d_data.begin()->first; } 67 : : /** 68 : : * Returns the term that is indexed by reps, if one exists, or 69 : : * or returns null otherwise. 70 : : */ 71 : : NodeTemplate<ref_count> existsTerm( 72 : : const std::vector<NodeTemplate<ref_count>>& reps) const; 73 : : /** 74 : : * Returns the term that is previously indexed by reps, if one exists, or 75 : : * adds n to the trie, indexed by reps, and returns n. 76 : : */ 77 : : NodeTemplate<ref_count> addOrGetTerm( 78 : : NodeTemplate<ref_count> n, 79 : : const std::vector<NodeTemplate<ref_count>>& reps); 80 : : /** 81 : : * Returns false if a term is previously indexed by reps. 82 : : * Returns true if no term is previously indexed by reps, 83 : : * and adds n to the trie, indexed by reps. 84 : : */ 85 : : inline bool addTerm(NodeTemplate<ref_count> n, 86 : : const std::vector<NodeTemplate<ref_count>>& reps); 87 : : /** Debug print this trie on Trace c with indentation depth. */ 88 : : void debugPrint(const char* c, unsigned depth = 0) const; 89 : : /** Clear all data from this trie. */ 90 : 2860911 : void clear() { d_data.clear(); } 91 : : /** Is this trie empty? */ 92 : 3128906 : bool empty() const { return d_data.empty(); } 93 : : /** 94 : : * Get leaves at the given depth, where depth>0. This argument is necessary 95 : : * since we do not know apriori the depth of where data occurs. 96 : : * 97 : : * If this trie stores applications of a function f, then depth should be set 98 : : * to the arity of f. 99 : : * 100 : : * Notice this method will never throw an assertion error, even if the 101 : : * depth is not set to the proper value. In particular, it will return 102 : : * the empty vector if the provided depth is larger than the actual depth, 103 : : * and will return internal nodes if the provided depth is less than the 104 : : * actual depth of the trie. 105 : : */ 106 : : std::vector<Node> getLeaves(size_t depth) const; 107 : : }; /* class NodeTemplateTrie */ 108 : : 109 : : template <bool ref_count> 110 : 2144260 : bool NodeTemplateTrie<ref_count>::addTerm( 111 : : NodeTemplate<ref_count> n, const std::vector<NodeTemplate<ref_count>>& reps) 112 : : { 113 : 2144260 : return addOrGetTerm(n, reps) == n; 114 : : } 115 : : 116 : : /** Reference-counted version of the above data structure */ 117 : : typedef NodeTemplateTrie<true> NodeTrie; 118 : : /** Non-reference-counted version of the above data structure */ 119 : : typedef NodeTemplateTrie<false> TNodeTrie; 120 : : 121 : : } // namespace cvc5::internal 122 : : 123 : : #endif /* CVC5__EXPR__NODE_TRIE_H */