enumap ~master

A lightweight structure that maps enum members to values


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Enumap

An Enumap is a glorified wrapper around a static array. Are you sold yet? No? Alright, take a look at this:

enum Attribute {
 strength, dexterity, constitution, wisdom, intellect, charisma
}

Now you want to map each Attribute to an int. You could use D's built-in associative arrays:

int[Attribute] attributes;
attributes[Attribute.strength] = 10;

However, I think you'll like Enumap better:

Enumap!(Attribute, int) attributes;
attributes[Attribute.strength] = 10;

Still not impressed? Well, you might prefer an Enumap if:

You like syntactic sugar:

// Boring!
if (hero.attributes[Attribute.wisdom] < 5) hero.drink(unidentifiedPotion);

// Fun! And Concise!
if (hero.attributes.wisdom < 5) hero.drink(unidentifiedPotion);

You like ranges:

// roll for stats!
attributes = generate!(() => uniform!"[]"(1, 20)).take(6);

You like default values:

int[Attribute] aa;
Enumap!(Attribute, int) em;

aa[Attribute.strength]; // Range violation!
em.strength;            // 0

You like array-wise operations:

// note the convenient constructor function:
auto bonus = enumap(Attribute.charisma, 2, Attribute.wisdom, 1);

// level up! adds 2 to charisma and 1 to wisdom.
hero.attributes += bonus;

You dislike garbage day:

      void donFancyHat(ref int[Attribute] aa) { aa[Attribute.charisma] += 1; }
@nogc void donFancyHat(ref Enumap!(Attribute, int) map) { map.charisma += 1; }

Check the docs for the full feature set.

Enumap comes in dub package form.

Authors:
  • rcorre
Dependencies:
none
Versions:
0.4.2 2018-Mar-04
0.4.1 2016-Feb-12
0.4.0 2015-Sep-13
0.3.1 2015-Sep-11
~master 2018-Mar-04
Show all 5 versions
Download Stats:
  • 1 downloads today

  • 6 downloads this week

  • 19 downloads this month

  • 1574 downloads total

Score:
2.0
Short URL:
enumap.dub.pm