我有下面的类:创建字典(哈希表)维持秩序
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<string, NodeTemp>
而不是List<NodeTemp>
是因为性能原因,为了查找我需要的节点,我必须进行多个查询。
所以我现在所拥有的是:
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循环?我不想创建一个List,然后将该大型列表转换为字典。我认为这不会有效。
或者我可以创建一些其他类型的数据结构,使我能够快速查询我需要的节点,并且我也能够维护顺序。