/************************************************************************ * $Id: DList.java,v 1.1.1.1 2000/03/29 22:58:14 kkeys Exp $ * * DList * version 1.0 beta ************************************************************************* /*********************************************************************** By accessing this software, DLIST, you are duly informed of and agree to be bound by the conditions described below in this notice: This software product, DLIST, is developed by Bradley Huffaker, and Jaeyeon Jung copyrighted(C) 1998 by the University of California, San Diego (UCSD), with all rights reserved. UCSD administers the NLANR Cache grants, NCR-9796082 and NCR-9616602, under which most of this code was developed. There is no charge for DLIST software. You can redistribute it and/or modify it under the terms of the GNU General Public License, v. 2 dated June 1991 which is incorporated by reference herein. DLIST is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use of it will not infringe on any third party's intellectual property rights. You should have received a copy of the GNU GPL along with the DLIST program. Copies can also be obtained from http://www.gnu.org/copyleft/gpl.html or by writing to University of California, San Diego SDSC/CAIDA/NLANR 9500 Gilman Dr., MS-0505 La Jolla, CA 92093 - 0505 USA Or contact INFO@NLANR.NET **************************************************************************/ package bhuffake.plankton; import java.awt.*; /** DNode ******************************************* * Helper class to DList. Used to store the data *****************************************************/ class DNode { public DisplayObject value; public DNode next; public DNode(DisplayObject val) { value = val; next = null; } } /** DList ******************************************* * This code has been Modifed to Work with * plankton. * * A linked list that has many of the standard * linked list member functions clear, enqueue, * push, pop, and empty. But also have a few of * my own creation: where, reset, next, insert, * and end. Which allow the user to edit/view * list with out destory it. *****************************************************/ public class DList { DNode first; DNode tail; DNode before; DNode where; boolean constant = false; /** DList() ******************************************* * Default Constructor. *****************************************************/ public DList() { where=before=tail = first = null; } /** DList(DList list) ***************************** * Copy constructor. Create new list with same * values. *****************************************************/ public DList(DList list) { if (list.first == null) { tail = first = null; return; } tail = first =new DNode(list.first.value); tail.next = null; DNode his = list.first.next; tail = first; while (his != null) { tail.next = new DNode(his.value); tail = tail.next; his = his.next; } reset(); } /** DList() ************************************** * Gives a new DList with its own where pointer * that points to the same linked list **************************************************/ synchronized DList DList() { DList dlist = new DList(); dlist.first = first; dlist.tail = tail; dlist.constant = false; return dlist; } /** clear() ***************************************** * Removes all nodes from the list. Notes that * because java does all the garabage collection * Simple making is so all nodes point to null * is same thing. *****************************************************/ synchronized public void clear() { if (constant) return; where = before= tail = first = null; } /** add(DisplayObject value) ******************************** * Adds the value in to the list sorting it with the less * operator. *****************************************************/ synchronized public void add(DisplayObject value) { if (constant) return; DNode temp = new DNode(value); if (first == null) { tail = first = temp; first.next = null; return; } if (first.value.greater(value)) { temp.next = first; first = temp; } else { DNode current = first; while (current.next != null && value.greater(current.next.value)) current = current.next; temp.next = current.next; current.next = temp; } if (temp.next == null) tail = temp; if (temp.next == where) before = temp; } /** push(DisplayObject value) ******************************** * Adds the value to the front of the list. * This is modified to use the less then operator to * sort the DList. *****************************************************/ synchronized public void push(DisplayObject value) { if (constant) return; DNode temp = new DNode(value); if (first == null) { tail = first = temp; first.next = null; return; } temp.next = first; first = temp; temp = null; } /** enqueue(DisplayObject value) **************************** * Stores value in a node at the end of the link * list. *****************************************************/ synchronized public void enqueue(DisplayObject value) { if (constant) return; DNode temp = new DNode(value); if (first == null) { tail = first = temp; first.next = null; return; } tail.next = temp; tail = tail.next; tail.next = null; } /** top() ******************************************* * Returns the value of the top node or null * if there are no nodes. *****************************************************/ synchronized public DisplayObject top () { if (first != null) return first.value; else return null; } /** pop() ******************************************* * Removes the top node and then returns * the value it contrained. *****************************************************/ synchronized public DisplayObject pop () { if (first == null) return null; if (first == where) where = first.next; DisplayObject value = first.value; first = first.next; return value; } /** empty() ***************************************** * Checks to see if the list has any nodes. Returns * true if it does else false. *****************************************************/ synchronized public boolean empty() { return (first == null); } /** exists(DisplayObject value) ***************************** * Returns true if value is contained in the link * list and false if it is not found. *****************************************************/ synchronized public boolean exists(DisplayObject value) { DNode current = first; while (current != null) { if (current.value == value) return true; current = current.next; } return false; } /** drop(DisplayObject value) ****************************** * Drop the first node in the list that contains * the passed value. *****************************************************/ synchronized public boolean drop(DisplayObject value) { if (constant) return false; DNode previous = first; DNode current = first; while (current != null) { if (current.value == value) { if (current == first) { previous = first = first.next; } else previous.next = current.next; if (before == current) before = previous; else if (where == current) where = current.next; if (current == tail) tail = previous; return true; } previous = current; current = current.next; } return false; } /** count() ******************************************* * Returns the number of nodes currently in linked * list. *****************************************************/ synchronized public int count() { int count = 0; DNode current = first; while(current != null) { count++; current = current.next; } return count; } /** where() ******************************************* * Returns the value stored in the where pointer. * returns null if the where pointer is null. *****************************************************/ synchronized public DisplayObject where() { if (where != null) return where.value; else return null; } /** next() ****************************************** * Moves the where pointer to the next node in the * link and returns the value that was stored in the * current node, before the move. *****************************************************/ synchronized public DisplayObject next() { if (where == null) return null; DisplayObject temp = where.value; before = where; where = where.next; return temp; } /** remove() **************************************** * Removes the node that where is pointing to. * Then returns the value that was stored in the * where pointer. *****************************************************/ synchronized public DisplayObject remove() { if (constant) return null; if (where == null) return null; if (before == null) first = first.next; DNode temp = where; before.next = where.next; where = where.next; return temp.value; } /** insert(DisplayObject value) **************************** * Inserts a value into the linked list just ahead * of the location of the where pointer. *****************************************************/ synchronized public void insert(DisplayObject value) { if (constant) return; DNode node = new DNode (value); if (before == null) { node.next = first; first = node; } else { node.next = before.next; before.next = node; } where = node; } /** reset() ***************************************** * Reset the where pointer to the beginnen of * Of the linked list. Used to start a next * Loop *****************************************************/ synchronized public void reset() { before = null; where = first; } /** end() ******************************************* * Notes where the where pointer reaches the end * of the list. Used as the boolean statment * when interating throught the loop with next *****************************************************/ synchronized public boolean end() { return (where == null); } /** String print() ********************************** * Returns a string which is a close representaion * of where the major nodes are * f= first * t = tail * b = before * w = where ******************************************************/ synchronized public String print() { DNode current = first; String answer = "["; while (current != null) { if (current == first) answer = answer + "f"; if (current == before) answer = answer + "b"; if (current == where) answer = answer + "w"; if (current == tail) answer = answer + "t"; answer = answer + ","; current = current.next; } if (where == null) answer = answer + "w"; answer = answer + "]"; return answer; } }