[base] Add iterator_range helper class.

TEST=unittests
R=titzer@chromium.org

Review URL: https://codereview.chromium.org/810683003

Cr-Commit-Position: refs/heads/master@{#25834}
This commit is contained in:
Benedikt Meurer 2014-12-16 08:31:46 +01:00
parent ab7622a094
commit 54a31b628d
5 changed files with 120 additions and 0 deletions

View File

@ -1262,6 +1262,7 @@ source_set("v8_libbase") {
"src/base/flags.h",
"src/base/functional.cc",
"src/base/functional.h",
"src/base/iterator.h",
"src/base/lazy-instance.h",
"src/base/logging.cc",
"src/base/logging.h",

56
src/base/iterator.h Normal file
View File

@ -0,0 +1,56 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_BASE_ITERATOR_H_
#define V8_BASE_ITERATOR_H_
#include <iterator>
#include "src/base/macros.h"
namespace v8 {
namespace base {
// The intention of the base::iterator_range class is to encapsulate two
// iterators so that the range defined by the iterators can be used like
// a regular STL container (actually only a subset of the full container
// functionality is available usually).
template <typename ForwardIterator>
class iterator_range {
public:
typedef ForwardIterator iterator;
typedef ForwardIterator const_iterator;
typedef typename std::iterator_traits<iterator>::pointer pointer;
typedef typename std::iterator_traits<iterator>::reference reference;
typedef typename std::iterator_traits<iterator>::value_type value_type;
typedef
typename std::iterator_traits<iterator>::difference_type difference_type;
iterator_range() : begin_(), end_() {}
template <typename ForwardIterator2>
iterator_range(ForwardIterator2 const& begin, ForwardIterator2 const& end)
: begin_(begin), end_(end) {}
iterator begin() { return begin_; }
iterator end() { return end_; }
const_iterator begin() const { return begin_; }
const_iterator end() const { return end_; }
const_iterator cbegin() const { return begin_; }
const_iterator cend() const { return end_; }
bool empty() const { return cbegin() == cend(); }
// Random Access iterators only.
reference operator[](difference_type n) { return begin()[n]; }
difference_type size() const { return cend() - cbegin(); }
private:
const_iterator const begin_;
const_iterator const end_;
};
} // namespace base
} // namespace v8
#endif // V8_BASE_ITERATOR_H_

View File

@ -0,0 +1,61 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/base/iterator.h"
#include <deque>
#include "test/unittests/test-utils.h"
namespace v8 {
namespace base {
TEST(IteratorTest, IteratorRangeEmpty) {
base::iterator_range<char*> r;
EXPECT_EQ(r.begin(), r.end());
EXPECT_EQ(r.end(), r.cend());
EXPECT_EQ(r.begin(), r.cbegin());
EXPECT_TRUE(r.empty());
EXPECT_EQ(0, r.size());
}
TEST(IteratorTest, IteratorRangeArray) {
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
base::iterator_range<int*> r1(&array[0], &array[10]);
for (auto i : r1) {
EXPECT_EQ(array[i], i);
}
EXPECT_EQ(10, r1.size());
EXPECT_FALSE(r1.empty());
for (size_t i = 0; i < arraysize(array); ++i) {
EXPECT_EQ(r1[i], array[i]);
}
base::iterator_range<int*> r2(&array[0], &array[0]);
EXPECT_EQ(0, r2.size());
EXPECT_TRUE(r2.empty());
for (auto i : array) {
EXPECT_EQ(r2.end(), std::find(r2.begin(), r2.end(), i));
}
}
TEST(IteratorTest, IteratorRangeDeque) {
typedef std::deque<unsigned> C;
C c;
c.push_back(1);
c.push_back(2);
c.push_back(2);
base::iterator_range<typename C::iterator> r(c.begin(), c.end());
EXPECT_EQ(3, r.size());
EXPECT_FALSE(r.empty());
EXPECT_TRUE(c.begin() == r.begin());
EXPECT_TRUE(c.end() == r.end());
EXPECT_EQ(0, std::count(r.begin(), r.end(), 0));
EXPECT_EQ(1, std::count(r.begin(), r.end(), 1));
EXPECT_EQ(2, std::count(r.begin(), r.end(), 2));
}
} // namespace base
} // namespace v8

View File

@ -28,6 +28,7 @@
'base/division-by-constant-unittest.cc',
'base/flags-unittest.cc',
'base/functional-unittest.cc',
'base/iterator-unittest.cc',
'base/platform/condition-variable-unittest.cc',
'base/platform/mutex-unittest.cc',
'base/platform/platform-unittest.cc',

View File

@ -1263,6 +1263,7 @@
'../../src/base/flags.h',
'../../src/base/functional.cc',
'../../src/base/functional.h',
'../../src/base/iterator.h',
'../../src/base/lazy-instance.h',
'../../src/base/logging.cc',
'../../src/base/logging.h',