Bug 1610775 [wpt PR 21336] - Update urllib3 to 1.25.8, a=testonly
[gecko.git] / mfbt / RangedArray.h
blob48b6680c9c16f37b18be3d1ad7ef74ad50fdb59c
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 T& operator[](size_t aIndex) {
30 MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
31 return mArr[aIndex - MinIndex];
34 const T& operator[](size_t aIndex) const {
35 MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
36 return mArr[aIndex - MinIndex];
39 typedef typename ArrayType::iterator iterator;
40 typedef typename ArrayType::const_iterator const_iterator;
41 typedef typename ArrayType::reverse_iterator reverse_iterator;
42 typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
44 // Methods for range-based for loops.
45 iterator begin() { return mArr.begin(); }
46 const_iterator begin() const { return mArr.begin(); }
47 const_iterator cbegin() const { return mArr.cbegin(); }
48 iterator end() { return mArr.end(); }
49 const_iterator end() const { return mArr.end(); }
50 const_iterator cend() const { return mArr.cend(); }
52 // Methods for reverse iterating.
53 reverse_iterator rbegin() { return mArr.rbegin(); }
54 const_reverse_iterator rbegin() const { return mArr.rbegin(); }
55 const_reverse_iterator crbegin() const { return mArr.crbegin(); }
56 reverse_iterator rend() { return mArr.rend(); }
57 const_reverse_iterator rend() const { return mArr.rend(); }
58 const_reverse_iterator crend() const { return mArr.crend(); }
61 } // namespace mozilla
63 #endif // mozilla_RangedArray_h