|
Boost : |
From: Meador Inge (meadori_at_[hidden])
Date: 2006-06-28 23:36:54
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk