enum_bit_vector

Last updated:

0 purchases

enum_bit_vector Image
enum_bit_vector Images
Add to Cart

Description:

enum bit vector

EnumBitVector - Datatype storing a set of enum values as a 64-bit integer #
Motivation: #
Often we want to store a set of flags/labels. For examples which super powers a super hero has, what permissions a user has, or what labels a food item posesses.
All those use cases can be represented by a set of enums:
enum SuperPowers { flying, speed, resistance, psychic, wealth}
batman.superPowers = {SuperPowers.wealth, SuperPowers.flying };

enum Permissions { deleteFiles, inviteOthers, createMeetings}
user1.permissions = { Permissions.inviteOthers };

enum FoodLabels {vegan, vegetarian, containsNuts, ketoFriendly}
carrotLabels = {FoodLabels.vegan, FoodLabels.vegetarian, FoodLabels.ketoFriendly};
copied to clipboard
If we store large quantities of data it could be inefficient to store these values as a List of Strings or even a Map of Strings to Booleans in json like this:
{
"title": "carrot",
"foodLabels": {
"FoodLabels.vegan": true,
"FoodLabels.vegetarian": true,
"FoodLabels.containsNuts": false,
"FoodLabels.ketoFriendly": true
}
}
copied to clipboard
Instead, in Dart we could use a 64-bit integer to represent up to 64 boolean values: As long as our enum contains at most 64 values we can fit them all into one int, which can be represented as a hexadecimal string in just 8 characters.
Installation: #
dart pub add enum_bit_vector
dart pub get
copied to clipboard
Import the package:
import 'package:enum_bit_vector/enum_bit_vector.dart';
copied to clipboard
How to use: #

Register the Enum Type you want to create EnumBitVectors for. This has to happen only once during the runtime of the program, but before you first use the data type anywhere.

EnumBitVector.registerEnum(SuperPowers.values);
copied to clipboard

Create an EnumBitVector from a set or list of enums, an int or a hexString:

enumBitVector = EnumBitVector.fromSet({SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic});
enumBitVector = EnumBitVector.fromList([SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic]);
enumBitVector = EnumBitVector.fromInt(13);
enumBitVector = EnumBitVector.fromHexString('0000000d');
copied to clipboard
Access values: #
you can easily convert an EnumBitVector to a set or list of enums, to an int or a hexString again, whenever needed:
enumBitVector.toSet() // => {SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic}
enumBitVector.enumSet // => {SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic}
enumBitVector.toList() // => [SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic]
enumBitVector.toInt() // => 13
enumBitVector.value // => 13
enumBitVector.toBinaryString() // => '0000000000000000000000000000000000000000000000000000000001101';
enumBitVector.toHexString() // => '0000000d';
copied to clipboard
JSON-conversion: #
final json = enumBitVector.toJson();
// json will be {'v': 13}
final bitVector = EnumBitVector<SuperPowers>.fromJson({'v': 13});
// will create a bitvector with int-value 13.
copied to clipboard
Print an EnumBitVector
enumBitVector = EnumBitVector.fromSet({SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic});
print(enumBitVector);
// prints: EnumBitVector<SuperPowers>(0000000000000000000000000000000000000000000000000000000000001101 = {SuperPowers.flying, SuperPowers.resistance, SuperPowers.psychic})
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.