LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
graph.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #include "engine/engineimp.h"
18 #include "graph.h"
19 void graph::insert(item_wrapper it_p){
20  if(nodes.count(it_p->name())){
21  std::cout << "Warning: variable " << it_p->name() << "has already been entered\n";
22  std::cout <<"Overwriting with new value\n";
23  }
24  _insert(it_p);
25 }
27  graphnode& g = nodes[it_p->name()];
28  g.p = it_p;
29  g.processed=0;
30  std::vector<std::string> deps(it_p.dependencies());
31  g.pointsto.clear();
32  // g.pointsto.insert(g.pointsto.begin(), deps.begin(), deps.end());
33  for(auto& val : deps){
34  trim(val);
35  g.pointsto.push_back(val);
36  }
37  return g;
38 }
40  for(auto& node : nodes){
41  graphnode& g = node.second;
42  if(!g.processed){
43  proc_node_deps(g);
44  }
45  }
46 }
47 //may add support for dependencies that aren't
48 //input variables later if need be
50  g.processed=1;
51  constexpr char maybe_dep = '!';
52  constexpr char exist_dep = '#';
53  std::list<std::string> gets_removed;
54  for(auto& val : g.pointsto){
55  //existence is optional
56  if(val[0] == maybe_dep){
57  //remove the dependency if it doesn't exist
58  if(!nodes.count(val)){
59  gets_removed.push_back(val);
60  }
61  else{
62  val.erase(0,1);
63  nodes[val].pointedat.push_back(g.p->name());
64  }
65  }
66  //postprocesing does not matter for the dependency
67  else if(val[0] == exist_dep){
68  if(!nodes.count(val)){
69  val.erase(0,1);
70  std::cout<<g.p->name()<<" depends on "<<val<<", which has not been inserted\n";
71  exit(EXIT_FAILURE);
72  }
73  gets_removed.push_back(val);
74  }
75  else{
76  if(!nodes.count(val)){
77  std::cout<<g.p->name()<<" depends on "<<val<<", which has not been inserted\n";
78  exit(EXIT_FAILURE);
79  }
80  nodes[val].pointedat.push_back(g.p->name());
81  }
82  }
83  //remove dependencies that are not considered to exist anymore
84  for(const auto& val : gets_removed){
85  g.pointsto.remove(val);
86  }
87 }
88 //returns sorted list, all items in list are added to the graph
89 std::list<item_wrapper > graph::sort(const std::list<item_wrapper >& l){
90  std::list<item_wrapper > outlist;
91  std::list<const graphnode*> queuelist;
92  for(const auto& val: l){
93  insert(val);
94  }
96  //populate the list of nodes without dependencies
97  for(const auto& node : nodes){
98  const graphnode& g = node.second;
99  if(g.pointedat.empty()){
100  queuelist.push_back(&g);
101  }
102  }
103  //perform topological sort algorithm
104  while(!queuelist.empty()){
105  const graphnode& g = *queuelist.front();
106  queuelist.pop_front();
107  outlist.push_front(g.p);
108  for(const auto& val : g.pointsto){
109  graphnode& gn = nodes[val];
110  gn.pointedat.remove(g.p->name());
111  if(gn.pointedat.empty()){
112  queuelist.push_back(&gn);
113  }
114  }
115  }
116  //check to see if list was fully sorted
117  if(l.size() != outlist.size()){
118  err("Topological sort has failed, check for cyclical input dependencies",
119  "graph::sort", "engine/graph.cpp", FATAL_ERROR);
120  }
121  return outlist;
122 }