mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
64 lines
2.1 KiB
C++
64 lines
2.1 KiB
C++
//===- ArrayRefView.h - Adapter for iterating over an ArrayRef --*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines ArrayRefView, a template class which provides a
|
|
// proxied view of the elements of an array.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_BASIC_ARRAYREFVIEW_H
|
|
#define SWIFT_BASIC_ARRAYREFVIEW_H
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
namespace swift {
|
|
|
|
/// An adapter for iterating over a range of values as a range of
|
|
/// values of a different type.
|
|
template <class Orig, class Projected, Projected (&Project)(const Orig &)>
|
|
class ArrayRefView {
|
|
ArrayRef<Orig> Array;
|
|
public:
|
|
ArrayRefView(ArrayRef<Orig> array) : Array(array) {}
|
|
|
|
class iterator {
|
|
friend class ArrayRefView<Orig,Projected,Project>;
|
|
const Orig *Ptr;
|
|
iterator(const Orig *ptr) : Ptr(ptr) {}
|
|
public:
|
|
Projected operator*() const { return Project(*Ptr); }
|
|
iterator &operator++() { Ptr++; return *this; }
|
|
iterator operator++(int) { return iterator(Ptr++); }
|
|
bool operator==(iterator rhs) { return Ptr == rhs.Ptr; }
|
|
bool operator!=(iterator rhs) { return Ptr != rhs.Ptr; }
|
|
};
|
|
iterator begin() const { return iterator(Array.begin()); }
|
|
iterator end() const { return iterator(Array.end()); }
|
|
|
|
bool empty() const { return Array.empty(); }
|
|
size_t size() const { return Array.size(); }
|
|
Projected operator[](unsigned i) const { return Project(Array[i]); }
|
|
Projected front() const { return Project(Array.front()); }
|
|
Projected back() const { return Project(Array.back()); }
|
|
|
|
ArrayRefView slice(unsigned start) const {
|
|
return ArrayRefView(Array.slice(start));
|
|
}
|
|
ArrayRefView slice(unsigned start, unsigned length) const {
|
|
return ArrayRefView(Array.slice(start, length));
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|