Re: named array
- Posted by mattlewis (admin) Sep 03, 2013
- 2149 views
To me, I don't see how this really adds any value, because it seems like you've just added a simple redirection layer on top of the normal map interface. So I would dump it and just use a regular map instead of a narr.
You completely misunderstood what I was trying to do. Map here is only the custodian of symbols. Theoretically, it can be replaced by a sequence or constants (it is the original version with the "enum"). This does not wrap around the map, and a wrap around sequences.
Then I would agree that I don't understand the point. I updated the code you posted:
include std/unittest.e include std/map.e include narr.e as narr sequence Narr1 = narr:new() map map1 = map:new() Narr1 = narr:put(Narr1, "X", 100) Narr1 = narr:put(Narr1, "Y", -200) Narr1 = narr:put(Narr1, "Z", 300) map:put( map1, "X", 100 ) map:put( map1, "Y", -200 ) map:put( map1, "Z", 300 ) test_equal("", 100, narr:get(Narr1, "X")) test_equal("", -200, narr:get(Narr1, "Y")) test_equal("", 300, narr:get(Narr1, "Z")) test_equal("", 100, map:get(map1, "X")) test_equal("", -200, map:get(map1, "Y")) test_equal("", 300, map:get(map1, "Z")) Narr1 = narr:put(Narr1, "X", 1000) Narr1 = narr:put(Narr1, "Y", -2000) Narr1 = narr:put(Narr1, "Z", 3000) map:put( map1, "X", 1000 ) map:put( map1, "Y", -2000 ) map:put( map1, "Z", 3000 ) test_equal("", 1000, narr:get(Narr1, "X")) test_equal("", -2000, narr:get(Narr1, "Y")) test_equal("", 3000, narr:get(Narr1, "Z")) test_equal("", 1000, map:get(map1, "X")) test_equal("", -2000, map:get(map1, "Y")) test_equal("", 3000, map:get(map1, "Z")) sequence Narr2 = narr:new() map map2 = map:new() Narr2 = narr:put(Narr2, "Velocity", 543.902) Narr2 = narr:put(Narr2, "X", 0.1) Narr2 = narr:put(Narr2, "Y", -0.2) Narr2 = narr:put(Narr2, "Z", 0.3) Narr2 = narr:put(Narr2, "Name", "Bullet") Narr2 = narr:put(Narr2, "XYZ-1", {narr:get(Narr1, "X"), narr:get(Narr1, "Y"), narr:get(Narr1, "Z")}) map:put(map2, "Velocity", 543.902) map:put(map2, "X", 0.1) map:put(map2, "Y", -0.2) map:put(map2, "Z", 0.3) map:put(map2, "Name", "Bullet") map:put(map2, "XYZ-1", {map:get(map1, "X"), map:get(map1, "Y"), map:get(map1, "Z")}) test_equal("", 0.1, narr:get(Narr2, "X")) test_equal("", -0.2, narr:get(Narr2, "Y")) test_equal("", 0.3, narr:get(Narr2, "Z")) test_equal("", "Bullet", narr:get(Narr2, "Name")) test_equal("", 543.902, narr:get(Narr2, "Velocity")) test_equal("", {1000, -2000, 3000}, narr:get(Narr2, "XYZ-1")) test_equal("", 0.1, map:get(map2, "X")) test_equal("", -0.2, map:get(map2, "Y")) test_equal("", 0.3, map:get(map2, "Z")) test_equal("", "Bullet", map:get(map2, "Name")) test_equal("", 543.902, map:get(map2, "Velocity")) test_equal("", {1000, -2000, 3000}, map:get(map2, "XYZ-1"))
All I did was replace the narr stuff with map stuff. Note that you can also nest maps, and store and retrieve the data with a single call. Is there a reason you can't simply store the information inside of maps?
Internally, it will look like:
-- map of indexes may contains "first_name" = 1, "second_name" = 2, "task" = 3 (order may be other, but numbers from 1 to 3) Citizen = {"John", "Smith", "buy milk"} Superhero = {"Hedgehogman", 0, "save world"} -- yes, the second element is not used, but it is done for uniformity and speed.
I would say that you're probably better off just coming up with a key, storing the person's structured data as a sequence in a map and using an enum to access the various parts:
enum CITIZEN_FIRST, CITIZEN_LAST, CITIZEN_INCOME enum SUPERHERO_FIRST, SUPERHERO_LAST, SUPERHERO_TASK
Matt