Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
October 01, 2008 Enum to string array | ||||
---|---|---|---|---|
| ||||
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? |
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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
|
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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" |
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to ylixir | 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.
|
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don | 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.
|
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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 |
October 01, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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 |
October 03, 2008 Re: Enum to string array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Spacen Jasset | 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. |
Copyright © 1999-2021 by the D Language Foundation