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/. */
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"
22 template <typename T
, size_t MinIndex
, size_t Length
>
25 typedef Array
<T
, Length
> ArrayType
;
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