Revision | 9491ec799a891e9f900cc8a864364b7a798b3b99 (tree) |
---|---|
Time | 2015-07-08 22:25:12 |
Author | komutan <t_komuta@nift...> |
Commiter | komutan |
LinkedListクラスを使わず、Nodeクラス自体にChiledNodesを管理させることで(生成されるインスタンス数が減り?)パフォーマンスが向上した
@@ -9,9 +9,63 @@ namespace NMeCab.Core | ||
9 | 9 | { |
10 | 10 | private class Node |
11 | 11 | { |
12 | - public T Value; | |
12 | + public T Value { get; private set; } | |
13 | 13 | |
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 | + } | |
15 | 69 | |
16 | 70 | public Node(T value) |
17 | 71 | { |
@@ -23,23 +77,26 @@ namespace NMeCab.Core | ||
23 | 77 | |
24 | 78 | public int Count { get; private set; } |
25 | 79 | |
26 | - public T First { get { return this.rootNode.Value; } } | |
27 | - | |
28 | 80 | public void Clear() |
29 | 81 | { |
30 | - this.rootNode = null; | |
31 | 82 | this.Count = 0; |
83 | + this.rootNode = null; | |
32 | 84 | } |
33 | 85 | |
34 | 86 | public void Push(T item) |
35 | 87 | { |
36 | - this.rootNode = this.Merge(this.rootNode, new Node(item)); | |
37 | 88 | this.Count++; |
89 | + this.rootNode = this.Merge(this.rootNode, new Node(item)); | |
90 | + } | |
91 | + | |
92 | + public T Peek() | |
93 | + { | |
94 | + return this.rootNode.Value; | |
38 | 95 | } |
39 | 96 | |
40 | 97 | public T Pop() |
41 | 98 | { |
42 | - T ret = this.First; | |
99 | + T ret = this.Peek(); | |
43 | 100 | this.RemoveFirst(); |
44 | 101 | return ret; |
45 | 102 | } |
@@ -48,8 +105,8 @@ namespace NMeCab.Core | ||
48 | 105 | { |
49 | 106 | if (this.Count == 0) throw new InvalidOperationException("Empty"); |
50 | 107 | |
51 | - this.rootNode = this.Unify(this.rootNode.Childs); | |
52 | 108 | this.Count--; |
109 | + this.rootNode = this.Unify(this.rootNode); | |
53 | 110 | } |
54 | 111 | |
55 | 112 | private Node Merge(Node l, Node r) |
@@ -59,39 +116,39 @@ namespace NMeCab.Core | ||
59 | 116 | |
60 | 117 | if (l.Value.CompareTo(r.Value) > 0) |
61 | 118 | { |
62 | - r.Childs.AddFirst(l); | |
119 | + r.AddFirstChild(l); | |
63 | 120 | return r; |
64 | 121 | } |
65 | 122 | else |
66 | 123 | { |
67 | - l.Childs.AddLast(r); | |
124 | + l.AddLastChild(r); | |
68 | 125 | return l; |
69 | 126 | } |
70 | 127 | } |
71 | 128 | |
72 | - private Node Unify(LinkedList<Node> nodes) | |
129 | + private Node Unify(Node node) | |
73 | 130 | { |
74 | - if (nodes == null || nodes.Count == 0) return null; | |
131 | + if (node == null || node.ChiledsCount == 0) return null; | |
75 | 132 | |
76 | - Node[] tmp = new Node[nodes.Count / 2]; //擬似的Stack | |
133 | + Node[] tmp = new Node[node.ChiledsCount / 2]; //擬似的Stack | |
77 | 134 | |
78 | 135 | for (int i = 0; i < tmp.Length; i++) |
79 | 136 | { |
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(); | |
84 | 139 | tmp[i] = this.Merge(x, y); |
85 | 140 | } |
86 | 141 | |
87 | 142 | Node z; |
88 | - if (nodes.Count == 1) | |
89 | - z = nodes.First.Value; | |
143 | + if (node.ChiledsCount == 1) | |
144 | + z = node.PollFirstChild(); | |
90 | 145 | else |
91 | 146 | z = null; |
92 | 147 | |
93 | 148 | for (int i = tmp.Length - 1; i >= 0; i--) |
149 | + { | |
94 | 150 | z = this.Merge(tmp[i], z); |
151 | + } | |
95 | 152 | |
96 | 153 | return z; |
97 | 154 | } |