用于双向转换数据的.NET容器?
我需要将转换表包含在内存中以便快速访问。到目前为止,我使用了一个简单的Hashtable
,其中键是内部代码,值是保存外部代码和其他元数据的对象。
现在我们需要进行反向查找,即根据外部代码来获取内部代码。我只能提出以下选项:
- 使用另一个容器进行此查找,哈希表仅包含内部代码作为值,以防止更多冗余。
- 使用我现在使用的相同容器,并使用外部代码作为键再次存储这些对象(有一个前缀以防止冲突)。
- 不要使用 Key 拉取数据,而是迭代同一容器下包含的 Value 来查找请求的对象( O(n),相同的内存使用量)。
容器是延迟加载的,因此选项 1 和 2 在最坏的情况下通常不会执行。
有人想吗?请告诉我有一些我可以使用的高效容器,但我错过了!
* 编辑*
作为一个 GC 框架,并接受我必须有两个转换数组(字典)的事实,下面的代码行实际上意味着我在内存上只存储了一个对象,然后在两个不同的散列单元下有两个指针?
Dictionary<K1,V> forward;
Dictionary<K2,V> reverse;
//...
void Add(V myObject)
{
// myObject being the BLL object
forward.Add(myObject.InternalCode, myObject);
reverse.Add(myObject.ExternalCode, myObject);
}
伊塔玛。
I have conversion tables I need to contain in memory for fast access. Until now I used a simple Hashtable
were the Key was the internal code, and the Value was an object holding the external code and other meta-data.
Now we need to have a reverse look-up, meaning to get the internal code based on the external code. I could only come up with the following options:
- Have another container for this look-up, Hashtable containing only the internal code as Value to prevent more redundancy.
- Use the same container I use now, and store those objects again now using the the external code as the Key (have a prefix to prevent collisions).
- Don't pull data using Keys, but iterate through the Values contained under the same container to find the requested object ( O(n), same memory usage ).
The container is being lazy-loaded, so options 1&2 usually won't perform under the worst-case scenario.
Thoughts anyone? Please tell me there's some efficient container I could use for that, which I missed!
* EDIT *
Being a GC'd framework, and accepting the fact I'd have to have two conversion arrays (Dictionaries), would the following lines of code actually mean I stored only one object on memory, and then two pointers for it, under two different hashed-cells?
Dictionary<K1,V> forward;
Dictionary<K2,V> reverse;
//...
void Add(V myObject)
{
// myObject being the BLL object
forward.Add(myObject.InternalCode, myObject);
reverse.Add(myObject.ExternalCode, myObject);
}
Itamar.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构建一个自定义集合类,该类具有两个内部哈希表(字典),每个方向一个。
注意:这将是有问题的,因为 K 和 V 都是同一类型,那么您需要不同的公式......
Build a custom collection class that has two internal hashtables (Dictionarys), one in each direction.
NOTE: This will be problematic is both K and V are the same type, you need a different formulation then...
我宁愿使用
Dictionary的两个实例
它有利于代码可读性
您确定这个字典是性能瓶颈吗?
I rather use two instances of
Dictionary<TKey, TValue>
It favors code readability
Are you sure this dictionary is a performance bottleneck?