dyng
Net.hxx
Go to the documentation of this file.
1 
9 /*
10  * Copyright:: Copyright (c) 2013, 2014, 2015, 2016, 2017, Frank Fischer
11  *
12  * This file is part of DynG.
13  *
14  * DynG is free software: you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation, either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * DynG is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with DynG. If not, see <http://www.gnu.org/licenses/>.
26  */
27 #ifndef __DYNG_NET_HXX__
28 #define __DYNG_NET_HXX__
29 
30 #include <vector>
31 #include <memory>
32 #include <stdexcept>
33 
54 namespace Dyng {
55 
57 typedef int index_type;
58 
68 class DyngError : public std::logic_error
69 {
70 public:
76  DyngError(const std::string& msg) : std::logic_error(msg) {}
77 
79  DyngError(const char* msg) : std::logic_error(msg) {}
80 };
81 
458 class Net
459 {
460 public:
462  typedef index_type Group;
463 
465  typedef index_type BaseNode;
466 
468  typedef index_type BaseArc;
469 
471  typedef index_type Node;
472 
474  typedef index_type Arc;
475 
477  typedef index_type Time;
478 
480  typedef index_type RunTime;
481 
483  static const Node nonode = -1;
485  static const Arc noarc = -2;
486 
492  enum class Result {
494  Solved=1,
497  Expanded=2
498  };
499 
504  template <typename It, typename Node, typename Arc,
505  Arc (Net::*First) (Node) const,
506  Arc (Net::*Next) (Arc) const>
507  class ArcIterator:
508  public std::iterator<std::forward_iterator_tag, const Arc>
509  {
510  public:
511  typedef Node node_type;
512  typedef Arc arc_type;
513 
514  public:
515  ArcIterator(const Net* net) :
516  _net(net), _arc(Net::noarc)
517  {}
518 
519  ArcIterator(const Net* net, Node node) :
520  _net(net), _arc((net->*First)(node))
521  {}
522 
523  Arc operator*() const { return _arc; }
524 
525  bool operator==(const It& it) const {
526  return _arc == it._arc;
527  }
528 
529  bool operator!=(const It& it) const {
530  return _arc != it._arc;
531  }
532 
533  It& operator++() {
534  _arc = (_net->*Next)(_arc);
535  return static_cast<It&>(*this);
536  }
537 
538  private:
539  const Net* _net;
540  Arc _arc;
541  };
542 
543  template <typename It>
544  class ArcList
545  {
546  public:
547  ArcList(const Net* net, typename It::node_type node):
548  _net(net), _node(node)
549  {}
550 
551  It begin() const {
552  return It(_net, _node);
553  }
554 
555  It end() const {
556  return It(_net);
557  }
558 
559  private:
560  const Net* _net;
561  typename It::node_type _node;
562  };
563 
564 
565 public:
570  Net();
571 
573  ~Net();
574 
576  Net(const Net& net) = delete;
577 
579  Net& operator=(const Net& net) = delete;
580 
582  Net(Net&& net);
583 
585  Net& operator=(Net&& net);
586 
589 
594  index_type n_groups() const;
595 
601  Group add_group();
602 
616  Group add_groups(index_type ngroups);
617 
622  index_type n_basenodes() const;
623 
632  BaseNode add_basenode(Group group);
633 
641  Group base_group(BaseNode bnode) const;
642 
655  index_type base_groupidx(BaseNode bnode) const;
656 
664  bool base_waitnode(BaseNode bnode) const;
665 
673  BaseArc base_firstout(BaseNode bnode) const;
674 
682  BaseArc base_firstin(BaseNode bnode) const;
683 
688  index_type n_basearcs() const;
689 
699  BaseArc add_basearc(BaseNode bsrc, BaseNode bsnk);
700 
715  BaseArc base_arc(BaseNode bsrc, BaseNode bsnk) const;
716 
727  void add_runningtime(BaseArc arc, RunTime running_time);
728 
743  void add_runningtimes(BaseArc arc,
744  std::size_t ntimes,
745  const RunTime running_times[]);
746 
748  void add_runningtimes(BaseArc arc,
749  const std::vector<RunTime>& running_times)
750  {
751  add_runningtimes(arc, running_times.size(), &(running_times[0]));
752  }
753 
765  BaseArc base_nextout(BaseArc arc) const;
766 
778  BaseArc base_nextin(BaseArc arc) const;
779 
788  BaseNode base_src(BaseArc arc) const;
789 
798  BaseNode base_snk(BaseArc arc) const;
799 
804  public Net::ArcIterator<BaseOutIterator,
805  BaseNode, BaseArc,
807  {
808  using ArcIterator::ArcIterator;
809  };
810 
814  class BaseOutList: public ArcList<BaseOutIterator> {
815  using ArcList::ArcList;
816  };
817 
819  BaseOutList base_outarcs(BaseNode bnode) const {
820  return BaseOutList(this, bnode);
821  }
822 
827  public Net::ArcIterator<BaseInIterator,
828  BaseNode, BaseArc,
829  &Net::base_firstin, &Net::base_nextin>
830  {
831  using ArcIterator::ArcIterator;
832  };
833 
837  class BaseInList: public ArcList<BaseInIterator> {
838  using ArcList::ArcList;
839  };
840 
842  BaseInList base_inarcs(BaseNode bnode) const {
843  return BaseInList(this, bnode);
844  }
845 
847 
850 
865  void init();
866 
877  Time group_mintime(Group group) const;
878 
889  Time basenode_mintime(BaseNode bnode) const;
890 
891 
898  index_type n_nodes() const;
899 
900 
915  Node node(BaseNode bnode, Time time) const;
916 
917 
929  BaseNode basenode(Node node) const;
930 
931 
944  Time time(Node node) const;
945 
946 
959  Arc firstout(Node node) const;
960 
961 
974  Arc firstin(Node node) const;
975 
976 
983  index_type n_arcs() const;
984 
985 
1001  Arc arc(Node src, Node snk) const;
1002 
1003 
1025  Arc find_arc(BaseNode bsrc, Time srctime, BaseNode bsnk, Time snktime) const;
1026 
1035  bool isactive(Arc arc) const;
1036 
1047  Node src(Arc arc) const;
1048 
1049 
1060  Node snk(Arc arc) const;
1061 
1062 
1077  Arc nextout(Arc arc) const;
1078 
1079 
1094  Arc nextin(Arc arc) const;
1095 
1100  public Net::ArcIterator<BaseOutIterator,
1101  Node, Arc,
1102  &Net::firstout, &Net::base_nextout>
1103  {
1104  using ArcIterator::ArcIterator;
1105  };
1106 
1110  class OutList: public ArcList<BaseOutIterator> {
1111  using ArcList::ArcList;
1112  };
1113 
1115  OutList outarcs(BaseNode bnode) const {
1116  return OutList(this, bnode);
1117  }
1118 
1122  class InIterator:
1123  public Net::ArcIterator<BaseInIterator,
1124  Node, Arc,
1125  &Net::firstin, &Net::base_nextin>
1126  {
1127  using ArcIterator::ArcIterator;
1128  };
1129 
1133  class InList: public ArcList<BaseInIterator> {
1134  using ArcList::ArcList;
1135  };
1136 
1138  InList inarcs(BaseNode bnode) const {
1139  return InList(this, bnode);
1140  }
1141 
1143 
1146 
1161  void force_active(Group grp, Time time_active);
1162 
1166  Time active_time(Group grp) const;
1167 
1196  Result shortest_path(const double basecosts[],
1197  const double augcosts[],
1198  index_type& nnodes,
1199  index_type& narcs);
1200 
1202  Result shortest_path(const std::vector<double>& basecosts,
1203  const std::vector<double>& augcosts,
1204  index_type& nnodes,
1205  index_type& narcs);
1206 
1208  Result shortest_path(const double basecosts[], const double augcosts[]);
1209 
1211  Result shortest_path(const std::vector<double>& basecosts,
1212  const std::vector<double>& augcosts);
1213 
1215  index_type get_n_newnodes() const;
1216 
1218  index_type get_n_newarcs() const;
1219 
1227  index_type get_pathlen() const;
1228 
1240  double get_path(Arc path[]) const;
1241 
1246  double get_path(std::vector<Arc>& path) const;
1247 
1253  index_type get_num_new_active() const;
1254 
1262  void get_new_active(Arc active[]) const;
1263 
1269  void get_new_active(std::vector<Arc>& active) const;
1270 
1272 private:
1273  struct Data;
1274  std::unique_ptr<Data> data;
1275 };
1276 
1277 }
1278 
1279 #endif
Iterator over the incoming arcs of a base node.
Definition: Net.hxx:826
OutList outarcs(BaseNode bnode) const
Return a list of outgoing arcs of a node.
Definition: Net.hxx:1115
static const Arc noarc
ID of a non-existing arc.
Definition: Net.hxx:485
A logical error in the use of the dynamic network occurred.
Definition: Net.hxx:68
Iterator over the outgoing arcs of a node.
Definition: Net.hxx:1099
Wrapper to provide an iterator over the outgoing arcs of a node.
Definition: Net.hxx:1110
BaseOutList base_outarcs(BaseNode bnode) const
Return a list of outgoing arcs of a base node.
Definition: Net.hxx:819
Wrapper to provide an iterator over the outgoing arcs of a base node.
Definition: Net.hxx:814
index_type BaseNode
Type of base nodes.
Definition: Net.hxx:465
index_type Node
Type of nodes.
Definition: Net.hxx:471
Wrapper to provide an iterator over the incoming arcs of a node.
Definition: Net.hxx:1133
index_type BaseArc
Type of base arcs.
Definition: Net.hxx:468
index_type RunTime
Type of running times.
Definition: Net.hxx:480
BaseInList base_inarcs(BaseNode bnode) const
Return a list of incoming arcs of a base node.
Definition: Net.hxx:842
BaseArc base_nextout(BaseArc arc) const
Return the next outgoing base arc.
BaseArc base_firstout(BaseNode bnode) const
Return first outgoing arc of a node in the base graph.
DyngError(const char *msg)
Definition: Net.hxx:79
void add_runningtimes(BaseArc arc, const std::vector< RunTime > &running_times)
Definition: Net.hxx:748
Wrapper to provide an iterator over the incoming arcs of a base node.
Definition: Net.hxx:837
index_type Time
Type of time indices.
Definition: Net.hxx:477
index_type Group
Type of groups.
Definition: Net.hxx:462
int index_type
Type of indices.
Definition: draw.hxx:39
InList inarcs(BaseNode bnode) const
Return a list of incoming arcs of a node.
Definition: Net.hxx:1138
A dynamic time expanded network.
Definition: Net.hxx:458
Result
Return states of the shortest path computation.
Definition: Net.hxx:492
Iterator over the outgoing arcs of a base node.
Definition: Net.hxx:803
index_type Arc
Type of arcs.
Definition: Net.hxx:474
The namespace of DynG.
Definition: draw.hxx:36
Iterator over the incoming arcs of a node.
Definition: Net.hxx:1122
DyngError(const std::string &msg)
Constructor with error message.
Definition: Net.hxx:76