From db9f49041b6590f3ed692ffe44a37b307b9aed79 Mon Sep 17 00:00:00 2001 From: komutan Date: Fri, 10 Jul 2015 22:00:38 +0900 Subject: [PATCH] =?utf8?q?=E5=BE=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/LibNMeCab/Core/PriorityQueue.cs | 50 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/LibNMeCab/Core/PriorityQueue.cs b/src/LibNMeCab/Core/PriorityQueue.cs index 2449c8c..7a13547 100644 --- a/src/LibNMeCab/Core/PriorityQueue.cs +++ b/src/LibNMeCab/Core/PriorityQueue.cs @@ -7,16 +7,16 @@ namespace NMeCab.Core public class PriorityQueue where T : IComparable { - private class Node + private class HeapNode { public T Value { get; private set; } public int ChildsCount { get; private set; } - public Node FirstChild { get; private set; } - public Node LastChild { get; private set; } - public Node Prev { get; private set; } - public Node Next { get; private set; } + public HeapNode FirstChild { get; private set; } + public HeapNode LastChild { get; private set; } + public HeapNode Prev { get; private set; } + public HeapNode Next { get; private set; } - public void AddFirstChild(Node first) + public void AddFirstChild(HeapNode first) { this.ChildsCount++; if (this.ChildsCount == 1) @@ -25,14 +25,14 @@ namespace NMeCab.Core } else { - Node old = this.FirstChild; + HeapNode old = this.FirstChild; first.Next = old; old.Prev = first; } this.FirstChild = first; } - public void AddLastChild(Node last) + public void AddLastChild(HeapNode last) { this.ChildsCount++; if (this.ChildsCount == 1) @@ -41,14 +41,14 @@ namespace NMeCab.Core } else { - Node old = this.LastChild; + HeapNode old = this.LastChild; last.Prev = old; old.Next = last; } this.LastChild = last; } - public Node PollFirstChild() + public HeapNode PollFirstChild() { this.ChildsCount--; if (this.ChildsCount == 0) @@ -56,19 +56,19 @@ namespace NMeCab.Core this.LastChild.Prev = null; this.LastChild = null; } - Node first = this.FirstChild; + HeapNode first = this.FirstChild; this.FirstChild = first.Next; first.Next = null; return first; } - public Node(T value) + public HeapNode(T value) { this.Value = value; } } - private Node rootNode; + private HeapNode rootNode; public int Count { get; private set; } @@ -81,7 +81,7 @@ namespace NMeCab.Core public void Push(T item) { this.Count++; - this.rootNode = this.Merge(this.rootNode, new Node(item)); + this.rootNode = this.MergeNodes(this.rootNode, new HeapNode(item)); } public T Peek() @@ -98,13 +98,11 @@ namespace NMeCab.Core public void RemoveFirst() { - if (this.Count == 0) throw new InvalidOperationException("Empty"); - this.Count--; - this.rootNode = this.Unify(this.rootNode); + this.rootNode = this.UnifyChilds(this.rootNode); } - private Node Merge(Node l, Node r) + private HeapNode MergeNodes(HeapNode l, HeapNode r) { if (l == null) return r; if (r == null) return l; @@ -121,20 +119,18 @@ namespace NMeCab.Core } } - private Node Unify(Node node) + private HeapNode UnifyChilds(HeapNode node) { - if (node == null || node.ChildsCount == 0) return null; - - Node[] tmp = new Node[node.ChildsCount / 2]; //必要な要素数が明らかなのでStackではなく配列 + HeapNode[] tmp = new HeapNode[node.ChildsCount / 2]; //必要な要素数が明らかなのでStackではなく配列 for (int i = 0; i < tmp.Length; i++) { - Node x = node.PollFirstChild(); - Node y = node.PollFirstChild(); - tmp[i] = this.Merge(x, y); + HeapNode x = node.PollFirstChild(); + HeapNode y = node.PollFirstChild(); + tmp[i] = this.MergeNodes(x, y); } - Node z; + HeapNode z; if (node.ChildsCount == 1) //子要素数が奇数の場合、まだ1つ残っている子要素をここで処理 z = node.PollFirstChild(); else @@ -142,7 +138,7 @@ namespace NMeCab.Core for (int i = tmp.Length - 1; i >= 0; i--) //逆順ループで配列をStackのように振る舞わせる { - z = this.Merge(tmp[i], z); + z = this.MergeNodes(tmp[i], z); } return z; -- 2.11.0