Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary data structure that uses entry's field as a key

Data records of some kind frequently have to be indexed by a unique key. Usually it looks something like this (I'm using C# because it's the language I'm most comfortable with, but this question isn't specific to it):

public class NamedRecord
{
    public readonly string UniqueImmutableName;
    ...
}

public class UsesUsualDict
{

    Dictionary<string, NamedRecord> myDict = new Dictionary<string, NamedRecord>();

    void AddRecord(NamedRecord _NewRecord)
    {
        myDict[_NewRecord.UniqueImmutableName] = _NewRecord;
    }

    NamedRecord GetRecord(string _Key)
    {
        return myDict[_Key];
    }

}

However, this seems a little redundant: keys in that dictionary should always be considered equal to NamedRecord.UniqueImmutableName, but developer ends maintaining this relationship himself. Also, this kind of data duplication just doesn't feel right to me.

Sometimes I see a solution that is similar: data record doesn't even have the UniqueImmutableName as their member. For example, in his tutorial for D language, Andrei Alexandrescu uses data struct that depicts word statistics of character in a play:

struct PersonaData {
   uint totalWordsSpoken;
   uint[string] wordCount;
}

But the character's name isn't even in it — it's only available as a key to the dictionary these structs are saved in. Outside of that context, this data structure is almost useless.

What I want to do is something like that:

public class UsesLambdaDict
{
    LambdaDictionary<string, NamedRecord> myDict = new LambdaDictionary<string, NamedRecord>(NamedRecord _Record => _Record.UniqueImmutableName);

    void AddRecord(NamedRecord _NewRecord)
    {
        myDict.Add(_NewRecord);
    }

    NamedRecord GetRecord(string _Key)
    {
        return myDict[_Key];
    }
}

It seems to me that this way of handling such data records is better, because the relationship between the NamedRecord's UniqueImmutableName member and the dictionary it's saved in is formalized at compile-time. The downside I see is that there's no way to ensure that given lambda will is a pure function, at least in C#. I don't really know D so well, but it seems that pure keyword it has can't it guarantee neither.

So, I have several questions about this:

  1. Is this even a real problem to being with? The downsides to the first solution that I have are somewhat theoretical — may be there's nothing wrong about it after all.
  2. What are other possible ways to solve it?
  3. What are other possible downsides to the proposed solution?
like image 602
Max Yankov Avatar asked Jan 10 '14 10:01

Max Yankov


People also ask

Which data structure can be used as a key while creating a dictionary?

Dictionary is a built-in implementation of “Python Data Structure” which is a collection of “Key: Value” pairs. Dictionary is created using curly braces with key and value separated by semicolon {Key : Value}.

Which data types can be used as dictionary keys?

Dictionary keys must be of an immutable type. Strings and numbers are the two most commonly used data types as dictionary keys. We can also use tuples as keys but they must contain only strings, integers, or other tuples.

Which data structure can you use as keys for your dictionary in Python?

In addition to the curly braces, there are also colons ( : ) throughout the dictionary. The words to the left of the colons are the keys. Keys can be made up of any immutable data type.

What is a dictionary what type of objects can be used as keys in dictionary?

only immutable type of objects like string, tuple or integer etc. can be used as keys in the dictionaries.


1 Answers

1.) I do not think so.

2.) Use structure with key and value, I do not think lambdas are necessary

3.) Performance issues (cache misses)

like image 181
Kozzi11 Avatar answered Oct 14 '22 07:10

Kozzi11