tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RangedArray.h (2285B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /*
      8 * A compile-time constant-length array, with bounds-checking assertions -- but
      9 * unlike mozilla::Array, with indexes biased by a constant.
     10 *
     11 * Thus where mozilla::Array<int, 3> is a three-element array indexed by [0, 3),
     12 * mozilla::RangedArray<int, 8, 3> is a three-element array indexed by [8, 11).
     13 */
     14 
     15 #ifndef mozilla_RangedArray_h
     16 #define mozilla_RangedArray_h
     17 
     18 #include "mozilla/Array.h"
     19 
     20 namespace mozilla {
     21 
     22 template <typename T, size_t MinIndex, size_t Length>
     23 class RangedArray {
     24 private:
     25  typedef Array<T, Length> ArrayType;
     26  ArrayType mArr;
     27 
     28 public:
     29  static size_t length() { return Length; }
     30  static size_t minIndex() { return MinIndex; }
     31 
     32  T& operator[](size_t aIndex) {
     33    MOZ_ASSERT(aIndex >= MinIndex);
     34    return mArr[aIndex - MinIndex];
     35  }
     36 
     37  const T& operator[](size_t aIndex) const {
     38    MOZ_ASSERT(aIndex >= MinIndex);
     39    return mArr[aIndex - MinIndex];
     40  }
     41 
     42  typedef typename ArrayType::iterator iterator;
     43  typedef typename ArrayType::const_iterator const_iterator;
     44  typedef typename ArrayType::reverse_iterator reverse_iterator;
     45  typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
     46 
     47  // Methods for range-based for loops.
     48  iterator begin() { return mArr.begin(); }
     49  const_iterator begin() const { return mArr.begin(); }
     50  const_iterator cbegin() const { return mArr.cbegin(); }
     51  iterator end() { return mArr.end(); }
     52  const_iterator end() const { return mArr.end(); }
     53  const_iterator cend() const { return mArr.cend(); }
     54 
     55  // Methods for reverse iterating.
     56  reverse_iterator rbegin() { return mArr.rbegin(); }
     57  const_reverse_iterator rbegin() const { return mArr.rbegin(); }
     58  const_reverse_iterator crbegin() const { return mArr.crbegin(); }
     59  reverse_iterator rend() { return mArr.rend(); }
     60  const_reverse_iterator rend() const { return mArr.rend(); }
     61  const_reverse_iterator crend() const { return mArr.crend(); }
     62 };
     63 
     64 }  // namespace mozilla
     65 
     66 #endif  // mozilla_RangedArray_h