2007-12-11 07:53:44 +01:00
|
|
|
//===- llvm/ADT/Trie.h ---- Generic trie structure --------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-11 07:53:44 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This class defines a generic trie structure. The trie structure
|
|
|
|
// is immutable after creation, but the payload contained within it is not.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_TRIE_H
|
|
|
|
#define LLVM_ADT_TRIE_H
|
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
|
|
|
#include "llvm/Support/DOTGraphTraits.h"
|
|
|
|
|
2009-11-06 00:01:30 +01:00
|
|
|
#include <cassert>
|
2007-12-11 07:53:44 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
// FIXME:
|
|
|
|
// - Labels are usually small, maybe it's better to use SmallString
|
|
|
|
// - Should we use char* during construction?
|
2007-12-11 22:55:38 +01:00
|
|
|
// - Should we templatize Empty with traits-like interface?
|
2007-12-11 07:53:44 +01:00
|
|
|
|
|
|
|
template<class Payload>
|
|
|
|
class Trie {
|
2007-12-16 02:27:04 +01:00
|
|
|
friend class GraphTraits<Trie<Payload> >;
|
|
|
|
friend class DOTGraphTraits<Trie<Payload> >;
|
|
|
|
public:
|
2007-12-11 22:55:38 +01:00
|
|
|
class Node {
|
|
|
|
friend class Trie;
|
2007-12-11 07:53:44 +01:00
|
|
|
|
2007-12-20 02:21:50 +01:00
|
|
|
public:
|
|
|
|
typedef std::vector<Node*> NodeVectorType;
|
|
|
|
typedef typename NodeVectorType::iterator iterator;
|
|
|
|
typedef typename NodeVectorType::const_iterator const_iterator;
|
|
|
|
|
|
|
|
private:
|
2008-03-13 02:07:53 +01:00
|
|
|
enum QueryResult {
|
2007-12-11 07:53:44 +01:00
|
|
|
Same = -3,
|
|
|
|
StringIsPrefix = -2,
|
|
|
|
LabelIsPrefix = -1,
|
|
|
|
DontMatch = 0,
|
|
|
|
HaveCommonPart
|
2008-03-13 02:07:53 +01:00
|
|
|
};
|
2007-12-12 20:08:44 +01:00
|
|
|
|
|
|
|
struct NodeCmp {
|
|
|
|
bool operator() (Node* N1, Node* N2) {
|
|
|
|
return (N1->Label[0] < N2->Label[0]);
|
|
|
|
}
|
|
|
|
bool operator() (Node* N, char Id) {
|
|
|
|
return (N->Label[0] < Id);
|
|
|
|
}
|
|
|
|
};
|
2007-12-11 07:53:44 +01:00
|
|
|
|
2007-12-11 22:55:38 +01:00
|
|
|
std::string Label;
|
|
|
|
Payload Data;
|
2007-12-20 02:21:50 +01:00
|
|
|
NodeVectorType Children;
|
2007-12-12 20:08:44 +01:00
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
// Do not implement
|
|
|
|
Node(const Node&);
|
|
|
|
Node& operator=(const Node&);
|
2007-12-12 20:08:44 +01:00
|
|
|
|
|
|
|
inline void addEdge(Node* N) {
|
|
|
|
if (Children.empty())
|
|
|
|
Children.push_back(N);
|
|
|
|
else {
|
2007-12-20 02:21:50 +01:00
|
|
|
iterator I = std::lower_bound(Children.begin(), Children.end(),
|
|
|
|
N, NodeCmp());
|
2007-12-12 20:08:44 +01:00
|
|
|
// FIXME: no dups are allowed
|
|
|
|
Children.insert(I, N);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void setEdge(Node* N) {
|
|
|
|
char Id = N->Label[0];
|
2007-12-20 02:21:50 +01:00
|
|
|
iterator I = std::lower_bound(Children.begin(), Children.end(),
|
|
|
|
Id, NodeCmp());
|
2007-12-12 20:08:44 +01:00
|
|
|
assert(I != Children.end() && "Node does not exists!");
|
|
|
|
*I = N;
|
2007-12-11 22:55:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QueryResult query(const std::string& s) const {
|
2007-12-11 07:53:44 +01:00
|
|
|
unsigned i, l;
|
2007-12-11 22:55:38 +01:00
|
|
|
unsigned l1 = s.length();
|
2007-12-11 07:53:44 +01:00
|
|
|
unsigned l2 = Label.length();
|
|
|
|
|
|
|
|
// Find the length of common part
|
|
|
|
l = std::min(l1, l2);
|
|
|
|
i = 0;
|
2007-12-11 22:55:38 +01:00
|
|
|
while ((i < l) && (s[i] == Label[i]))
|
2007-12-11 07:53:44 +01:00
|
|
|
++i;
|
|
|
|
|
|
|
|
if (i == l) { // One is prefix of another, find who is who
|
|
|
|
if (l1 == l2)
|
|
|
|
return Same;
|
|
|
|
else if (i == l1)
|
|
|
|
return StringIsPrefix;
|
|
|
|
else
|
|
|
|
return LabelIsPrefix;
|
2007-12-11 22:55:38 +01:00
|
|
|
} else // s and Label have common (possible empty) part, return its length
|
2007-12-11 07:53:44 +01:00
|
|
|
return (QueryResult)i;
|
|
|
|
}
|
2007-12-16 02:27:04 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
inline explicit Node(const Payload& data, const std::string& label = ""):
|
|
|
|
Label(label), Data(data) { }
|
|
|
|
|
|
|
|
inline const Payload& data() const { return Data; }
|
|
|
|
inline void setData(const Payload& data) { Data = data; }
|
|
|
|
|
|
|
|
inline const std::string& label() const { return Label; }
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
inline void dump() {
|
2009-08-03 03:02:24 +02:00
|
|
|
llvm::cerr << "Node: " << this << "\n"
|
2007-12-16 02:27:04 +01:00
|
|
|
<< "Label: " << Label << "\n"
|
|
|
|
<< "Children:\n";
|
|
|
|
|
2007-12-20 02:21:50 +01:00
|
|
|
for (iterator I = Children.begin(), E = Children.end(); I != E; ++I)
|
2009-08-03 03:02:24 +02:00
|
|
|
llvm::cerr << (*I)->Label << "\n";
|
2007-12-16 02:27:04 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
inline Node* getEdge(char Id) {
|
|
|
|
Node* fNode = NULL;
|
2007-12-20 02:21:50 +01:00
|
|
|
iterator I = std::lower_bound(Children.begin(), Children.end(),
|
2007-12-16 02:27:04 +01:00
|
|
|
Id, NodeCmp());
|
|
|
|
if (I != Children.end() && (*I)->Label[0] == Id)
|
|
|
|
fNode = *I;
|
|
|
|
|
|
|
|
return fNode;
|
|
|
|
}
|
2007-12-20 02:21:50 +01:00
|
|
|
|
|
|
|
inline iterator begin() { return Children.begin(); }
|
|
|
|
inline const_iterator begin() const { return Children.begin(); }
|
|
|
|
inline iterator end () { return Children.end(); }
|
|
|
|
inline const_iterator end () const { return Children.end(); }
|
|
|
|
|
|
|
|
inline size_t size () const { return Children.size(); }
|
|
|
|
inline bool empty() const { return Children.empty(); }
|
|
|
|
inline const Node* &front() const { return Children.front(); }
|
|
|
|
inline Node* &front() { return Children.front(); }
|
|
|
|
inline const Node* &back() const { return Children.back(); }
|
2007-12-20 02:30:27 +01:00
|
|
|
inline Node* &back() { return Children.back(); }
|
2007-12-20 02:21:50 +01:00
|
|
|
|
2007-12-11 07:53:44 +01:00
|
|
|
};
|
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
private:
|
2007-12-11 07:53:44 +01:00
|
|
|
std::vector<Node*> Nodes;
|
|
|
|
Payload Empty;
|
|
|
|
|
2007-12-11 22:55:38 +01:00
|
|
|
inline Node* addNode(const Payload& data, const std::string label = "") {
|
|
|
|
Node* N = new Node(data, label);
|
2007-12-11 07:53:44 +01:00
|
|
|
Nodes.push_back(N);
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2007-12-11 22:55:38 +01:00
|
|
|
inline Node* splitEdge(Node* N, char Id, size_t index) {
|
2007-12-12 20:08:44 +01:00
|
|
|
Node* eNode = N->getEdge(Id);
|
|
|
|
assert(eNode && "Node doesn't exist");
|
2007-12-11 07:53:44 +01:00
|
|
|
|
2007-12-11 22:55:38 +01:00
|
|
|
const std::string &l = eNode->Label;
|
|
|
|
assert(index > 0 && index < l.length() && "Trying to split too far!");
|
2007-12-11 07:53:44 +01:00
|
|
|
std::string l1 = l.substr(0, index);
|
|
|
|
std::string l2 = l.substr(index);
|
|
|
|
|
2007-12-11 22:55:38 +01:00
|
|
|
Node* nNode = addNode(Empty, l1);
|
2007-12-12 20:08:44 +01:00
|
|
|
N->setEdge(nNode);
|
2007-12-11 22:55:38 +01:00
|
|
|
|
2007-12-12 20:08:44 +01:00
|
|
|
eNode->Label = l2;
|
|
|
|
nNode->addEdge(eNode);
|
2007-12-11 07:53:44 +01:00
|
|
|
|
|
|
|
return nNode;
|
|
|
|
}
|
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
// Do not implement
|
|
|
|
Trie(const Trie&);
|
|
|
|
Trie& operator=(const Trie&);
|
|
|
|
|
2007-12-11 07:53:44 +01:00
|
|
|
public:
|
|
|
|
inline explicit Trie(const Payload& empty):Empty(empty) {
|
|
|
|
addNode(Empty);
|
|
|
|
}
|
|
|
|
inline ~Trie() {
|
|
|
|
for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
|
|
|
|
delete Nodes[i];
|
|
|
|
}
|
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
inline Node* getRoot() const { return Nodes[0]; }
|
|
|
|
|
2007-12-16 02:36:16 +01:00
|
|
|
bool addString(const std::string& s, const Payload& data);
|
|
|
|
const Payload& lookup(const std::string& s) const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define this out-of-line to dissuade the C++ compiler from inlining it.
|
|
|
|
template<class Payload>
|
|
|
|
bool Trie<Payload>::addString(const std::string& s, const Payload& data) {
|
|
|
|
Node* cNode = getRoot();
|
|
|
|
Node* tNode = NULL;
|
|
|
|
std::string s1(s);
|
|
|
|
|
|
|
|
while (tNode == NULL) {
|
|
|
|
char Id = s1[0];
|
|
|
|
if (Node* nNode = cNode->getEdge(Id)) {
|
|
|
|
typename Node::QueryResult r = nNode->query(s1);
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case Node::Same:
|
|
|
|
case Node::StringIsPrefix:
|
|
|
|
// Currently we don't allow to have two strings in the trie one
|
|
|
|
// being a prefix of another. This should be fixed.
|
|
|
|
assert(0 && "FIXME!");
|
|
|
|
return false;
|
|
|
|
case Node::DontMatch:
|
|
|
|
assert(0 && "Impossible!");
|
|
|
|
return false;
|
|
|
|
case Node::LabelIsPrefix:
|
|
|
|
s1 = s1.substr(nNode->label().length());
|
|
|
|
cNode = nNode;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nNode = splitEdge(cNode, Id, r);
|
|
|
|
tNode = addNode(data, s1.substr(r));
|
|
|
|
nNode->addEdge(tNode);
|
2007-12-11 22:55:38 +01:00
|
|
|
}
|
2007-12-16 02:36:16 +01:00
|
|
|
} else {
|
|
|
|
tNode = addNode(data, s1);
|
|
|
|
cNode->addEdge(tNode);
|
2007-12-11 07:53:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-16 02:36:16 +01:00
|
|
|
return true;
|
|
|
|
}
|
2007-12-11 07:53:44 +01:00
|
|
|
|
2007-12-16 02:36:16 +01:00
|
|
|
template<class Payload>
|
|
|
|
const Payload& Trie<Payload>::lookup(const std::string& s) const {
|
|
|
|
Node* cNode = getRoot();
|
|
|
|
Node* tNode = NULL;
|
|
|
|
std::string s1(s);
|
|
|
|
|
|
|
|
while (tNode == NULL) {
|
|
|
|
char Id = s1[0];
|
|
|
|
if (Node* nNode = cNode->getEdge(Id)) {
|
|
|
|
typename Node::QueryResult r = nNode->query(s1);
|
|
|
|
|
|
|
|
switch (r) {
|
|
|
|
case Node::Same:
|
|
|
|
tNode = nNode;
|
|
|
|
break;
|
|
|
|
case Node::StringIsPrefix:
|
|
|
|
return Empty;
|
|
|
|
case Node::DontMatch:
|
|
|
|
assert(0 && "Impossible!");
|
|
|
|
return Empty;
|
|
|
|
case Node::LabelIsPrefix:
|
|
|
|
s1 = s1.substr(nNode->label().length());
|
|
|
|
cNode = nNode;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Empty;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return Empty;
|
2007-12-16 02:27:04 +01:00
|
|
|
}
|
|
|
|
|
2007-12-16 02:36:16 +01:00
|
|
|
return tNode->data();
|
|
|
|
}
|
2007-12-16 02:27:04 +01:00
|
|
|
|
|
|
|
template<class Payload>
|
|
|
|
struct GraphTraits<Trie<Payload> > {
|
2007-12-20 02:30:27 +01:00
|
|
|
typedef Trie<Payload> TrieType;
|
|
|
|
typedef typename TrieType::Node NodeType;
|
|
|
|
typedef typename NodeType::iterator ChildIteratorType;
|
2007-12-16 02:27:04 +01:00
|
|
|
|
2009-02-20 23:20:18 +01:00
|
|
|
static inline NodeType *getEntryNode(const TrieType& T) {
|
|
|
|
return T.getRoot();
|
|
|
|
}
|
2007-12-16 02:27:04 +01:00
|
|
|
|
2009-02-20 23:20:18 +01:00
|
|
|
static inline ChildIteratorType child_begin(NodeType *N) {
|
|
|
|
return N->begin();
|
|
|
|
}
|
2007-12-20 02:30:27 +01:00
|
|
|
static inline ChildIteratorType child_end(NodeType *N) { return N->end(); }
|
2007-12-16 02:27:04 +01:00
|
|
|
|
|
|
|
typedef typename std::vector<NodeType*>::const_iterator nodes_iterator;
|
|
|
|
|
2007-12-20 02:30:27 +01:00
|
|
|
static inline nodes_iterator nodes_begin(const TrieType& G) {
|
2007-12-16 02:27:04 +01:00
|
|
|
return G.Nodes.begin();
|
|
|
|
}
|
2007-12-20 02:30:27 +01:00
|
|
|
static inline nodes_iterator nodes_end(const TrieType& G) {
|
2007-12-16 02:27:04 +01:00
|
|
|
return G.Nodes.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Payload>
|
|
|
|
struct DOTGraphTraits<Trie<Payload> > : public DefaultDOTGraphTraits {
|
|
|
|
typedef typename Trie<Payload>::Node NodeType;
|
|
|
|
typedef typename GraphTraits<Trie<Payload> >::ChildIteratorType EdgeIter;
|
|
|
|
|
|
|
|
static std::string getGraphName(const Trie<Payload>& T) {
|
|
|
|
return "Trie";
|
|
|
|
}
|
|
|
|
|
2009-11-30 14:14:13 +01:00
|
|
|
static std::string getNodeLabel(NodeType* Node, const Trie<Payload>& T) {
|
2007-12-16 02:27:04 +01:00
|
|
|
if (T.getRoot() == Node)
|
|
|
|
return "<Root>";
|
|
|
|
else
|
|
|
|
return Node->label();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getEdgeSourceLabel(NodeType* Node, EdgeIter I) {
|
|
|
|
NodeType* N = *I;
|
|
|
|
return N->label().substr(0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeAttributes(const NodeType* Node,
|
|
|
|
const Trie<Payload>& T) {
|
|
|
|
if (Node->data() != T.Empty)
|
|
|
|
return "color=blue";
|
|
|
|
|
|
|
|
return "";
|
2007-12-11 07:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-12-16 02:27:04 +01:00
|
|
|
} // end of llvm namespace
|
2007-12-11 07:53:44 +01:00
|
|
|
|
|
|
|
#endif // LLVM_ADT_TRIE_H
|