• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Commit MetaInfo

Revisión9491ec799a891e9f900cc8a864364b7a798b3b99 (tree)
Tiempo2015-07-08 22:25:12
Autorkomutan <t_komuta@nift...>
Commiterkomutan

Log Message

LinkedListクラスを使わず、Nodeクラス自体にChiledNodesを管理させることで(生成されるインスタンス数が減り?)パフォーマンスが向上した

Cambiar Resumen

Diferencia incremental

--- a/src/LibNMeCab/Core/PriorityQueue.cs
+++ b/src/LibNMeCab/Core/PriorityQueue.cs
@@ -9,9 +9,63 @@ namespace NMeCab.Core
99 {
1010 private class Node
1111 {
12- public T Value;
12+ public T Value { get; private set; }
1313
14- public readonly LinkedList<Node> Childs = new LinkedList<Node>();
14+ public int ChiledsCount { get; private set; }
15+
16+ public Node FirstChild { get; private set; }
17+
18+ public Node LastChild { get; private set; }
19+
20+ public Node Prev { get; private set; }
21+
22+ public Node Next { get; private set; }
23+
24+ public void AddFirstChild(Node first)
25+ {
26+ this.ChiledsCount++;
27+ if (this.ChiledsCount == 1)
28+ {
29+ this.LastChild = first;
30+ }
31+ else
32+ {
33+ Node old = this.FirstChild;
34+ first.Next = old;
35+ old.Prev = first;
36+ }
37+ this.FirstChild = first;
38+ }
39+
40+ public void AddLastChild(Node last)
41+ {
42+ this.ChiledsCount++;
43+ if (this.ChiledsCount == 1)
44+ {
45+ this.FirstChild = last;
46+ }
47+ else
48+ {
49+ Node old = this.LastChild;
50+ last.Prev = old;
51+ old.Next = last;
52+ }
53+ this.LastChild = last;
54+ }
55+
56+ public Node PollFirstChild()
57+ {
58+ this.ChiledsCount--;
59+ if (this.ChiledsCount == 0)
60+ {
61+ this.LastChild.Prev = null;
62+ this.LastChild = null;
63+ }
64+ Node first = this.FirstChild;
65+ this.FirstChild = first.Next;
66+ first.Next = null;
67+ return first;
68+ }
1569
1670 public Node(T value)
1771 {
@@ -23,23 +77,26 @@ namespace NMeCab.Core
2377
2478 public int Count { get; private set; }
2579
26- public T First { get { return this.rootNode.Value; } }
27-
2880 public void Clear()
2981 {
30- this.rootNode = null;
3182 this.Count = 0;
83+ this.rootNode = null;
3284 }
3385
3486 public void Push(T item)
3587 {
36- this.rootNode = this.Merge(this.rootNode, new Node(item));
3788 this.Count++;
89+ this.rootNode = this.Merge(this.rootNode, new Node(item));
90+ }
91+
92+ public T Peek()
93+ {
94+ return this.rootNode.Value;
3895 }
3996
4097 public T Pop()
4198 {
42- T ret = this.First;
99+ T ret = this.Peek();
43100 this.RemoveFirst();
44101 return ret;
45102 }
@@ -48,8 +105,8 @@ namespace NMeCab.Core
48105 {
49106 if (this.Count == 0) throw new InvalidOperationException("Empty");
50107
51- this.rootNode = this.Unify(this.rootNode.Childs);
52108 this.Count--;
109+ this.rootNode = this.Unify(this.rootNode);
53110 }
54111
55112 private Node Merge(Node l, Node r)
@@ -59,39 +116,39 @@ namespace NMeCab.Core
59116
60117 if (l.Value.CompareTo(r.Value) > 0)
61118 {
62- r.Childs.AddFirst(l);
119+ r.AddFirstChild(l);
63120 return r;
64121 }
65122 else
66123 {
67- l.Childs.AddLast(r);
124+ l.AddLastChild(r);
68125 return l;
69126 }
70127 }
71128
72- private Node Unify(LinkedList<Node> nodes)
129+ private Node Unify(Node node)
73130 {
74- if (nodes == null || nodes.Count == 0) return null;
131+ if (node == null || node.ChiledsCount == 0) return null;
75132
76- Node[] tmp = new Node[nodes.Count / 2]; //擬似的Stack
133+ Node[] tmp = new Node[node.ChiledsCount / 2]; //擬似的Stack
77134
78135 for (int i = 0; i < tmp.Length; i++)
79136 {
80- Node x = nodes.First.Value;
81- nodes.RemoveFirst();
82- Node y = nodes.First.Value;
83- nodes.RemoveFirst();
137+ Node x = node.PollFirstChild();
138+ Node y = node.PollFirstChild();
84139 tmp[i] = this.Merge(x, y);
85140 }
86141
87142 Node z;
88- if (nodes.Count == 1)
89- z = nodes.First.Value;
143+ if (node.ChiledsCount == 1)
144+ z = node.PollFirstChild();
90145 else
91146 z = null;
92147
93148 for (int i = tmp.Length - 1; i >= 0; i--)
149+ {
94150 z = this.Merge(tmp[i], z);
151+ }
95152
96153 return z;
97154 }