C#C
C#3y ago
hugeman

❔ A Dictionary<Type, List<T>> that bakes the list for the type hierarchy (for fast Get calls)

Does C# have an object that does this? When you try to Get/TryGet from the map, it will check if the given type exists in the internal map, and if so, it just returns the list that it maps to. However if it doesn't, it keeps scanning the BaseType until it finds a valid entry, and then it adds each of those types (that weren't in the map).
But it also combines the values of each BaseType's list

Say I had an instance of it as ClassDictionary<string>, i could map do:
class ObjectA {}
class ObjectB : ObjectA {}
class ObjectC : ObjectB {}
dict.AddRange(typeof(ObjectA), "str1", "str2", "str3");
dict.AddRange(typeof(ObjectB), "str4", "str5", "str6");

And then if I do dict.Get(typeof(ObjectC)), it would return a baked list containing "str1", "str2", "str3", "str4", "str5" and "str6"

I could just use a for loop using the key's type, and then accumulate all of the values of each type found, into a new list, and then return that list. But that would be quite slow, especially if the Get method is being called many times a second, and there's a lot of entries and values in each list, hence why this class would bake all of it so that it would eventually be O1 just like a normal dictionary
Was this page helpful?