OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COURGETTE_CONSECUTIVE_RANGE_VISITOR_H_ | |
6 #define COURGETTE_CONSECUTIVE_RANGE_VISITOR_H_ | |
7 | |
8 #include <iterator> | |
9 | |
10 namespace courgette { | |
11 | |
12 // Before using this, check whether std::unique() is a better fit. | |
13 // | |
14 // ConsecutiveRangeVisitor is a visitor to read equal consecutive items | |
15 // ("ranges") between two iterators. The base value of InputIterator must | |
16 // implement the == operator. | |
17 // | |
18 // Example, "AAAAABZZZZOO" consists of ranges ["AAAAA", "B", "ZZZZ", "OO"]. The | |
19 // visitor provides accessors to iterate through the ranges, and to access each | |
20 // range's value and repeat, i.e., [('A', 5), ('B', 1), ('Z', 4), ('O', 2)]. | |
21 template<class InputIterator> | |
22 class ConsecutiveRangeVisitor { | |
23 public: | |
24 ConsecutiveRangeVisitor(InputIterator begin, InputIterator end) | |
25 : head_(begin), end_(end) { | |
26 advance(); | |
27 } | |
28 | |
29 // Returns whether there are more ranges to traverse. | |
30 bool has_more() const { return tail_ != end_; } | |
31 | |
32 // Returns an iterator to an element in the current range. | |
33 InputIterator cur() const { return tail_; } | |
34 | |
35 // Returns the number of repeated elements in the current range. | |
36 size_t repeat() const { return std::distance(tail_, head_); } | |
37 | |
38 // Advances to the next range. | |
39 void advance() { | |
40 tail_ = head_; | |
41 if (head_ != end_) { | |
42 ++head_; | |
43 while (head_ != end_ && *head_ == *tail_) | |
44 ++head_; | |
45 } | |
46 } | |
47 | |
48 private: | |
49 InputIterator tail_; | |
grt (UTC plus 2)
2015/12/02 19:03:40
please add lightweight comments
huangs
2015/12/02 20:51:01
Done.
| |
50 InputIterator head_; | |
51 InputIterator end_; | |
52 }; | |
grt (UTC plus 2)
2015/12/02 19:03:40
DISALLOW_COPY_AND_ASSIGN?
huangs
2015/12/02 20:51:02
I omitted this since I figured a visitor should be
| |
53 | |
54 } // namespace courgette | |
55 | |
56 #endif // COURGETTE_CONSECUTIVE_RANGE_VISITOR_H_ | |
OLD | NEW |