OSDN Git Service

BinaryHeap, fixed up so it can actually be used.
authorNathanSweet <nathan.sweet@gmail.com>
Mon, 1 Oct 2012 01:55:08 +0000 (18:55 -0700)
committerNathanSweet <nathan.sweet@gmail.com>
Mon, 1 Oct 2012 01:55:08 +0000 (18:55 -0700)
gdx/src/com/badlogic/gdx/utils/BinaryHeap.java

index 673f559..9e79929 100644 (file)
 package com.badlogic.gdx.utils;\r
 \r
 /** @author Nathan Sweet */\r
-public class BinaryHeap<T> {\r
+public class BinaryHeap<T extends BinaryHeap.Node> {\r
        public int size = 0;\r
 \r
-       private Node<T>[] nodes;\r
+       private Node[] nodes;\r
        private final boolean isMaxHeap;\r
 \r
        public BinaryHeap () {\r
@@ -32,7 +32,7 @@ public class BinaryHeap<T> {
                nodes = new Node[capacity];\r
        }\r
 \r
-       public Node add (Node node) {\r
+       public T add (T node) {\r
                // Expand if necessary.\r
                if (size == nodes.length) {\r
                        Node[] newNodes = new Node[size << 1];\r
@@ -46,16 +46,16 @@ public class BinaryHeap<T> {
                return node;\r
        }\r
 \r
-       public Node pop () {\r
+       public T pop () {\r
                Node[] nodes = this.nodes;\r
                Node popped = nodes[0];\r
                nodes[0] = nodes[--size];\r
                nodes[size] = null;\r
                if (size > 0) down(0);\r
-               return popped;\r
+               return (T)popped;\r
        }\r
 \r
-       public void setValue (Node node, float value) {\r
+       public void setValue (T node, float value) {\r
                float oldValue = node.value;\r
                node.value = value;\r
                if (value < oldValue ^ isMaxHeap)\r
@@ -142,12 +142,16 @@ public class BinaryHeap<T> {
        }\r
 \r
        /** @author Nathan Sweet */\r
-       static public class Node<T> {\r
+       static public class Node {\r
                float value;\r
                int index;\r
 \r
                public Node (float value) {\r
                        this.value = value;\r
                }\r
+\r
+               public float getValue () {\r
+                       return value;\r
+               }\r
        }\r
 }\r