Tulip  5.3.0
Large graphs analysis and drawing
ConcatIterator.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TULIP_CONCATITERATOR_H
21 #define TULIP_CONCATITERATOR_H
22 
23 #include <tulip/Iterator.h>
24 
25 namespace tlp {
26 
27 /**
28  * @brief This Iterator iterates over the sequence formed by the concatenation of the sequences it
29  *is given.
30  * @warning This class takes ownership of the Iterators it is given.
31  **/
32 template <class T>
33 struct ConcatIterator : public Iterator<T> {
34 
35  /**
36  * @brief Creates an Iterator that iterates over the concatenation of the two sequences it is
37  *given.
38  *
39  * @param itOne The first sequence to iterate upon.
40  * @param itTwo The second sequence, which will be iterated upon after the first sequence has been
41  *completely iterated upon.
42  **/
43  ConcatIterator(Iterator<T> *itOne, Iterator<T> *itTwo) : itOne(itOne), itTwo(itTwo) {}
44 
45  /**
46  * @brief Deletes the two iterators it was given at construction.
47  **/
48  ~ConcatIterator() override {
49  delete itOne;
50  delete itTwo;
51  }
52 
53  T next() override {
54  if (itOne->hasNext())
55  return itOne->next();
56  else {
57  return itTwo->next();
58  }
59  }
60 
61  bool hasNext() override {
62  return (itOne->hasNext() || itTwo->hasNext());
63  }
64 
65 private:
66  Iterator<T> *itOne;
67  Iterator<T> *itTwo;
68 };
69 
70 /**
71  * @brief Convenient function for creating a ConcatIterator.
72  * @ingroup Iterators
73  *
74  * @since Tulip 5.2
75  *
76  * Creates a ConcatIterator from two other Iterators.
77  * The returned Iterator takes ownership of the one provided as parameter.
78  *
79  * @param itOne the first input Iterator
80  * @param itTwo the second input Iterator
81  * @return a ConcatIterator
82  **/
83 template <class T>
85  return new ConcatIterator<T>(itOne, itTwo);
86 }
87 } // namespace tlp
88 #endif
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Graph.h:41
bool hasNext() override
Tells if the sequence is at its end.
T next() override
Moves the Iterator on the next element.
~ConcatIterator() override
Deletes the two iterators it was given at construction.
This Iterator iterates over the sequence formed by the concatenation of the sequences it is given...
ConcatIterator(Iterator< T > *itOne, Iterator< T > *itTwo)
Creates an Iterator that iterates over the concatenation of the two sequences it is given...
Iterator< T > * concatIterator(Iterator< T > *itOne, Iterator< T > *itTwo)
Convenient function for creating a ConcatIterator.