/// <summary> /// Builds the heap down. /// </summary> /// <param name="k">the k-th element to start from.</param> public void BuildDown(int k) { int child = k; for (; 2 * child + 1 < size; k = child) { // local subtree root ISchedulableThread root = items[k]; // left child. child = 2 * k + 1; // if left child is bigger then the right then pick the right. if (child != size - 1 && items[child].CompareTo(items[child + 1]) > 0) { child++; } // now compare with root if (root.CompareTo(items[child]) > 0) { if (max.CompareTo(items[child]) < 0) { max = items[child]; } // swamp items[k] = items[child]; items[child] = root; items[k].SchedulableIndex = k; items[child].SchedulableIndex = child; } } }
/// <summary> /// Builds the heap up. /// </summary> /// <param name="k">the k-th element to start from.</param> private void BuildUp(int k) { int parent = k; for (; k > 1; k = parent) { // local subtree root ISchedulableThread root = items[k]; parent = (k - 1) / 2; // check my parent, if im smaller then him then swamp if (root.CompareTo(items[parent]) > 0) { if (max.CompareTo(items[parent]) <= 0) { max = items[parent]; } items[k] = items[parent]; items[parent] = root; items[k].SchedulableIndex = k; items[parent].SchedulableIndex = parent; } } }
/// <summary> /// Checks if k-th element is smaller then it's parent and builds up /// if that yeilds true. /// </summary> /// <param name="k">k-th element.</param> /// <param name="parent">the parent.</param> /// <param name="item">the value of k-th element.</param> /// <returns>boolean value indicating that the heap was build up.</returns> private bool CheckAndBuildUp(int k, int parent, ISchedulableThread item) { if (parent > 0 && item.CompareTo(items[parent]) < 0) { items[k] = items[parent]; items[parent] = item; items[k].SchedulableIndex = k; items[parent].SchedulableIndex = parent; BuildUp(parent); return(true); } return(false); }
/// <summary> /// Checks if k-th element is bigger then it's parent and builds down /// if that yeilds true. /// </summary> /// <param name="k">k-th element.</param> /// <param name="parent">the parent.</param> /// <param name="item">the value of k-th element.</param> /// <returns>boolean value indicating that the heap was build down.</returns> private bool CheckAndBuildDown(int k, int child, ISchedulableThread item) { if (child != items.Length && child < items.Length && item.CompareTo(items[child]) > 0) { // swamp items[k] = items[child]; items[child] = item; items[k].SchedulableIndex = k; items[child].SchedulableIndex = child; BuildDown(child); return(true); } return(false); }
/// <summary> /// Checks if k-th element is smaller then it's parent and builds up /// if that yeilds true. /// </summary> /// <param name="k">k-th element.</param> /// <param name="parent">the parent.</param> /// <param name="item">the value of k-th element.</param> /// <returns>boolean value indicating that the heap was build up.</returns> private bool CheckAndBuildUp(int k, int parent, ISchedulableThread item) { if (parent > 0 && item.CompareTo(items[parent]) < 0) { items[k] = items[parent]; items[parent] = item; items[k].SchedulableIndex = k; items[parent].SchedulableIndex = parent; BuildUp(parent); return true; } return false; }
/// <summary> /// Checks if k-th element is bigger then it's parent and builds down /// if that yeilds true. /// </summary> /// <param name="k">k-th element.</param> /// <param name="parent">the parent.</param> /// <param name="item">the value of k-th element.</param> /// <returns>boolean value indicating that the heap was build down.</returns> private bool CheckAndBuildDown(int k, int child, ISchedulableThread item) { if (child != items.Length && child < items.Length && item.CompareTo(items[child]) > 0) { // swamp items[k] = items[child]; items[child] = item; items[k].SchedulableIndex = k; items[child].SchedulableIndex = child; BuildDown(child); return true; } return false; }