Bug 1216041 - Add ranged iterator support to mozilla::{Array,RangedArray,EnumerationArray}. r=froydnj

This commit is contained in:
Cameron McCormack 2015-10-20 10:16:20 +11:00
parent c0513349d0
commit e55561d84e
4 changed files with 76 additions and 7 deletions

View File

@ -11,6 +11,7 @@
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/ReverseIterator.h"
#include <stddef.h>
@ -33,6 +34,27 @@ public:
MOZ_ASSERT(aIndex < Length);
return mArr[aIndex];
}
typedef T* iterator;
typedef const T* const_iterator;
typedef ReverseIterator<T*> reverse_iterator;
typedef ReverseIterator<const T*> const_reverse_iterator;
// Methods for range-based for loops.
iterator begin() { return mArr; }
const_iterator begin() const { return mArr; }
const_iterator cbegin() const { return begin(); }
iterator end() { return mArr + Length; }
const_iterator end() const { return mArr + Length; }
const_iterator cend() const { return end(); }
// Methods for reverse iterating.
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
const_reverse_iterator crbegin() const { return rbegin(); }
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
const_reverse_iterator crend() const { return rend(); }
};
template<typename T>

View File

@ -46,7 +46,9 @@ public:
static const size_t kSize = size_t(SizeAsEnumValue);
private:
Array<ValueType, kSize> mArray;
typedef Array<ValueType, kSize> ArrayType;
ArrayType mArray;
public:
EnumeratedArray() {}
@ -67,6 +69,27 @@ public:
{
return mArray[size_t(aIndex)];
}
typedef typename ArrayType::iterator iterator;
typedef typename ArrayType::const_iterator const_iterator;
typedef typename ArrayType::reverse_iterator reverse_iterator;
typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
// Methods for range-based for loops.
iterator begin() { return mArray.begin(); }
const_iterator begin() const { return mArray.begin(); }
const_iterator cbegin() const { return mArray.cbegin(); }
iterator end() { return mArray.end(); }
const_iterator end() const { return mArray.end(); }
const_iterator cend() const { return mArray.cend(); }
// Methods for reverse iterating.
reverse_iterator rbegin() { return mArray.rbegin(); }
const_reverse_iterator rbegin() const { return mArray.rbegin(); }
const_reverse_iterator crbegin() const { return mArray.crbegin(); }
reverse_iterator rend() { return mArray.rend(); }
const_reverse_iterator rend() const { return mArray.rend(); }
const_reverse_iterator crend() const { return mArray.crend(); }
};
} // namespace mozilla

View File

@ -195,12 +195,14 @@ MakeEnumeratedRange(EnumType aBegin, EnumType aEnd)
// Since the enumeration starts at EnumType(0), we know for sure that the values
// will be in range of our deduced IntType.
template<typename EnumType>
inline detail::EnumeratedRange<UnsignedStdintTypeForSize<sizeof(EnumType)>::Type,
EnumType>
inline detail::EnumeratedRange<
typename UnsignedStdintTypeForSize<sizeof(EnumType)>::Type,
EnumType>
MakeEnumeratedRange(EnumType aEnd)
{
return MakeEnumeratedRange<UnsignedStdintTypeForSize<sizeof(EnumType)>::Type>(
EnumType(0), aEnd);
return MakeEnumeratedRange<
typename UnsignedStdintTypeForSize<sizeof(EnumType)>::Type>(EnumType(0),
aEnd);
}
#ifdef __GNUC__

View File

@ -22,6 +22,10 @@ namespace mozilla {
template<typename T, size_t MinIndex, size_t Length>
class RangedArray
{
private:
typedef Array<T, Length> ArrayType;
ArrayType mArr;
public:
T& operator[](size_t aIndex)
{
@ -35,8 +39,26 @@ public:
return mArr[aIndex - MinIndex];
}
private:
Array<T, Length> mArr;
typedef typename ArrayType::iterator iterator;
typedef typename ArrayType::const_iterator const_iterator;
typedef typename ArrayType::reverse_iterator reverse_iterator;
typedef typename ArrayType::const_reverse_iterator const_reverse_iterator;
// Methods for range-based for loops.
iterator begin() { return mArr.begin(); }
const_iterator begin() const { return mArr.begin(); }
const_iterator cbegin() const { return mArr.cbegin(); }
iterator end() { return mArr.end(); }
const_iterator end() const { return mArr.end(); }
const_iterator cend() const { return mArr.cend(); }
// Methods for reverse iterating.
reverse_iterator rbegin() { return mArr.rbegin(); }
const_reverse_iterator rbegin() const { return mArr.rbegin(); }
const_reverse_iterator crbegin() const { return mArr.crbegin(); }
reverse_iterator rend() { return mArr.rend(); }
const_reverse_iterator rend() const { return mArr.rend(); }
const_reverse_iterator crend() const { return mArr.crend(); }
};
} // namespace mozilla