changes: TINF-175 TZPC-4722
[platform/upstream/libgee.git] / gee / unfolditerator.vala
1 /* unfolditerator.vala
2  *
3  * Copyright (C) 2011  Maciej Piechotka
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18  *
19  * Author:
20  *      Maciej Piechotka <uzytkownik2@gmail.com>
21  */
22
23 internal class Gee.UnfoldIterator<G> : Object, Traversable<G>, Iterator<G> {
24         public UnfoldIterator (owned UnfoldFunc<G> func, owned Lazy<G>? current = null) {
25                 _current = (owned)current;
26                 _func = (owned)func;
27                 _end = false;
28         }
29
30         public bool next () {
31                 if (has_next ()) {
32                         if (_current != null)
33                                 _current.eval ();
34                         _current = (owned)_next;
35                         return true;
36                 }
37                 return false;
38         }
39
40         public bool has_next () {
41                 if (_end)
42                         return false;
43                 if (_next != null)
44                         return true;
45                 _next = _func ();
46                 if (_next == null)
47                         _end = true;
48                 return _next != null;
49         }
50
51         public new G get () {
52                 assert (_current != null);
53                 return _current.value;
54         }
55
56         public void remove () {
57                 assert_not_reached ();
58         }
59
60         public bool valid { get { return _current != null; } }
61         public bool read_only { get { return true; } }
62
63         public bool foreach (ForallFunc<G> f) {
64                 if (_current != null) {
65                         if (!f (_current.value)) {
66                                 return false;
67                         }
68                 }
69                 if (_next != null) {
70                         _current = (owned)_next;
71                         if (!f (_current.value)) {
72                                 return false;
73                         }
74                 } else if (_end) {
75                         return true;
76                 }
77                 if (_current == null) {
78                         _current = _func ();
79                         if (_current == null) {
80                                 _end = true;
81                                 return true;
82                         } else {
83                                 if (!f (_current.value)) {
84                                         return false;
85                                 }
86                         }
87                 }
88                 while ((_next = _func ()) != null) {
89                         _current = (owned)_next;
90                         if (!f (_current.value)) {
91                                 return false;
92                         }
93                 }
94                 _end = true;
95                 return true;
96         }
97
98         private UnfoldFunc<G> _func;
99         private Lazy<G>? _current;
100         private Lazy<G>? _next;
101         private bool _end;
102 }