www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Enum to string array

reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
enum Symbols {
		CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
};

How could I map this into an array? (or vice-versa) must I use a switch 
statement, or an enum -> string mapping array?
Oct 01 2008
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Spacen Jasset:
 enum Symbols {
 		CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a switch 
 statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol. You can also add the opposite mapping, letting it create a switch at compile time, or an associative array, or (but this is often overkill) a perfect hash. If you need help ask more :-) Bye, bearophile
Oct 01 2008
next sibling parent reply Don <nospam nospam.com.au> writes:
bearophile wrote:
 Spacen Jasset:
 enum Symbols {
 		CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a switch 
 statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol.

Such a thing already exists in phobos2 in std.typecons.
Oct 01 2008
parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Don wrote:
 bearophile wrote:
 Spacen Jasset:
 enum Symbols {
         CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a 
 switch statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol.

Such a thing already exists in phobos2 in std.typecons.

currently use.
Oct 01 2008
prev sibling next sibling parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
bearophile wrote:
 Spacen Jasset:
 enum Symbols {
 		CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a switch 
 statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol. You can also add the opposite mapping, letting it create a switch at compile time, or an associative array, or (but this is often overkill) a perfect hash. If you need help ask more :-) Bye, bearophile

This sounds interesting. Can you give me a simple example of this. I don't really deal in mixins and templates much at the moment, so I cannot imagine how this might be done.
Oct 01 2008
parent Gide Nwawudu <gide btinternet.com> writes:
On Wed, 01 Oct 2008 16:50:43 +0100, Spacen Jasset
<spacenjasset yahoo.co.uk> wrote:

bearophile wrote:
 Spacen Jasset:
 enum Symbols {
 		CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a switch 
 statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol. You can also add the opposite mapping, letting it create a switch at compile time, or an associative array, or (but this is often overkill) a perfect hash. If you need help ask more :-) Bye, bearophile

This sounds interesting. Can you give me a simple example of this. I don't really deal in mixins and templates much at the moment, so I cannot imagine how this might be done.

The docs are here. http://www.digitalmars.com/d/2.0/phobos/std_typecons.html module main; import std.stdio; import std.typecons; void main() { mixin(defineEnum!("Direction", "North", "South", "East", "West")); auto d = Direction.North; writefln(d, " = ", enumToString(d)); enumFromString("South",d); writefln(d, " = ", enumToString(d)); } Hope this helps. Gide
Oct 01 2008
prev sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
On Thu, Oct 2, 2008 at 1:04 AM, Spacen Jasset <spacenjasset yahoo.co.uk> wrote:
 Don wrote:
 bearophile wrote:
 Spacen Jasset:
 enum Symbols {
        CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 How could I map this into an array? (or vice-versa) must I use a switch
 statement, or an enum -> string mapping array?

You can create a template mixin that given the list of names as strings, defines an enum and its array of strings, to map index => symbol.

Such a thing already exists in phobos2 in std.typecons.

I will have a look at how this works. Presumably is DMD2, which I don't currently use.

It is D2, but there's a port of it to D1 in the "std2" project. http://www.dsource.org/projects/std2/browser/trunk/std2/std2/typecons.d It's not one of the modules I've used, though, so I can't recall if it actually worked or not. (For some reason I didn't put it in the list of "ported modules" here http://www.dsource.org/projects/std2. -- could be I just forgot to update that list after porting it.) --bb
Oct 01 2008
prev sibling parent reply ylixir <ylixir gmail.com> writes:
Spacen Jasset wrote:
 
 
 enum Symbols {
         CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };
 
 How could I map this into an array? (or vice-versa) must I use a switch 
 statement, or an enum -> string mapping array?

maybe an associative array is what you are looking for? something like: char[][Symbols] theArray; theArray[Symbols.CYCLIC_FORWARD] = "Cycle Forward"; theArray[Symbols.CYCLIC_BACKWARD] = "Cycle Backwards"; writefln(theArray[Symbols.CYCLIC_FORWARD]); //prints "Cycle Forward" writefln(theArray[Symbols.CYCLIC_BACKWARDS]); //prints "Cycle Backwards"
Oct 01 2008
parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
ylixir wrote:
 Spacen Jasset wrote:
 enum Symbols {
         CYCLIC_FORWARD, CYCLIC_BACKWARD, CYCLIC_LEFT, etc
 };

 How could I map this into an array? (or vice-versa) must I use a 
 switch statement, or an enum -> string mapping array?

maybe an associative array is what you are looking for? something like: char[][Symbols] theArray; theArray[Symbols.CYCLIC_FORWARD] = "Cycle Forward"; theArray[Symbols.CYCLIC_BACKWARD] = "Cycle Backwards"; writefln(theArray[Symbols.CYCLIC_FORWARD]); //prints "Cycle Forward" writefln(theArray[Symbols.CYCLIC_BACKWARDS]); //prints "Cycle Backwards"

Yes indeed. But I really would like it to map the other way, also it's a bit akward as you have to add an enum entry and a AA aray entry. But it's defiantly a possible solution. At the moment I have parallel arrays, which is also a bit risky.
Oct 01 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Wed, 01 Oct 2008 17:03:26 +0100,
Spacen Jasset wrote:
 ylixir wrote:
 maybe an associative array is what you are looking for?
 
 something like:
 
 char[][Symbols] theArray;
 
 theArray[Symbols.CYCLIC_FORWARD] = "Cycle Forward";
 theArray[Symbols.CYCLIC_BACKWARD] = "Cycle Backwards";
 
 writefln(theArray[Symbols.CYCLIC_FORWARD]); //prints "Cycle Forward"
 writefln(theArray[Symbols.CYCLIC_BACKWARDS]); //prints "Cycle Backwards"

Yes indeed. But I really would like it to map the other way, also it's a bit akward as you have to add an enum entry and a AA aray entry. But it's defiantly a possible solution. At the moment I have parallel arrays, which is also a bit risky.

Here's one possible solution: import std.stdio: writeln; string makeList(R...)(string first, R rest) { static if (rest.length) return first ~ ", " ~ makeList(rest); else return first; } string[] toArray(R...)(string first, R rest) { static if (rest.length) return [first] ~ toArray(rest); else return [first]; } template TwoWayEnum(Fields...) { mixin("enum { " ~ makeList(Fields) ~ "};"); string toString(int el) { return toArray(Fields)[el]; } int fromString(string s) { int fromString(R...)(int id, string first, R rest) { if (first == s) return id; else { static if (rest.length) return fromString(id+1, rest); else throw new Exception("bad name"); } } return fromString(0, Fields); } } void main() { alias TwoWayEnum!("a"[],"b"[],"c"[],"d"[]) abcd; writeln(abcd.c); writeln(abcd.toString(abcd.c)); writeln(abcd.fromString("c")); } One complication here is that you must specify element names with square brackets at the end which converts them into slices, otherwise toArray stuff doesn't work.
Oct 02 2008
parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Sergey Gromov wrote:
 Wed, 01 Oct 2008 17:03:26 +0100,
 Spacen Jasset wrote:
 ylixir wrote:
 maybe an associative array is what you are looking for?

 something like:

 char[][Symbols] theArray;

 theArray[Symbols.CYCLIC_FORWARD] = "Cycle Forward";
 theArray[Symbols.CYCLIC_BACKWARD] = "Cycle Backwards";

 writefln(theArray[Symbols.CYCLIC_FORWARD]); //prints "Cycle Forward"
 writefln(theArray[Symbols.CYCLIC_BACKWARDS]); //prints "Cycle Backwards"

bit akward as you have to add an enum entry and a AA aray entry. But it's defiantly a possible solution. At the moment I have parallel arrays, which is also a bit risky.

Here's one possible solution: import std.stdio: writeln; string makeList(R...)(string first, R rest) { static if (rest.length) return first ~ ", " ~ makeList(rest); else return first; } string[] toArray(R...)(string first, R rest) { static if (rest.length) return [first] ~ toArray(rest); else return [first]; } template TwoWayEnum(Fields...) { mixin("enum { " ~ makeList(Fields) ~ "};"); string toString(int el) { return toArray(Fields)[el]; } int fromString(string s) { int fromString(R...)(int id, string first, R rest) { if (first == s) return id; else { static if (rest.length) return fromString(id+1, rest); else throw new Exception("bad name"); } } return fromString(0, Fields); } } void main() { alias TwoWayEnum!("a"[],"b"[],"c"[],"d"[]) abcd; writeln(abcd.c); writeln(abcd.toString(abcd.c)); writeln(abcd.fromString("c")); } One complication here is that you must specify element names with square brackets at the end which converts them into slices, otherwise toArray stuff doesn't work.

documentation on the mixin("") form on the official site.
Oct 03 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Fri, 03 Oct 2008 14:44:58 +0100,
Spacen Jasset wrote:
 Ah thanks. I will try this out. I must say that I can't find 
 documentation on the mixin("") form on the official site.

http://www.digitalmars.com/d/2.0/expression.html#MixinExpression
Oct 03 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Sat, 4 Oct 2008 00:50:55 +0400,
Sergey Gromov wrote:
 Fri, 03 Oct 2008 14:44:58 +0100,
 Spacen Jasset wrote:
 Ah thanks. I will try this out. I must say that I can't find 
 documentation on the mixin("") form on the official site.

http://www.digitalmars.com/d/2.0/expression.html#MixinExpression

Oh, and http://www.digitalmars.com/d/2.0/statement.html#MixinStatement
Oct 03 2008
parent Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Sergey Gromov wrote:
 Sat, 4 Oct 2008 00:50:55 +0400,
 Sergey Gromov wrote:
 Fri, 03 Oct 2008 14:44:58 +0100,
 Spacen Jasset wrote:
 Ah thanks. I will try this out. I must say that I can't find 
 documentation on the mixin("") form on the official site.


Oh, and http://www.digitalmars.com/d/2.0/statement.html#MixinStatement

Oct 07 2008