Interest in a statically initialized map?

A typical idiom in C is to initialize map data structure statically. For example: static struct name_age { const char* name; int age; } name_age_map[] = { {"Will", 50}, {"James", 32}, }; static const struct name_age* map_begin = ...; static const struct name_age* map_end = ...; void foo() { const struct name_age* na = lookup_by_name(map_begin, map_end, "James"); if (na != map_end) { /* process */ } } This is very convenient when one knows that the contents of the map will never change at runtime. However, writing the map data structures and lookup code gets old quick. I have not found an existing way nicely mirror this style in C++. I have being playing around with something similar in spirit to boost::array. Similar in that it can be initialized statically using an initialization list relying on a POD class for the implementation. So the above example would look something like: static static_map<const char*, int, 2> name_age_map = { "Will", 50, "James", 32, }; void foo() { static_map<const char*, int, 3>::iterator it = name_age_map.find("James"); if (it != name_age_map.end()) { // process } } Static_map::find would just be a simple linear search. However, there could be another type, say sorted_static_map, that relies on the keys in the initialization list to be sorted by the user. Then a binary search could be used for sorted_static_map::find. Does anyone else have a need for such a beast? --Meador

Meador Inge wrote:
A typical idiom in C is to initialize map data structure statically.
...snip detail...
Static_map::find would just be a simple linear search. However, there could be another type, say sorted_static_map, that relies on the keys in the initialization list to be sorted by the user. Then a binary search could be used for sorted_static_map::find.
Does anyone else have a need for such a beast?
How about boost.assign: http://www.boost.org/libs/assign/doc/index.html using namespace std; using namespace boost::assign; // bring 'map_list_of()' into scope map<int,int> mii = map_list_of(1,2)(2,3)(3,4)(4,5)(5,6); Same idea should work with strings. Jeff

On 6/29/06, Jeff Garland <jeff@crystalclearsoftware.com> wrote:
How about boost.assign:
<snip detail...> Quick quesiton would be, is this static or done during runtime? I also recall that Boost.Fusion already allows you to create static maps, but those are used to index values using types as indexes. It's interesting to be able to do something like an "enum" but where the index is a string/const char * and the value's type is templatized. But then I'd rather use the standard map to do this, since I'll be dealing with the values in runtime anyway. I suppose static maps will make a lot of sense if you need to deal with it in template metaprogramming or even preprocessing... Otherwise, it's just another way of implementing a simple map. -- Dean Michael C. Berris C/C++ Software Architect Orange and Bronze Software Labs http://3w-agility.blogspot.com/ http://cplusplus-soup.blogspot.com/ Mobile: +639287291459 Email: dean [at] orangeandbronze [dot] com
participants (4)
-
Dean Michael Berris
-
Jeff Garland
-
Meador Inge
-
Thorsten Ottosen