1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/EnumSet.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,177 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* A set abstraction for enumeration values. */ 1.11 + 1.12 +#ifndef mozilla_EnumSet_h 1.13 +#define mozilla_EnumSet_h 1.14 + 1.15 +#include "mozilla/Assertions.h" 1.16 + 1.17 +#include <stdint.h> 1.18 + 1.19 +namespace mozilla { 1.20 + 1.21 +/** 1.22 + * EnumSet<T> is a set of values defined by an enumeration. It is implemented 1.23 + * using a 32 bit mask for each value so it will only work for enums with an int 1.24 + * representation less than 32. It works both for enum and enum class types. 1.25 + */ 1.26 +template<typename T> 1.27 +class EnumSet 1.28 +{ 1.29 + public: 1.30 + EnumSet() 1.31 + : mBitField(0) 1.32 + { } 1.33 + 1.34 + EnumSet(T aEnum) 1.35 + : mBitField(aEnum) 1.36 + { } 1.37 + 1.38 + EnumSet(T aEnum1, T aEnum2) 1.39 + : mBitField(bitFor(aEnum1) | 1.40 + bitFor(aEnum2)) 1.41 + { } 1.42 + 1.43 + EnumSet(T aEnum1, T aEnum2, T aEnum3) 1.44 + : mBitField(bitFor(aEnum1) | 1.45 + bitFor(aEnum2) | 1.46 + bitFor(aEnum3)) 1.47 + { } 1.48 + 1.49 + EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4) 1.50 + : mBitField(bitFor(aEnum1) | 1.51 + bitFor(aEnum2) | 1.52 + bitFor(aEnum3) | 1.53 + bitFor(aEnum4)) 1.54 + { } 1.55 + 1.56 + EnumSet(const EnumSet& aEnumSet) 1.57 + : mBitField(aEnumSet.mBitField) 1.58 + { } 1.59 + 1.60 + /** 1.61 + * Add an element 1.62 + */ 1.63 + void operator+=(T aEnum) { 1.64 + mBitField |= bitFor(aEnum); 1.65 + } 1.66 + 1.67 + /** 1.68 + * Add an element 1.69 + */ 1.70 + EnumSet<T> operator+(T aEnum) const { 1.71 + EnumSet<T> result(*this); 1.72 + result += aEnum; 1.73 + return result; 1.74 + } 1.75 + 1.76 + /** 1.77 + * Union 1.78 + */ 1.79 + void operator+=(const EnumSet<T> aEnumSet) { 1.80 + mBitField |= aEnumSet.mBitField; 1.81 + } 1.82 + 1.83 + /** 1.84 + * Union 1.85 + */ 1.86 + EnumSet<T> operator+(const EnumSet<T> aEnumSet) const { 1.87 + EnumSet<T> result(*this); 1.88 + result += aEnumSet; 1.89 + return result; 1.90 + } 1.91 + 1.92 + /** 1.93 + * Remove an element 1.94 + */ 1.95 + void operator-=(T aEnum) { 1.96 + mBitField &= ~(bitFor(aEnum)); 1.97 + } 1.98 + 1.99 + /** 1.100 + * Remove an element 1.101 + */ 1.102 + EnumSet<T> operator-(T aEnum) const { 1.103 + EnumSet<T> result(*this); 1.104 + result -= aEnum; 1.105 + return result; 1.106 + } 1.107 + 1.108 + /** 1.109 + * Remove a set of elements 1.110 + */ 1.111 + void operator-=(const EnumSet<T> aEnumSet) { 1.112 + mBitField &= ~(aEnumSet.mBitField); 1.113 + } 1.114 + 1.115 + /** 1.116 + * Remove a set of elements 1.117 + */ 1.118 + EnumSet<T> operator-(const EnumSet<T> aEnumSet) const { 1.119 + EnumSet<T> result(*this); 1.120 + result -= aEnumSet; 1.121 + return result; 1.122 + } 1.123 + 1.124 + /** 1.125 + * Intersection 1.126 + */ 1.127 + void operator&=(const EnumSet<T> aEnumSet) { 1.128 + mBitField &= aEnumSet.mBitField; 1.129 + } 1.130 + 1.131 + /** 1.132 + * Intersection 1.133 + */ 1.134 + EnumSet<T> operator&(const EnumSet<T> aEnumSet) const { 1.135 + EnumSet<T> result(*this); 1.136 + result &= aEnumSet; 1.137 + return result; 1.138 + } 1.139 + 1.140 + /** 1.141 + * Equality 1.142 + */ 1.143 + 1.144 + bool operator==(const EnumSet<T> aEnumSet) const { 1.145 + return mBitField == aEnumSet.mBitField; 1.146 + } 1.147 + 1.148 + /** 1.149 + * Test is an element is contained in the set 1.150 + */ 1.151 + bool contains(T aEnum) const { 1.152 + return mBitField & bitFor(aEnum); 1.153 + } 1.154 + 1.155 + /** 1.156 + * Return the number of elements in the set 1.157 + */ 1.158 + 1.159 + uint8_t size() { 1.160 + uint8_t count = 0; 1.161 + for (uint32_t bitField = mBitField; bitField; bitField >>= 1) { 1.162 + if (bitField & 1) 1.163 + count++; 1.164 + } 1.165 + return count; 1.166 + } 1.167 + 1.168 + private: 1.169 + static uint32_t bitFor(T aEnum) { 1.170 + uint32_t bitNumber(aEnum); 1.171 + MOZ_ASSERT(bitNumber < 32); 1.172 + return 1U << bitNumber; 1.173 + } 1.174 + 1.175 + uint32_t mBitField; 1.176 +}; 1.177 + 1.178 +} // namespace mozilla 1.179 + 1.180 +#endif /* mozilla_EnumSet_h_*/