All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.unimi.dsi.fastutil.longs.LongHeapSemiIndirectPriorityQueue Maven / Gradle / Ivy

Go to download

fastutil extends the Java Collections Framework by providing type-specific maps, sets, lists and priority queues with a small memory footprint and fast access and insertion; provides also big (64-bit) arrays, sets and lists, and fast, practical I/O classes for binary and text files.

There is a newer version: 8.5.15
Show newest version
/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   .  */
/* This header is separate from features.h so that the compiler can
   include it implicitly at the start of every compilation.  It must
   not itself include  or any other header that includes
    because the implicit include comes before any feature
   test macros that may be defined in a source file before it first
   explicitly includes a system header.  GCC knows the name of this
   header in order to preinclude it.  */
/* glibc's intent is to support the IEC 559 math functionality, real
   and complex.  If the GCC (4.9 and later) predefined macros
   specifying compiler intent are available, use them to determine
   whether the overall intent is to support these features; otherwise,
   presume an older compiler has intent to support these features and
   define these macros by default.  */
/* wchar_t uses Unicode 9.0.0.  Version 9.0 of the Unicode Standard is
   synchronized with ISO/IEC 10646:2014, fourth edition, plus
   Amd. 1  and Amd. 2 and 273 characters from forthcoming  10646, fifth edition.
   (Amd. 2 was published 2016-05-01,
   see https://www.iso.org/obp/ui/#iso:std:iso-iec:10646:ed-4:v1:amd:2:v1:en) */
/* We do not support C11 .  */
/* Generic definitions */
/* Assertions (useful to generate conditional code) */
/* Current type and class (and size, if applicable) */
/* Value methods */
/* Interfaces (keys) */
/* Interfaces (values) */
/* Abstract implementations (keys) */
/* Abstract implementations (values) */
/* Static containers (keys) */
/* Static containers (values) */
/* Implementations */
/* Synchronized wrappers */
/* Unmodifiable wrappers */
/* Other wrappers */
/* Methods (keys) */
/* Methods (values) */
/* Methods (keys/values) */
/* Methods that have special names depending on keys (but the special names depend on values) */
/* Equality */
/* Object/Reference-only definitions (keys) */
/* Primitive-type-only definitions (keys) */
/* Object/Reference-only definitions (values) */
/*		 
 * Copyright (C) 2003-2016 Paolo Boldi and Sebastiano Vigna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package it.unimi.dsi.fastutil.longs;

import java.util.NoSuchElementException;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.AbstractIndirectPriorityQueue;

/**
 * A type-specific heap-based semi-indirect priority queue.
 *
 * 

* Instances of this class use as reference list a reference array, * which must be provided to each constructor. The priority queue is represented * using a heap. The heap is enlarged as needed, but it is never shrunk. Use the * {@link #trim()} method to reduce its size, if necessary. * *

* This implementation allows one to enqueue several time the same index, but * you must be careful when calling {@link #changed()}. */ public class LongHeapSemiIndirectPriorityQueue extends AbstractIndirectPriorityQueue implements LongIndirectPriorityQueue { /** The reference array. */ protected final long refArray[]; /** The semi-indirect heap. */ protected int heap[] = IntArrays.EMPTY_ARRAY; /** The number of elements in this queue. */ protected int size; /** The type-specific comparator used in this queue. */ protected LongComparator c; /** * Creates a new empty queue without elements with a given capacity and * comparator. * * @param refArray * the reference array. * @param capacity * the initial capacity of this queue. * @param c * the comparator used in this queue, or null for * the natural order. */ public LongHeapSemiIndirectPriorityQueue(long[] refArray, int capacity, LongComparator c) { if (capacity > 0) this.heap = new int[capacity]; this.refArray = refArray; this.c = c; } /** * Creates a new empty queue with given capacity and using the natural * order. * * @param refArray * the reference array. * @param capacity * the initial capacity of this queue. */ public LongHeapSemiIndirectPriorityQueue(long[] refArray, int capacity) { this(refArray, capacity, null); } /** * Creates a new empty queue with capacity equal to the length of the * reference array and a given comparator. * * @param refArray * the reference array. * @param c * the comparator used in this queue, or null for * the natural order. */ public LongHeapSemiIndirectPriorityQueue(long[] refArray, LongComparator c) { this(refArray, refArray.length, c); } /** * Creates a new empty queue with capacity equal to the length of the * reference array and using the natural order. * * @param refArray * the reference array. */ public LongHeapSemiIndirectPriorityQueue(final long[] refArray) { this(refArray, refArray.length, null); } /** * Wraps a given array in a queue using a given comparator. * *

* The queue returned by this method will be backed by the given array. The * first size element of the array will be rearranged so to * form a heap (this is more efficient than enqueing the elements of * a one by one). * * @param refArray * the reference array. * @param a * an array of indices into refArray. * @param size * the number of elements to be included in the queue. * @param c * the comparator used in this queue, or null for * the natural order. */ public LongHeapSemiIndirectPriorityQueue(final long[] refArray, final int[] a, int size, final LongComparator c) { this(refArray, 0, c); this.heap = a; this.size = size; LongSemiIndirectHeaps.makeHeap(refArray, a, size, c); } /** * Wraps a given array in a queue using a given comparator. * *

* The queue returned by this method will be backed by the given array. The * elements of the array will be rearranged so to form a heap (this is more * efficient than enqueing the elements of a one by one). * * @param refArray * the reference array. * @param a * an array of indices into refArray. * @param c * the comparator used in this queue, or null for * the natural order. */ public LongHeapSemiIndirectPriorityQueue(final long[] refArray, final int[] a, final LongComparator c) { this(refArray, a, a.length, c); } /** * Wraps a given array in a queue using the natural order. * *

* The queue returned by this method will be backed by the given array. The * first size element of the array will be rearranged so to * form a heap (this is more efficient than enqueing the elements of * a one by one). * * @param refArray * the reference array. * @param a * an array of indices into refArray. * @param size * the number of elements to be included in the queue. */ public LongHeapSemiIndirectPriorityQueue(final long[] refArray, final int[] a, int size) { this(refArray, a, size, null); } /** * Wraps a given array in a queue using the natural order. * *

* The queue returned by this method will be backed by the given array. The * elements of the array will be rearranged so to form a heap (this is more * efficient than enqueing the elements of a one by one). * * @param refArray * the reference array. * @param a * an array of indices into refArray. */ public LongHeapSemiIndirectPriorityQueue(final long[] refArray, final int[] a) { this(refArray, a, a.length); } /** * Ensures that the given index is a valid reference. * * @param index * an index in the reference array. * @throws IndexOutOfBoundsException * if the given index is negative or larger than the reference * array length. */ protected void ensureElement(final int index) { if (index < 0) throw new IndexOutOfBoundsException("Index (" + index + ") is negative"); if (index >= refArray.length) throw new IndexOutOfBoundsException("Index (" + index + ") is larger than or equal to reference array size (" + refArray.length + ")"); } public void enqueue(int x) { ensureElement(x); if (size == heap.length) heap = IntArrays.grow(heap, size + 1); heap[size++] = x; LongSemiIndirectHeaps.upHeap(refArray, heap, size, size - 1, c); } public int dequeue() { if (size == 0) throw new NoSuchElementException(); final int result = heap[0]; heap[0] = heap[--size]; if (size != 0) LongSemiIndirectHeaps.downHeap(refArray, heap, size, 0, c); return result; } public int first() { if (size == 0) throw new NoSuchElementException(); return heap[0]; } /** * {@inheritDoc} * *

* The caller must guarantee that when this method is * called the index of the first element appears just once in the queue. * Failure to do so will bring the queue in an inconsistent state, and will * cause unpredictable behaviour. */ public void changed() { LongSemiIndirectHeaps.downHeap(refArray, heap, size, 0, c); } /** * Rebuilds this heap in a bottom-up fashion. */ public void allChanged() { LongSemiIndirectHeaps.makeHeap(refArray, heap, size, c); } public int size() { return size; } public void clear() { size = 0; } /** * Trims the backing array so that it has exactly {@link #size()} elements. */ public void trim() { heap = IntArrays.trim(heap, size); } public LongComparator comparator() { return c; } public int front(final int[] a) { return c == null ? LongSemiIndirectHeaps.front(refArray, heap, size, a) : LongSemiIndirectHeaps.front(refArray, heap, size, a, c); } public String toString() { StringBuffer s = new StringBuffer(); s.append("["); for (int i = 0; i < size; i++) { if (i != 0) s.append(", "); s.append(refArray[heap[i]]); } s.append("]"); return s.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy