Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: courgette/consecutive_range_visitor.h

Issue 1491703003: [Courgette] Initial Implementation of LabelManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « courgette/assembly_program.h ('k') | courgette/consecutive_range_visitor_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "base/macros.h"
11
12 namespace courgette {
13
14 // Usage note: First check whether std::unique() would suffice.
15 //
16 // ConsecutiveRangeVisitor is a visitor to read equal consecutive items
17 // ("ranges") between two iterators. The base value of InputIterator must
18 // implement the == operator.
19 //
20 // Example: "AAAAABZZZZOO" consists of ranges ["AAAAA", "B", "ZZZZ", "OO"]. The
21 // visitor provides accessors to iterate through the ranges, and to access each
22 // range's value and repeat, i.e., [('A', 5), ('B', 1), ('Z', 4), ('O', 2)].
23 template <class InputIterator>
24 class ConsecutiveRangeVisitor {
25 public:
26 ConsecutiveRangeVisitor(InputIterator begin, InputIterator end)
27 : head_(begin), end_(end) {
28 advance();
29 }
30
31 // Returns whether there are more ranges to traverse.
32 bool has_more() const { return tail_ != end_; }
33
34 // Returns an iterator to an element in the current range.
35 InputIterator cur() const { return tail_; }
36
37 // Returns the number of repeated elements in the current range.
38 size_t repeat() const { return std::distance(tail_, head_); }
39
40 // Advances to the next range.
41 void advance() {
42 tail_ = head_;
43 if (head_ != end_)
44 while (++head_ != end_ && *head_ == *tail_) {}
45 }
46
47 private:
48 InputIterator tail_; // The trailing pionter of a range (inclusive).
49 InputIterator head_; // The leading pointer of a range (exclusive).
50 InputIterator end_; // Store the end pointer so we know when to stop.
51
52 DISALLOW_COPY_AND_ASSIGN(ConsecutiveRangeVisitor);
53 };
54
55 } // namespace courgette
56
57 #endif // COURGETTE_CONSECUTIVE_RANGE_VISITOR_H_
OLDNEW
« no previous file with comments | « courgette/assembly_program.h ('k') | courgette/consecutive_range_visitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698