❔ 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");
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
3 Replies
bighugemassive3
bighugemassive3OP15mo ago
I was trying to make this myself but I was stuck on the part where you add to one of those lists; you have to access all of entries whose type is derived from the type's list you want to add an element to, and then add that value to each of their list I don't know how to do that efficiently without storing each of those derived types in another list (stored along side the values list) Nevermind... didn't need this at all, but instead needed it to be Dictionary<Type, Dictionary<Type, TValue>>, where the value of TValue can be overridden for derived types... better get working on it I guess
bighugemassive3
bighugemassive3OP15mo ago
😄
No description
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?