Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / April 2005

Tip: Looking for answers? Try searching our database.

Index Heap based P-Queue

Thread view: 
Carlos Brando - 25 Apr 2005 07:46 GMT
Hi,
I'm searching for an indexed based priorityqueue (like in Sedgewick
Program 9.12) in Java.

My Intention is to update the priority of the nodes or delete a node in
the queue, without scaning everytime the queue. (i.e. access via index)

Does anyone implemented such a datastructure in Java or knows where I
can find it on the web.

Thanx a lot

Carlos
John - 26 Apr 2005 01:43 GMT
Do you mean queue implemented using circular arrays?
That's easy to do. You can do it yourself pretty fast.

Here's a sample:

public class CircularArray {
   
    private Object m_elements[];
    private int m_head, m_tail;
   
    /** This is the constructor that creates an instance of CircularArray.
    * @param maxsize an <code>int</code> value representing the maximum
number of objects
    * that can be stored in this object. The maximum size has to be at
least 2, so a smaller
    * number passed as maxsize to the constructor will be disregarded and
2 will be used.
    * Given the fact that one position in a circular array is not usable,
the size of the
    * underlying array will be one larger than the argument.
    */
    public CircularArray(int maxsize) {
        if(maxsize<2)
            m_elements=new Object[3];
        else
            m_elements=new Object[maxsize+1];
        reset();
    }
   
    /**
    * This method returns true if the object is void otherwise false.
    * @return true if there are no elements in this object.
    */
    public boolean isVoid() {
        return ((m_head+1)%m_elements.length)==m_tail;
    }
   
    /**
    * This method returns true if the object is full otherwise null.
    * @return true if there are no more free positions.
    */
    public boolean isFull() {
        return ((m_head+2)%m_elements.length)==m_tail;
    }
   
    /** This method adds an element to this object.
    * @param obj an <code>Object</code> to be added to this object
    * @exception CircularArrayException is thrown if the object is full.
    */
    public void push(Object obj) throws CircularArrayException {
        if(isFull())
            throw new CircularArrayException(CircularArrayException.TYPE_FULLPUSH);
        else {
            m_head=(m_head+1)%m_elements.length;
            m_elements[m_head]=obj;
        }
    }
   
    /** This method adds an element to this object without throwing an
exception on error.
    * The user should test whether or not the array is full before adding
a new element
    * using the isFull method.
    * @param obj an <code>Object</code> to be added to this object
    */
    public void pushnoex(Object obj) {
        m_head=(m_head+1)%m_elements.length;
        m_elements[m_head]=obj;
    }
   
    /** This method returns the oldest element in this object.
    * @return an <code>Object</code> representing the oldest object in
this object.
    * @exception CircularArrayException is thrown if the object is void.
    */
    public Object pop() throws CircularArrayException {
        if(isVoid())
            throw new CircularArrayException(CircularArrayException.TYPE_EMPTYPOP);
        else {
            Object res=m_elements[m_tail];
            m_tail=(m_tail+1)%m_elements.length;
            return res;
        }
    }
   
    /** This method returns the oldest element in this object without
throwing an exception
    * when the operation is performed on an empty array. The user should
check whether or not
    * the array is empty before using this method using the isVoid method.
    * @return an <code>Object</code> representing the oldest element in
this object.
    */
    public Object popnoex() {
        Object res=m_elements[m_tail];
        m_tail=(m_tail+1)%m_elements.length;
        return res;
    }
   
    /** This method returns the maximum number of elements this object can
contain.
    * @return an <code>int</code> representing the maximum number of
elements this object
    * can contain
    */
    public int getMaxSize() {
        return m_elements.length-1;
    }
   
    /** This method returns the number of elements present in this object
    * @return an <code>int</code> representing the total number of
elements existing in this object.
    * This number is always at least 1 smaller than the value returned by
{@link #getMaxSize}
    */
    public int getSize() {
        int size;
        size=1+m_head-m_tail;
        if(size<0)
            size=m_elements.length+size;
        size=size%m_elements.length;
        return size;
    }
   
    /**
    * This method returns the position of the internal pointer that
specifies the place where the next push
    * will add a new element.
    * @return an <code>int</code> value.
    */
    public int getHeadValue() {
        return m_head;
    }
   
    /** This method returns the position of the internal pointer that
specifies the place from where the next
    * pop will extract an element.
    * @return an <code>int</code> value
    */
    public int getTailValue() {
        return m_tail;
    }
   
    /** This method allows the user to set the value of the element on a
given position.
    * The position is zero based. The tail pointer specifies position 0.
The highest
    * position that can be set is getSize()-1.
    * @param position an <code>int</code> specifying the position
    * @param value an <code>Object</code> representing the new element on
the given position
    * @return the old element on the given position.
    * @exception ArrayIndexOutOfBoundsException if the position is before
0 or after the last element
    */
    public Object set(int position,Object value) throws
ArrayIndexOutOfBoundsException{
        if(position<0&&position>=getSize())
            throw new ArrayIndexOutOfBoundsException("set exception: "+position);
        Object res=m_elements[(m_tail+position)%m_elements.length];
        m_elements[(m_tail+position)%m_elements.length]=value;
        return res;
    }
   
   
    /** This method allows the user to get the value of the element on a
given position.
    * The position is zero based. The tail pointer specifies position 0.
The highest
    * position than can be queried is getSize()-1.
    * @param position an <code>int</code> specifying the position
    * @return an <code>Object</code> representing the element on the given
position.
    * @exception ArrayIndexOutOfBoundsException if the position is before
0 or after the last element
    */
    public Object get(int position) throws ArrayIndexOutOfBoundsException{
        if(position<0&&position>=getSize())
            throw new ArrayIndexOutOfBoundsException("get exception: "+position);
        return m_elements[(m_tail+position)%m_elements.length];
    }
   
    /** This method allows the user to set the value of the element on a
given position without
    * throwing an exception in case of overflowing the array. Make sure
the position is lower than
    * the number of elements before using this method.
    * @oaram position an <code>int</code> specifying the position
    * @param value an <code>Object</code> representing the new element on
the given position
    * @return the old element on the given position
    */
    public Object setnoex(int position, Object value) {
        Object res=m_elements[(m_tail+position)%m_elements.length];
        m_elements[(m_tail+position)%m_elements.length]=value;
        return res;
    }
   
    /** This method allows the user to get the value of the elements on a
given position without
    * throwing an exception. The user should check that the position
queried is after 0 and less than the
    * size of the array.
    * @param position an <code>int</code> specifying the position
    * @return an <code>Object</code> representing the element on the given
position
    */
    public Object getnoex(int position) {
        return m_elements[(m_tail+position)%m_elements.length];
    }
   
    /** This method deletes all the elements in the array and resets the
pointers to a void array
    */
    public void reset() {
        for(int i=0;i<m_elements.length;i++)
            m_elements[i]=null;
        m_head=0;
        m_tail=1;
    }
   
   
}

> Hi,
> I'm searching for an indexed based priorityqueue (like in Sedgewick
[quoted text clipped - 9 lines]
>
> Carlos
Esmond Pitt - 26 Apr 2005 02:37 GMT
>> I'm searching for an indexed based priorityqueue (like in Sedgewick
>>Program 9.12) in Java.
>
> Do you mean queue implemented using circular arrays?

No he doesn't.
John - 27 Apr 2005 04:02 GMT
I see.
I was thinking about that when I read about accessing the elements using
indices.

>>> I'm searching for an indexed based priorityqueue (like in Sedgewick
>>> Program 9.12) in Java.
>>
>> Do you mean queue implemented using circular arrays?
>
> No he doesn't.
Daniel Rohe - 26 Apr 2005 08:13 GMT
A priority queue implementation is in Jakarta Commons Collections
(http://jakarta.apache.org/commons/collections). I've used that for external
sorting of a great amount of data. Maybe you could use or extend it.

Daniel

> Hi,
> I'm searching for an indexed based priorityqueue (like in Sedgewick
[quoted text clipped - 9 lines]
>
> Carlos


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.