用于双向转换数据的.NET容器?

发布于 2024-08-13 08:28:12 字数 792 浏览 4 评论 0原文

我需要将转换表包含在内存中以便快速访问。到目前为止,我使用了一个简单的Hashtable,其中键是内部代码,值是保存外部代码和其他元数据的对象。

现在我们需要进行反向查找,即根据外部代码来获取内部代码。我只能提出以下选项:

  1. 使用另一个容器进行此查找,哈希表仅包含内部代码作为值,以防止更多冗余。
  2. 使用我现在使用的相同容器,并使用外部代码作为键再次存储这些对象(有一个前缀以防止冲突)。
  3. 不要使用 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:

  1. Have another container for this look-up, Hashtable containing only the internal code as Value to prevent more redundancy.
  2. 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).
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2024-08-20 08:28:12

构建一个自定义集合类,该类具有两个内部哈希表(字典),每个方向一个。

  public BiHashTable<K, V>
  {
     private Dictionary<K, V> vals = new Dictionary<K, V>();
     private Dictionary<V, K> keys = new Dictionary<V, K>();
     public void Add(K key, V val)
     {
        vals.Add(key, val);
        keys.Add(val, key);
     }
     public K this[v val] { get { return keys[val]; } }
     public V this[K key] { get { return vals[key]; } }
     // etc... 
  }

注意:这将是有问题的,因为 K 和 V 都是同一类型,那么您需要不同的公式......

Build a custom collection class that has two internal hashtables (Dictionarys), one in each direction.

  public BiHashTable<K, V>
  {
     private Dictionary<K, V> vals = new Dictionary<K, V>();
     private Dictionary<V, K> keys = new Dictionary<V, K>();
     public void Add(K key, V val)
     {
        vals.Add(key, val);
        keys.Add(val, key);
     }
     public K this[v val] { get { return keys[val]; } }
     public V this[K key] { get { return vals[key]; } }
     // etc... 
  }

NOTE: This will be problematic is both K and V are the same type, you need a different formulation then...

情定在深秋 2024-08-20 08:28:12

我宁愿使用 Dictionary的两个实例

它有利于代码可读性

您确定这个字典是性能瓶颈吗?

I rather use two instances of Dictionary<TKey, TValue>

It favors code readability

Are you sure this dictionary is a performance bottleneck?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文