0

I have a set of rules for string transformation that I parse from a file. The rules look like this:

'aaa' -> 'bbb'

'aa' -> 'bc'

Assuming I know how to parse the file, what would be a good data structure in C to hold these transformations? Later on I will need to go through the data structure and get both the 'input' and the 'output' of the transformation for all the rules.

Thank you.

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • 1
    Looks like you need a hash. – cnicutar Feb 23 '13 at 14:48
  • there is a similar answer here: http://stackoverflow.com/questions/4551677/dictionary-map-key-value-pairs-data-structure-in-c – Wasafa1 Feb 23 '13 at 14:51
  • 1
    Why would a hash be good here? Wouldn't a linked list of structs that contain 'input' and 'output' be enough? – Hommer Smith Feb 23 '13 at 14:55
  • @HommerSmith: yes, but thats much less efficient. – Linuxios Feb 23 '13 at 14:56
  • 1
    Linuxios. Why is less efficient? I won't need to go to a particular 'input'. I need to iterate over all of them. – Hommer Smith Feb 23 '13 at 14:57
  • @HommerSmith: Than you've answered your own question. – Linuxios Feb 23 '13 at 14:58
  • @HommerSmith To extend Linuxios' answer: Using a hashtable you can calculate the position in the table of translation rules which reduces the effort to find the rule to lookup in the table + calculating the hash. Iterating through the linked list will neither be a constant effort nor faster than a simple lookup. – junix Feb 23 '13 at 15:34
  • 1
    I don't need to find any rule. I need to iterate over all of them. – Hommer Smith Feb 23 '13 at 15:42

1 Answers1

1
struct map {
  char* key;
  char* value;
};

Assuming you know all the key's you can then make an array of these structs, traverse it searching by key, then use the associated value.

Jared Beekman
  • 320
  • 2
  • 10