Merge pull request #2885 from dotnet-bot/from-tfs
[platform/upstream/coreclr.git] / src / jit / tinyarray.h
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.
4
5 #ifndef TINYARRAY_H
6 #define TINYARRAY_H
7
8 /*****************************************************************************/
9
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>
15 class TinyArray
16 {
17 public:
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 
21     // the element type.
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
24     // element type.
25     class TinyArrayRef
26     {
27     public:
28         // this is really the getter for the array.
29         operator itemType() 
30         {
31             storageType mask = ((1 << bits_per_element) - 1);
32             int shift = bits_per_element * index;
33
34             itemType result = (itemType)((*data >> shift) & mask);
35             return result;
36         }
37
38         void operator =(const itemType b)
39         {
40             storageType mask = ((1 << bits_per_element) - 1);
41             assert(itemType(b&mask) == b);
42
43             mask <<= bits_per_element * index;
44
45             *data &= ~mask;
46             *data |= b << (bits_per_element * index);
47         }
48         friend class TinyArray;
49     protected:
50         TinyArrayRef(storageType *d, int idx) : data(d), index(idx) {}
51
52         storageType *data;
53         int index;
54         
55     };
56
57
58     storageType data;
59
60     void clear() { data = 0; }
61
62     TinyArrayRef operator [](unsigned int n)
63     {
64         assert((n+1) * bits_per_element <= sizeof(itemType) * 8);
65         return TinyArrayRef(&data, n);
66     }
67     // only use this for clearing it 
68     void operator=(void *rhs)
69     {
70         assert(rhs==NULL);
71         data = 0;
72     }
73 };
74
75 #endif // TINYARRAY_H