我有以下类:
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]
,因为它包含对对象的引用,最后如果我更改它,它将更改所有内容。如何在循环中解决此问题?我不想创建一个列表,然后将该大列表转换为字典。我认为这不会有效率。
或者,也许我可以创建一些其他类型的数据文档,使我能够快速查询所需的节点,并且我还能够维护顺序。
我认为您可能需要一个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