Home | History | Annotate | Download | only in slicer
      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 #pragma once
     18 
     19 #include "common.h"
     20 
     21 #include <stdlib.h>
     22 
     23 namespace slicer {
     24 
     25 // A shallow array view
     26 template <class T>
     27 class ArrayView {
     28  public:
     29   ArrayView() = default;
     30 
     31   ArrayView(const ArrayView&) = default;
     32   ArrayView& operator=(const ArrayView&) = default;
     33 
     34   ArrayView(T* ptr, size_t count) : begin_(ptr), end_(ptr + count) {}
     35 
     36   T* begin() const { return begin_; }
     37   T* end() const { return end_; }
     38 
     39   T* data() const { return begin_; }
     40 
     41   T& operator[](size_t i) const {
     42     SLICER_CHECK(i < size());
     43     return *(begin_ + i);
     44   }
     45 
     46   size_t size() const { return end_ - begin_; }
     47   bool empty() const { return begin_ == end_; }
     48 
     49  private:
     50   T* begin_ = nullptr;
     51   T* end_ = nullptr;
     52 };
     53 
     54 } // namespace slicer
     55 
     56