Fastest possible string key lookup for known set of keys

后端 未结 8 1982
夕颜
夕颜 2020-12-30 04:26

Consider a lookup function with the following signature, which needs to return an integer for a given string key:

int GetValue(string key) { ... }

8条回答
  •  失恋的感觉
    2020-12-30 04:49

    What you want is a look-up table of look-up tables. If memory cost is not an issue you can go all out.

    const int POSSIBLE_CHARCODES = 256; //256 for ascii //65536 for unicode 16bit
    struct LutMap {
        int value;
        LutMap[POSSIBLE_CHARCODES] next;
    }
    int GetValue(string key) {
        LutMap root = Global.AlreadyCreatedLutMap;
        for(int x=0; x

提交回复
热议问题