Is there HashTable structure in Wolfram Mathematica?

后端 未结 5 1684
暖寄归人
暖寄归人 2021-01-31 11:21

I want to use a Structure like HashTable. Is there similar structure in Wolfram Mathematica?

5条回答
  •  不要未来只要你来
    2021-01-31 11:34

    I've made Dictionary.m module, which contained:

    DictHasKey = Function[
        {
            dict,
            key
        },
        ValueQ[dict[key]]
    ]
    
    DictAddKey = Function[
        {
            dict,
            key,
            value
        },
        If[
            DictHasKey[dict,key],
            Print["Warning, Dictionary already has key " <> ToString[key]]
        ];
        dict[key] = value;
    ]
    
    DictKeys = Function[
        {
            dict
        },
        res = {};
        ForEach[DownValues[dict], Function[{dictKeyDescr},
            res = Append[res, ((dictKeyDescr[[1]]) /. dict -> neverUsedSymbolWhatever)[[1, 1]]];
        ]];
        res
    ]
    
    DictValues = Function[
        {
            dict
        },
        res = {};
        ForEach[DownValues[dict], Function[{dictKeyDescr},
            res = Append[res, dictKeyDescr[[2]]];
        ]];
        res
    ]
    
    DictKeyValuePairs = Function[
        {
            dict
        },
        res = {};
        ForEach[DownValues[dict], Function[{dictKeyDescr},
            res = Append[res, {((dictKeyDescr[[1]]) /. dict -> neverUsedSymbolWhatever)[[1, 1]], dictKeyDescr[[2]]}];
        ]];
        res
    ]
    
    ForEach = Function[
        {
            list, 
            func 
        }, 
        len = Length[list]; 
        For[i = 1, i <= len, i++, 
            func[
                list[[i]]
            ]; 
        ]; 
    ]
    

提交回复
热议问题