[llvm-commits] [PATCH] SmallEnumSet

Talin viridia at gmail.com
Thu Jan 8 15:42:28 PST 2009


This patch contains a new container class, SmallEnumSet, and unit tests for
it.
SmallEnumSet is inspired by SmallVector and the other "small" sets in ADT.

The keys of the set are enumeration constants, which range from 0 to some
maximum value which is known at compile time. Internally, SmallEnumSet
maintains a fixed-length bit vector, so that membership in the set is
represented by a single bit per possible key.

Traditionally in C and C++ this sort of thing is done via a "flags" field.
SmallEnumSet has a number of advantages over using flags:

   - It's type safe.
   - It has a set-like interface, so you can use "add", "contains" and other
   set methods.
   - It isn't limited to 32 flags or whatever the size of an integer may be.
   - The keys can be sequentially-numbered enumeration constants, such as
   produced by an enum statement with default initialization - you don't have
   to initialize your enum constants to single-bit values such as "(1 << 5)" or
   "0x00000800", nor do you have to update all of the initializers if you want
   to insert a new constant in the middle of the sequence.

At the same time, SmallEnumSet retains many of the advantages of an integer
flags field - it allocates no memory, and can efficiently be passed or
returned by value between functions. All of the methods of the class can be
optimized down to essentially the same code you would write if you were
using flags.

(I actually wrote this about 6 months ago, and I've been using it quite a
bit in my own code, but I didn't want to submit a patch until I could also
submit the unit tests - which was one of my motivations for doing the unit
test work.)

-- 
-- Talin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20090108/5c1a388e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SmallEnumSet.patch
Type: application/octet-stream
Size: 21274 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20090108/5c1a388e/attachment.obj>


More information about the llvm-commits mailing list