From 65cf764428644450efb588ee7040735dc6061e34 Mon Sep 17 00:00:00 2001 From: komutan Date: Mon, 21 Jul 2014 02:58:47 +0900 Subject: [PATCH] =?utf8?q?=E3=83=87=E3=83=90=E3=83=83=E3=82=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/LibNMeCab/Core/PriorityQueue.cs | 41 +++++++++++++------------------------ 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/LibNMeCab/Core/PriorityQueue.cs b/src/LibNMeCab/Core/PriorityQueue.cs index 9c26bf3..865b73b 100644 --- a/src/LibNMeCab/Core/PriorityQueue.cs +++ b/src/LibNMeCab/Core/PriorityQueue.cs @@ -20,7 +20,7 @@ namespace NMeCab.Core } #if BinaryHeapPriorityQueue - + public void Push(T item) { int currentPos = this.list.Count; @@ -52,48 +52,35 @@ namespace NMeCab.Core private void DeleteTop() { int tailPos = this.list.Count - 1; - this.list[0] = this.list[tailPos]; + if (tailPos == -1) return; + T current = this.list[tailPos]; this.list.RemoveAt(tailPos); if (tailPos == 0) return; tailPos--; - + int currentPos = 0; - T current = this.list[0]; while (true) { - int leftPos = currentPos * 2 + 1; - if (leftPos > tailPos) break; - int rightPos = leftPos + 1; + int chiledPos = currentPos * 2 + 1; + if (chiledPos > tailPos) break; + T chiled = this.list[chiledPos]; - int chiledPos; - T chiled; - if (rightPos > tailPos) + int wrkPos = chiledPos + 1; + if (wrkPos <= tailPos) { - chiledPos = leftPos; - chiled = this.list[chiledPos]; - } - else - { - T left = this.list[leftPos]; - T right = this.list[rightPos]; - if (left.CompareTo(right) < 0) - { - chiledPos = leftPos; - chiled = left; - } - else + T wrk = this.list[wrkPos]; + if (chiled.CompareTo(wrk) > 0) { - chiledPos = rightPos; - chiled = right; + chiledPos = wrkPos; + chiled = wrk; } } if (current.CompareTo(chiled) < 0) break; this.list[currentPos] = chiled; - this.list[chiledPos] = current; - current = chiled; currentPos = chiledPos; } + this.list[currentPos] = current; } #else -- 2.11.0