Bug 1874684 - Part 38: Enable now passing tests. r=allstarschh
[gecko.git] / mfbt / RangedArray.h
blob4417e09e9d3c953be99846bd10b970d894bac813
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/. */
7 /*
8 * A compile-time constant-length array, with bounds-checking assertions -- but
9 * unlike mozilla::Array, with indexes biased by a constant.
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).
15 #ifndef mozilla_RangedArray_h
16 #define mozilla_RangedArray_h
18 #include "mozilla/Array.h"
20 namespace mozilla {
22 template <typename T, size_t MinIndex, size_t Length>
23 class RangedArray {
24 private:
25 typedef Array<T, Length> ArrayType;
26 ArrayType mArr;
28 public:
29 static size_t length() { return Length; }
30 static size_t minIndex() { return MinIndex; }
32 T& operator[](size_t aIndex) {
33 MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
34 return mArr[aIndex - MinIndex];
37 const T& operator[](size_t aIndex) const {
38 MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
39 return mArr[aIndex - MinIndex];
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;
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(); }
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(); }
64 } // namespace mozilla
66 #endif // mozilla_RangedArray_h