【问题标题】:Create dictionary(hash table) maintaining order创建字典(哈希表)维护顺序
【发布时间】:2012-06-27 05:56:19
【问题描述】:

我有以下课程:

    private class NodeTemp
    {            
        public string Content;
        public NodeTemp Next;
        public NodeTemp Prev;
    }

如您所见,我有 NodeTemp Next 以便能够引用哈希表上的下一个元素,就像 NodeTemp Prev 将引用哈希表上的 previos 元素一样。

所以我有一个非常大的“xml”,比如我必须解析的文本文件。我看起来像:

<1><a5>: Abbrev Number: 2 (DW_TAG_base_type)
    <a6>   DW_AT_name        : unsigned short   
    <b5>   DW_AT_byte_size   : 2    
    <b6>   DW_AT_encoding    : 7    (unsigned)
 <1><b7>: Abbrev Number: 2 (DW_TAG_base_type)
    <b8>   DW_AT_name        : unsigned int 
    <c5>   DW_AT_byte_size   : 4    
    <c6>   DW_AT_encoding    : 7    (unsigned)
 <1><c7>: Abbrev Number: 2 (DW_TAG_base_type)
    <c8>   DW_AT_name        : unsigned char    
    <d6>   DW_AT_byte_size   : 1    
    <d7>   DW_AT_encoding    : 8    (unsigned char)
 <1><d8>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <d9>   DW_AT_type        : DW_FORM_ref4 <0x552> 
 <1><de>: Abbrev Number: 2 (DW_TAG_base_type)
    <df>   DW_AT_name        : void 
    <e4>   DW_AT_byte_size   : 0    
    <e5>   DW_AT_encoding    : 5    (signed)
 <1><e6>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <e7>   DW_AT_type        : DW_FORM_ref_udata <0xde> 
 <1><ea>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <eb>   DW_AT_type        : DW_FORM_ref4 <0x180> 
 <1><f0>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <f1>   DW_AT_type        : DW_FORM_ref4 <0x4cb> 
 <1><f6>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <f7>   DW_AT_type        : DW_FORM_ref4 <0x4efb>    
 <1><fc>: Abbrev Number: 2 (DW_TAG_base_type)
    <fd>   DW_AT_name        : char 
    <102>   DW_AT_byte_size   : 1   
    <103>   DW_AT_encoding    : 8   (unsigned char)
.....
....

我有一个方法可以搜索它并一次返回一个块。我创建Dictionary&lt;string, NodeTemp&gt; 而不是List&lt;NodeTemp&gt; 的原因是为了性能,我必须进行多次查询才能找到我需要的节点。

所以我现在拥有的是:

var mtch = Regex.Match(GetUnparsedDebugInfo(), @"(?s)<\d+><\w+>.*?(?=\n <)");

int ctr = 0; // counter                       
NodeTemp[] nodes = new NodeTemp[3]; // circular array

while (mtch.Success)
{

    /*  mtch.value should = something like:

              <1><a5>: Abbrev Number: 2 (DW_TAG_base_type)
                <a6>   DW_AT_name        : unsigned short   
                <b5>   DW_AT_byte_size   : 2    
                <b6>   DW_AT_encoding    : 7    (unsigned)

    */

    var index = ctr % 3; // current possition in circular array

    //get key
    var k = Regex.Match(mtch.Value, @"><(\w+)>").Groups[1].Value;

    var cNode = new NodeTemp() { Content = mtch.Value };                           

    dictionary.Add(k, cNode);

    nodes[index] = cNode;

    if (ctr > 0)
    {
        var lastIndex = index - 1;
        if (lastIndex < 0)
        lastIndex = 2;

        nodes[lastIndex].Next = cNode;
        cNode.Prev = nodes[lastIndex];
    }

    ctr++;

    mtch = mtch.NextMatch();
}

这不起作用,因为nodes[index] 包含对对象的引用,最后如果我更改它,它将全部更改。我该如何解决这个while循环?我不想创建一个列表,然后将该大列表转换为字典。我认为这不会有效率。

或者,也许我可以创建一些其他类型的数据结构,使我能够快速查询我需要的节点,并且我还能够保持顺序。

【问题讨论】:

    标签: c# performance dictionary hashtable


    【解决方案1】:

    我认为您可能需要的是 OrderedDictionary。看一看:http://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary.aspx

    看起来也有一个使用泛型的,但还没有尝试过。 http://www.codeproject.com/Articles/18615/OrderedDictionary-T-A-generic-implementation-of-IO

    【讨论】:

      猜你喜欢
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 2010-12-06
      • 2013-03-21
      • 2012-08-23
      相关资源
      最近更新 更多