--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/using-bitarray-to-represent-sets/example.py Tue Jan 15 22:17:13 2013 +0900
+from bitarray import bitarray
+PARTLEN = 5 # 5 bits per character
+CHARS = (string.ascii_lowercase + string.digits)[:2 ** PARTLEN]
+# 'abcdefghijklmnopqrstuvwxyz012345'
+for i, char in enumerate(CHARS):
+ PARTS[char] = bitarray(bin(i).lstrip('-0b').rjust(PARTLEN, '0'), endian='big')
+# 'a': bitarray('00000'),
+# '5': bitarray('11111')
+ times, more = divmod(max(ids) + 1, PARTLEN)
+ length = PARTLEN * (times + bool(more))
+ ba = bitarray(length, endian='big')
+ return ''.join(ba.decode(PARTS))
+ result = bitarray(endian='big')
+ result.encode(PARTS, string)
+ return set(result.search(bitarray('1')))
+print(encode_set({1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16})) # Buttons in an elevator in Shanghai
+print(encode_set({1, 4, 11, 16, 24, 29, 33, 35, 39, 45, 47, 51, 56, 58, 62, 64})) # Aronson's sequence
+print(decode_set('choices'))
+# {3, 7, 8, 9, 11, 12, 13, 16, 23, 27, 30, 33}