1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
8 /*****************************************************************************/
10 // This is an array packed into some kind of integral data type
11 // storagetype is the type (integral) which your array is going to be packed into
12 // itemtype is the type of array elements
13 // bits_per_element is size of the elements in bits
14 template<class storageType, class itemType, int bits_per_element>
18 // operator[] returns a 'ref' (usually a ref to the element type)
19 // This presents a problem if you wanted to implement something like a
20 // bitvector via this packed array, because you cannot make a ref to
22 // The trick is you define something that acts like a ref (TinyArrayRef in this case)
23 // which for our purposes means you can assign to and from it and our chosen
28 // this is really the getter for the array.
31 storageType mask = ((1 << bits_per_element) - 1);
32 int shift = bits_per_element * index;
34 itemType result = (itemType)((*data >> shift) & mask);
38 void operator =(const itemType b)
40 storageType mask = ((1 << bits_per_element) - 1);
41 assert(itemType(b&mask) == b);
43 mask <<= bits_per_element * index;
46 *data |= b << (bits_per_element * index);
48 friend class TinyArray;
50 TinyArrayRef(storageType *d, int idx) : data(d), index(idx) {}
60 void clear() { data = 0; }
62 TinyArrayRef operator [](unsigned int n)
64 assert((n+1) * bits_per_element <= sizeof(itemType) * 8);
65 return TinyArrayRef(&data, n);
67 // only use this for clearing it
68 void operator=(void *rhs)