What data structure is used for a huge but persistent dictionary in C ++

I need to use a huge dictionary with integer (or enumerated) keys and string values. But this is absolutely constant. Unable to change the runtime. Is there a way (using templates, etc.) to retrieve dictionary data at compile time instead of using the existing dictionary structure?

+5
source share
3 answers

Clang and LLVM solved your problem by creating tables containing their objects using a combination of code generation and preprocessor tricks.

You can skip any step, depending on your own setup. For instance:

// records.inc
EXPAND_RECORD(Foo, "Foo", 4);
EXPAND_RECORD(Bar, "Bar", 18);
EXPAND_RECORD(Bar2, "Bar", 19);

:

// records.h
enum Record {

#define EXPAND_RECORD(Name, String, Value) Name,
#include "records.inc"
#undef EXPAND_RECORD

};

char const* getRecordName(Record r);
int getRecordValue(Record r);

// records.cpp

char const* getRecordName(Record r) {
  switch(r) {
#define EXPAND_RECORD(Name, String, Value) case Name: return String;
#include "records.inc"
#undef EXPAND_RECORD
  }

  abort(); // unreachable, or you can return a "null" value
}

int getRecordValue(Record r) {
  switch(r) {
#define EXPAND_RECORD(Name, String, Value) case Name: return Value;
#include "records.inc"
#undef EXPAND_RECORD
  }

  abort(); // unreachable, or you can return a "null" value
}

Clang LLVM .inc .

... , . , " ", , , (, ) (unsigned), : , , .

+5

, sed , , :

template <int Index> struct Dictionary { static const char *entry; };

:

template <> const char *Dictionary<5>::entry = "Entry for five";

, ? .

+3

? ++. :

#define MYCONST_1 "#00ff00"
#define MYCONST_10 "My other configuration string"

bash script ruby ​​/python ( ++, ), .

make, .

0
source

All Articles