aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/Analysis/DSGraphTraits.h
blob: 575c1e183061126ce55a5dc4f7a5e684c272d424 (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
//===- DSGraphTraits.h - Provide generic graph interface --------*- C++ -*-===//
//
// This file provides GraphTraits specializations for the DataStructure graph
// nodes, allowing datastructure graphs to be processed by generic graph
// algorithms.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_ANALYSIS_DSGRAPHTRAITS_H
#define LLVM_ANALYSIS_DSGRAPHTRAITS_H

#include "llvm/Analysis/DSGraph.h"
#include "Support/GraphTraits.h"
#include "Support/iterator"
#include "Support/STLExtras.h"

class DSNodeIterator : public forward_iterator<DSNode, ptrdiff_t> {
  friend class DSNode;
  DSNode * const Node;
  unsigned Link;
  
  typedef DSNodeIterator _Self;

  DSNodeIterator(DSNode *N) : Node(N), Link(0) {}   // begin iterator
  DSNodeIterator(DSNode *N, bool)       // Create end iterator
    : Node(N), Link(N->getSize()) {
  }
public:

  bool operator==(const _Self& x) const {
    return Link == x.Link;
  }
  bool operator!=(const _Self& x) const { return !operator==(x); }

  const _Self &operator=(const _Self &I) {
    assert(I.Node == Node && "Cannot assign iterators to two different nodes!");
    Link = I.Link;
    return *this;
  }
  
  pointer operator*() const {
    DSNodeHandle *NH = Node->getLink(Link);
    return NH ? NH->getNode() : 0;
  }
  pointer operator->() const { return operator*(); }
  
  _Self& operator++() {                // Preincrement
    ++Link;
    return *this;
  }
  _Self operator++(int) { // Postincrement
    _Self tmp = *this; ++*this; return tmp; 
  }

  unsigned getLink() const { return Link; }
  DSNode *getNode() const { return Node; }
};

// Provide iterators for DSNode...
inline DSNode::iterator DSNode::begin() { return DSNodeIterator(this); }
inline DSNode::iterator DSNode::end()   { return DSNodeIterator(this, false); }

template <> struct GraphTraits<DSNode*> {
  typedef DSNode NodeType;
  typedef DSNode::iterator ChildIteratorType;

  static NodeType *getEntryNode(NodeType *N) { return N; }
  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
};

static DSNode &dereference(DSNode *N) { return *N; }

template <> struct GraphTraits<DSGraph*> {
  typedef DSNode NodeType;
  typedef DSNode::iterator ChildIteratorType;

  typedef std::pointer_to_unary_function<DSNode *, DSNode&> DerefFun;

  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  typedef mapped_iterator<std::vector<DSNode*>::iterator,
                          DerefFun> nodes_iterator;
  static nodes_iterator nodes_begin(DSGraph *G) { return map_iterator(G->getNodes().begin(), DerefFun(dereference));}
  static nodes_iterator nodes_end  (DSGraph *G) { return map_iterator(G->getNodes().end(), DerefFun(dereference)); }

  static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
};

#endif