Bug 1147766 - Part 1: Add a mozilla::RangedArray class, for fixed length arrays with a non-zero base index. r=waldo
authorCameron McCormack <cam@mcc.id.au>
Sat, 23 May 2015 12:50:44 +1000
changeset 245399 3cb74142cd09fdb9b12d2a8bfb02be2ae0346f27
parent 245398 0be288d81a00557f7149c6bdd67450fe5c791750
child 245400 5d5bd8ed66c93249a375605660e20ada9d38e766
push id13144
push userphilringnalda@gmail.com
push dateSun, 24 May 2015 22:28:13 +0000
treeherderfx-team@7637b24cd7a5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs1147766
milestone41.0a1
Bug 1147766 - Part 1: Add a mozilla::RangedArray class, for fixed length arrays with a non-zero base index. r=waldo
mfbt/RangedArray.h
mfbt/moz.build
new file mode 100644
--- /dev/null
+++ b/mfbt/RangedArray.h
@@ -0,0 +1,44 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * A compile-time constant-length array, with bounds-checking assertions -- but
+ * unlike mozilla::Array, with indexes biased by a constant.
+ *
+ * Thus where mozilla::Array<int, 3> is a three-element array indexed by [0, 3),
+ * mozilla::RangedArray<int, 8, 3> is a three-element array indexed by [8, 11).
+ */
+
+#ifndef mozilla_RangedArray_h
+#define mozilla_RangedArray_h
+
+#include "mozilla/Array.h"
+
+namespace mozilla {
+
+template<typename T, size_t MinIndex, size_t Length>
+class RangedArray
+{
+public:
+  T& operator[](size_t aIndex)
+  {
+    MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
+    return mArr[aIndex - MinIndex];
+  }
+
+  const T& operator[](size_t aIndex) const
+  {
+    MOZ_ASSERT(aIndex == MinIndex || aIndex > MinIndex);
+    return mArr[aIndex - MinIndex];
+  }
+
+private:
+  Array<T, Length> mArr;
+};
+
+} // namespace mozilla
+
+#endif // mozilla_RangedArray_h
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -54,16 +54,17 @@ EXPORTS.mozilla = [
     'MemoryReporting.h',
     'Move.h',
     'NullPtr.h',
     'NumericLimits.h',
     'Pair.h',
     'PodOperations.h',
     'Poison.h',
     'Range.h',
+    'RangedArray.h',
     'RangedPtr.h',
     'RefCountType.h',
     'ReentrancyGuard.h',
     'RefPtr.h',
     'ReverseIterator.h',
     'RollingMean.h',
     'Scoped.h',
     'SegmentedVector.h',