1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_COMMON_VECTOR_SPAN_H_
18 #define LIBTEXTCLASSIFIER_COMMON_VECTOR_SPAN_H_
19 
20 #include <vector>
21 
22 namespace libtextclassifier {
23 
24 // StringPiece analogue for std::vector<T>.
25 template <class T>
26 class VectorSpan {
27  public:
VectorSpan()28   VectorSpan() : begin_(), end_() {}
VectorSpan(const std::vector<T> & v)29   VectorSpan(const std::vector<T>& v)  // NOLINT(runtime/explicit)
30       : begin_(v.begin()), end_(v.end()) {}
VectorSpan(typename std::vector<T>::const_iterator begin,typename std::vector<T>::const_iterator end)31   VectorSpan(typename std::vector<T>::const_iterator begin,
32              typename std::vector<T>::const_iterator end)
33       : begin_(begin), end_(end) {}
34 
35   const T& operator[](typename std::vector<T>::size_type i) const {
36     return *(begin_ + i);
37   }
38 
size()39   int size() const { return end_ - begin_; }
begin()40   typename std::vector<T>::const_iterator begin() const { return begin_; }
end()41   typename std::vector<T>::const_iterator end() const { return end_; }
42 
43  private:
44   typename std::vector<T>::const_iterator begin_;
45   typename std::vector<T>::const_iterator end_;
46 };
47 
48 }  // namespace libtextclassifier
49 
50 #endif  // LIBTEXTCLASSIFIER_COMMON_VECTOR_SPAN_H_
51