views:

159

answers:

4

I need a simple structure to store polygon names based on sides ... so for e.g. 1 side is "monogon", 2 sides is "digon", 3 sides is "triangle" and so on (say for upto 12 sides) What is the simplest way to store these and reuse them in the code dynamically? for e.g. if my polygonShape class has 3 as the number of sides, it should return "Triangle" as the name (which is a property declared in the class) ( I am using Obj-c). I thought of 3 options

1. enums

typedef enum {monogon = 1, digon, triangle, ...}

But then realized this is reverse of what I need. They would actually encode the numbers for me to a string. I need to obtain the names from the numbers.

2. switch-case statements - came as close alternative

3. Arrays - Then I thought may be use arrays and their indexes map to Strings

Somehow I feel I might be missing something in the "too-simple-to-be-true" solution of arrays. Any opinions appreciated.

+3  A: 

I'd suggest using a Hash table (or hash map), or the equivalent in your language. They are good for key/value pairs. :).

Your array solution seems good enough too, if you're sure you're using the ints in sequence, starting from zero (which in your case would result in a weird "zerogon" or a (i-1) for every time you get the name from the array). The hash suggestion is a more general one.

So analysing both:

Hash table:

  • More general
  • You only store for the values you will use
  • Occupies more memory

Array:

  • Might fit in your specific situation
  • Weird "zerogon" included in the array, or (i-1) because arrays are zero-based
  • Less memory occupied (only for the strings, not for the ints)
Samuel Carrijo
yup ... also I was looking for a simpler structure (speed and maintenance) although I would like to add Hashtables could also be expanded dynamically in a much cleaner way than probably NSMutableArray
Dev
+3  A: 

I think an array would be the way to go. Only catch, is that an array starts at 0 so you'd have to store something like an empty string in index zero, or always subtract 1 from you number of sides to get the proper index. You can't store nil in index zero since it is used to mark the end of the array. The other approach could be a NSDictionary so you don't have to deal with index zero. You would simply have NSStrings 1 - x as your keys, and then the NSStrings of mongon, digon, triangle,... as your objects in the array. Hope this helps.

Brian
+3  A: 

I think an array is an elegant solution in this case, assuming you don't need the prefixes for polygons with more than 20 sides:

NSArray *greekPrefixes = [[NSArray alloc] initWithObjects:
 @"an", @"mono", @"di", @"tri", @"tetra", @"penta", @"hexa", @"hepta", @"octa",
 @"ennea", @"deca", @"hendeca", @"dodeca", @"triskaideca", @"tetrakaideca",
 @"pentakaideca", @"hexakaideca", @"heptakaideca", @"octakaideca",
 @"enneakaideca", @"icosa", nil];

The string at index i is the greek prefix for the number i.

And yes, a polygon with zero sides is an angon :)

Matt Howells
+1  A: 

One other thing to keep in mind in these situations is that you can also store the key/value pairs in a plist in your application bundle, and load them when needed. This can make it easier to maintain a large number of items and localize the strings into different languages.

Marc Charbonneau