41 /// |
41 /// |
42 /// A structure for representing directed path in a digraph. |
42 /// A structure for representing directed path in a digraph. |
43 /// \param Digraph The digraph type in which the path is. |
43 /// \param Digraph The digraph type in which the path is. |
44 /// |
44 /// |
45 /// In a sense, the path can be treated as a list of arcs. The |
45 /// In a sense, the path can be treated as a list of arcs. The |
46 /// lemon path type stores just this list. As a consequence it |
46 /// lemon path type stores just this list. As a consequence, it |
47 /// cannot enumerate the nodes in the path and the zero length paths |
47 /// cannot enumerate the nodes of the path and the source node of |
48 /// cannot store the source. |
48 /// a zero length path is undefined. |
49 /// |
49 /// |
50 /// This implementation is a back and front insertable and erasable |
50 /// This implementation is a back and front insertable and erasable |
51 /// path type. It can be indexed in O(1) time. The front and back |
51 /// path type. It can be indexed in O(1) time. The front and back |
52 /// insertion and erasure is amortized O(1) time. The |
52 /// insertion and erase is done in O(1) (amortized) time. The |
53 /// impelementation is based on two opposite organized vectors. |
53 /// implementation uses two vectors for storing the front and back |
|
54 /// insertions. |
54 template <typename _Digraph> |
55 template <typename _Digraph> |
55 class Path { |
56 class Path { |
56 public: |
57 public: |
57 |
58 |
58 typedef _Digraph Digraph; |
59 typedef _Digraph Digraph; |
63 /// Default constructor |
64 /// Default constructor |
64 Path() {} |
65 Path() {} |
65 |
66 |
66 /// \brief Template copy constructor |
67 /// \brief Template copy constructor |
67 /// |
68 /// |
68 /// This path can be initialized with any other path type. It just |
69 /// This constuctor initializes the path from any other path type. |
69 /// makes a copy of the given path. |
70 /// It simply makes a copy of the given path. |
70 template <typename CPath> |
71 template <typename CPath> |
71 Path(const CPath& cpath) { |
72 Path(const CPath& cpath) { |
72 copyPath(*this, cpath); |
73 copyPath(*this, cpath); |
73 } |
74 } |
74 |
75 |
75 /// \brief Template copy assignment |
76 /// \brief Template copy assignment |
76 /// |
77 /// |
77 /// This path can be initialized with any other path type. It just |
78 /// This operator makes a copy of a path of any other type. |
78 /// makes a copy of the given path. |
|
79 template <typename CPath> |
79 template <typename CPath> |
80 Path& operator=(const CPath& cpath) { |
80 Path& operator=(const CPath& cpath) { |
81 copyPath(*this, cpath); |
81 copyPath(*this, cpath); |
82 return *this; |
82 return *this; |
83 } |
83 } |
90 public: |
90 public: |
91 /// \brief Default constructor |
91 /// \brief Default constructor |
92 ArcIt() {} |
92 ArcIt() {} |
93 /// \brief Invalid constructor |
93 /// \brief Invalid constructor |
94 ArcIt(Invalid) : path(0), idx(-1) {} |
94 ArcIt(Invalid) : path(0), idx(-1) {} |
95 /// \brief Initializate the constructor to the first arc of path |
95 /// \brief Initializate the iterator to the first arc of path |
96 ArcIt(const Path &_path) |
96 ArcIt(const Path &_path) |
97 : path(&_path), idx(_path.empty() ? -1 : 0) {} |
97 : path(&_path), idx(_path.empty() ? -1 : 0) {} |
98 |
98 |
99 private: |
99 private: |
100 |
100 |
127 int idx; |
127 int idx; |
128 }; |
128 }; |
129 |
129 |
130 /// \brief Length of the path. |
130 /// \brief Length of the path. |
131 int length() const { return head.size() + tail.size(); } |
131 int length() const { return head.size() + tail.size(); } |
132 /// \brief Returns whether the path is empty. |
132 /// \brief Return whether the path is empty. |
133 bool empty() const { return head.empty() && tail.empty(); } |
133 bool empty() const { return head.empty() && tail.empty(); } |
134 |
134 |
135 /// \brief Resets the path to an empty path. |
135 /// \brief Reset the path to an empty one. |
136 void clear() { head.clear(); tail.clear(); } |
136 void clear() { head.clear(); tail.clear(); } |
137 |
137 |
138 /// \brief Gives back the nth arc. |
138 /// \brief The nth arc. |
139 /// |
139 /// |
140 /// \pre n is in the [0..length() - 1] range |
140 /// \pre n is in the [0..length() - 1] range |
141 const Arc& nth(int n) const { |
141 const Arc& nth(int n) const { |
142 return n < int(head.size()) ? *(head.rbegin() + n) : |
142 return n < int(head.size()) ? *(head.rbegin() + n) : |
143 *(tail.begin() + (n - head.size())); |
143 *(tail.begin() + (n - head.size())); |
144 } |
144 } |
145 |
145 |
146 /// \brief Initializes arc iterator to point to the nth arc |
146 /// \brief Initialize arc iterator to point to the nth arc |
147 /// |
147 /// |
148 /// \pre n is in the [0..length() - 1] range |
148 /// \pre n is in the [0..length() - 1] range |
149 ArcIt nthIt(int n) const { |
149 ArcIt nthIt(int n) const { |
150 return ArcIt(*this, n); |
150 return ArcIt(*this, n); |
151 } |
151 } |
152 |
152 |
153 /// \brief Gives back the first arc of the path |
153 /// \brief The first arc of the path |
154 const Arc& front() const { |
154 const Arc& front() const { |
155 return head.empty() ? tail.front() : head.back(); |
155 return head.empty() ? tail.front() : head.back(); |
156 } |
156 } |
157 |
157 |
158 /// \brief Add a new arc before the current path |
158 /// \brief Add a new arc before the current path |
173 std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
173 std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
174 tail.resize(tail.size() - halfsize - 1); |
174 tail.resize(tail.size() - halfsize - 1); |
175 } |
175 } |
176 } |
176 } |
177 |
177 |
178 /// \brief Gives back the last arc of the path |
178 /// \brief The last arc of the path |
179 const Arc& back() const { |
179 const Arc& back() const { |
180 return tail.empty() ? head.front() : tail.back(); |
180 return tail.empty() ? head.front() : tail.back(); |
181 } |
181 } |
182 |
182 |
183 /// \brief Add a new arc behind the current path |
183 /// \brief Add a new arc behind the current path |
197 std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
197 std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
198 head.resize(head.size() - halfsize - 1); |
198 head.resize(head.size() - halfsize - 1); |
199 } |
199 } |
200 } |
200 } |
201 |
201 |
202 |
|
203 |
|
204 typedef True BuildTag; |
202 typedef True BuildTag; |
205 |
203 |
206 template <typename CPath> |
204 template <typename CPath> |
207 void build(const CPath& path) { |
205 void build(const CPath& path) { |
208 int len = path.length(); |
206 int len = path.length(); |
321 int idx; |
319 int idx; |
322 }; |
320 }; |
323 |
321 |
324 /// \brief Length of the path. |
322 /// \brief Length of the path. |
325 int length() const { return data.size(); } |
323 int length() const { return data.size(); } |
326 /// \brief Returns whether the path is empty. |
324 /// \brief Return true if the path is empty. |
327 bool empty() const { return data.empty(); } |
325 bool empty() const { return data.empty(); } |
328 |
326 |
329 /// \brief Resets the path to an empty path. |
327 /// \brief Reset the path to an empty one. |
330 void clear() { data.clear(); } |
328 void clear() { data.clear(); } |
331 |
329 |
332 /// \brief Gives back the nth arc. |
330 /// \brief The nth arc. |
333 /// |
331 /// |
334 /// \pre n is in the [0..length() - 1] range |
332 /// \pre n is in the [0..length() - 1] range |
335 const Arc& nth(int n) const { |
333 const Arc& nth(int n) const { |
336 return data[n]; |
334 return data[n]; |
337 } |
335 } |
339 /// \brief Initializes arc iterator to point to the nth arc. |
337 /// \brief Initializes arc iterator to point to the nth arc. |
340 ArcIt nthIt(int n) const { |
338 ArcIt nthIt(int n) const { |
341 return ArcIt(*this, n); |
339 return ArcIt(*this, n); |
342 } |
340 } |
343 |
341 |
344 /// \brief Gives back the first arc of the path. |
342 /// \brief The first arc of the path. |
345 const Arc& front() const { |
343 const Arc& front() const { |
346 return data.front(); |
344 return data.front(); |
347 } |
345 } |
348 |
346 |
349 /// \brief Gives back the last arc of the path. |
347 /// \brief The last arc of the path. |
350 const Arc& back() const { |
348 const Arc& back() const { |
351 return data.back(); |
349 return data.back(); |
352 } |
350 } |
353 |
351 |
354 /// \brief Add a new arc behind the current path. |
352 /// \brief Add a new arc behind the current path. |
504 private: |
502 private: |
505 const ListPath *path; |
503 const ListPath *path; |
506 Node *node; |
504 Node *node; |
507 }; |
505 }; |
508 |
506 |
509 /// \brief Gives back the nth arc. |
507 /// \brief The nth arc. |
510 /// |
508 /// |
511 /// Gives back the nth arc in O(n) time. |
509 /// This function looks for the nth arc in O(n) time. |
512 /// \pre n is in the [0..length() - 1] range |
510 /// \pre n is in the [0..length() - 1] range |
513 const Arc& nth(int n) const { |
511 const Arc& nth(int n) const { |
514 Node *node = first; |
512 Node *node = first; |
515 for (int i = 0; i < n; ++i) { |
513 for (int i = 0; i < n; ++i) { |
516 node = node->next; |
514 node = node->next; |
536 ++len; |
534 ++len; |
537 } |
535 } |
538 return len; |
536 return len; |
539 } |
537 } |
540 |
538 |
541 /// \brief Returns whether the path is empty. |
539 /// \brief Return true if the path is empty. |
542 bool empty() const { return first == 0; } |
540 bool empty() const { return first == 0; } |
543 |
541 |
544 /// \brief Resets the path to an empty path. |
542 /// \brief Reset the path to an empty one. |
545 void clear() { |
543 void clear() { |
546 while (first != 0) { |
544 while (first != 0) { |
547 last = first->next; |
545 last = first->next; |
548 alloc.destroy(first); |
546 alloc.destroy(first); |
549 alloc.deallocate(first, 1); |
547 alloc.deallocate(first, 1); |
550 first = last; |
548 first = last; |
551 } |
549 } |
552 } |
550 } |
553 |
551 |
554 /// \brief Gives back the first arc of the path |
552 /// \brief The first arc of the path |
555 const Arc& front() const { |
553 const Arc& front() const { |
556 return first->arc; |
554 return first->arc; |
557 } |
555 } |
558 |
556 |
559 /// \brief Add a new arc before the current path |
557 /// \brief Add a new arc before the current path |
615 } |
613 } |
616 alloc.destroy(node); |
614 alloc.destroy(node); |
617 alloc.deallocate(node, 1); |
615 alloc.deallocate(node, 1); |
618 } |
616 } |
619 |
617 |
620 /// \brief Splicing the given path to the current path. |
618 /// \brief Splice a path to the back of the current path. |
621 /// |
619 /// |
622 /// It splices the \c tpath to the back of the current path and \c |
620 /// It splices \c tpath to the back of the current path and \c |
623 /// tpath becomes empty. The time complexity of this function is |
621 /// tpath becomes empty. The time complexity of this function is |
624 /// O(1). |
622 /// O(1). |
625 void spliceBack(ListPath& tpath) { |
623 void spliceBack(ListPath& tpath) { |
626 if (first) { |
624 if (first) { |
627 if (tpath.first) { |
625 if (tpath.first) { |
634 last = tpath.last; |
632 last = tpath.last; |
635 } |
633 } |
636 tpath.first = tpath.last = 0; |
634 tpath.first = tpath.last = 0; |
637 } |
635 } |
638 |
636 |
639 /// \brief Splicing the given path to the current path. |
637 /// \brief Splice a path to the front of the current path. |
640 /// |
638 /// |
641 /// It splices the \c tpath before the current path and \c tpath |
639 /// It splices \c tpath before the current path and \c tpath |
642 /// becomes empty. The time complexity of this function |
640 /// becomes empty. The time complexity of this function |
643 /// is O(1). |
641 /// is O(1). |
644 void spliceFront(ListPath& tpath) { |
642 void spliceFront(ListPath& tpath) { |
645 if (first) { |
643 if (first) { |
646 if (tpath.first) { |
644 if (tpath.first) { |
653 last = tpath.last; |
651 last = tpath.last; |
654 } |
652 } |
655 tpath.first = tpath.last = 0; |
653 tpath.first = tpath.last = 0; |
656 } |
654 } |
657 |
655 |
658 /// \brief Splicing the given path into the current path. |
656 /// \brief Splice a path into the current path. |
659 /// |
657 /// |
660 /// It splices the \c tpath into the current path before the |
658 /// It splices the \c tpath into the current path before the |
661 /// position of \c it iterator and \c tpath becomes empty. The |
659 /// position of \c it iterator and \c tpath becomes empty. The |
662 /// time complexity of this function is O(1). If the \c it is \c |
660 /// time complexity of this function is O(1). If the \c it is |
663 /// INVALID then it will splice behind the current path. |
661 /// \c INVALID then it will splice behind the current path. |
664 void splice(ArcIt it, ListPath& tpath) { |
662 void splice(ArcIt it, ListPath& tpath) { |
665 if (it.node) { |
663 if (it.node) { |
666 if (tpath.first) { |
664 if (tpath.first) { |
667 tpath.first->prev = it.node->prev; |
665 tpath.first->prev = it.node->prev; |
668 if (it.node->prev) { |
666 if (it.node->prev) { |
686 } |
684 } |
687 } |
685 } |
688 tpath.first = tpath.last = 0; |
686 tpath.first = tpath.last = 0; |
689 } |
687 } |
690 |
688 |
691 /// \brief Spliting the current path. |
689 /// \brief Split the current path. |
692 /// |
690 /// |
693 /// It splits the current path into two parts. The part before \c |
691 /// It splits the current path into two parts. The part before |
694 /// it iterator will remain in the current path and the part from |
692 /// the iterator \c it will remain in the current path and the part |
695 /// the it will put into the \c tpath. If the \c tpath had arcs |
693 /// starting with |
696 /// before the operation they will be removed first. The time |
694 /// \c it will put into \c tpath. If \c tpath have arcs |
697 /// complexity of this function is O(1) plus the clearing of \c |
695 /// before the operation they are removed first. The time |
698 /// tpath. If the \c it is \c INVALID then it just clears \c |
696 /// complexity of this function is O(1) plus the the time of emtying |
699 /// tpath. |
697 /// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
700 void split(ArcIt it, ListPath& tpath) { |
698 void split(ArcIt it, ListPath& tpath) { |
701 tpath.clear(); |
699 tpath.clear(); |
702 if (it.node) { |
700 if (it.node) { |
703 tpath.first = it.node; |
701 tpath.first = it.node; |
704 tpath.last = last; |
702 tpath.last = last; |
736 /// A structure for representing directed path in a digraph. |
734 /// A structure for representing directed path in a digraph. |
737 /// \param Digraph The digraph type in which the path is. |
735 /// \param Digraph The digraph type in which the path is. |
738 /// |
736 /// |
739 /// In a sense, the path can be treated as a list of arcs. The |
737 /// In a sense, the path can be treated as a list of arcs. The |
740 /// lemon path type stores just this list. As a consequence it |
738 /// lemon path type stores just this list. As a consequence it |
741 /// cannot enumerate the nodes in the path and the zero length paths |
739 /// cannot enumerate the nodes in the path and the source node of |
742 /// cannot store the source. |
740 /// a zero length path is undefined. |
743 /// |
741 /// |
744 /// This implementation is completly static, so it cannot be |
742 /// This implementation is completly static, i.e. it can be copy constucted |
745 /// modified exclude the assign an other path. It is intented to be |
743 /// or copy assigned from another path, but otherwise it cannot be |
746 /// used when you want to store a large number of paths because it is |
744 /// modified. |
747 /// the most memory efficient path type in the lemon. |
745 /// |
|
746 /// Being the the most memory efficient path type in LEMON, |
|
747 /// it is intented to be |
|
748 /// used when you want to store a large number of paths. |
748 template <typename _Digraph> |
749 template <typename _Digraph> |
749 class StaticPath { |
750 class StaticPath { |
750 public: |
751 public: |
751 |
752 |
752 typedef _Digraph Digraph; |
753 typedef _Digraph Digraph; |
757 /// Default constructor |
758 /// Default constructor |
758 StaticPath() : len(0), arcs(0) {} |
759 StaticPath() : len(0), arcs(0) {} |
759 |
760 |
760 /// \brief Template copy constructor |
761 /// \brief Template copy constructor |
761 /// |
762 /// |
762 /// This path can be initialized with any other path type. It just |
763 /// This path can be initialized from any other path type. |
763 /// makes a copy of the given path. |
|
764 template <typename CPath> |
764 template <typename CPath> |
765 StaticPath(const CPath& cpath) : arcs(0) { |
765 StaticPath(const CPath& cpath) : arcs(0) { |
766 copyPath(*this, cpath); |
766 copyPath(*this, cpath); |
767 } |
767 } |
768 |
768 |
773 if (arcs) delete[] arcs; |
773 if (arcs) delete[] arcs; |
774 } |
774 } |
775 |
775 |
776 /// \brief Template copy assignment |
776 /// \brief Template copy assignment |
777 /// |
777 /// |
778 /// This path can be initialized with any other path type. It just |
778 /// This path can be made equal to any other path type. It simply |
779 /// makes a copy of the given path. |
779 /// makes a copy of the given path. |
780 template <typename CPath> |
780 template <typename CPath> |
781 StaticPath& operator=(const CPath& cpath) { |
781 StaticPath& operator=(const CPath& cpath) { |
782 copyPath(*this, cpath); |
782 copyPath(*this, cpath); |
783 return *this; |
783 return *this; |
829 private: |
829 private: |
830 const StaticPath *path; |
830 const StaticPath *path; |
831 int idx; |
831 int idx; |
832 }; |
832 }; |
833 |
833 |
834 /// \brief Gives back the nth arc. |
834 /// \brief The nth arc. |
835 /// |
835 /// |
836 /// \pre n is in the [0..length() - 1] range |
836 /// \pre n is in the [0..length() - 1] range |
837 const Arc& nth(int n) const { |
837 const Arc& nth(int n) const { |
838 return arcs[n]; |
838 return arcs[n]; |
839 } |
839 } |
840 |
840 |
841 /// \brief Initializes arc iterator to point to the nth arc. |
841 /// \brief The arc iterator pointing to the nth arc. |
842 ArcIt nthIt(int n) const { |
842 ArcIt nthIt(int n) const { |
843 return ArcIt(*this, n); |
843 return ArcIt(*this, n); |
844 } |
844 } |
845 |
845 |
846 /// \brief Gives back the length of the path. |
846 /// \brief The length of the path. |
847 int length() const { return len; } |
847 int length() const { return len; } |
848 |
848 |
849 /// \brief Returns true when the path is empty. |
849 /// \brief Return true when the path is empty. |
850 int empty() const { return len == 0; } |
850 int empty() const { return len == 0; } |
851 |
851 |
852 /// \break Erase all arc in the digraph. |
852 /// \break Erase all arcs in the digraph. |
853 void clear() { |
853 void clear() { |
854 len = 0; |
854 len = 0; |
855 if (arcs) delete[] arcs; |
855 if (arcs) delete[] arcs; |
856 arcs = 0; |
856 arcs = 0; |
857 } |
857 } |
858 |
858 |
859 /// \brief Gives back the first arc of the path. |
859 /// \brief The first arc of the path. |
860 const Arc& front() const { |
860 const Arc& front() const { |
861 return arcs[0]; |
861 return arcs[0]; |
862 } |
862 } |
863 |
863 |
864 /// \brief Gives back the last arc of the path. |
864 /// \brief The last arc of the path. |
865 const Arc& back() const { |
865 const Arc& back() const { |
866 return arcs[len - 1]; |
866 return arcs[len - 1]; |
867 } |
867 } |
868 |
868 |
869 |
869 |