--- /dev/null
+/* SortedVector.java
+ * insert-sort vector (default to strings)
+ */
+
+import java.awt.*;
+import java.util.*;
+
+public class SortedVector {
+ Vector theList;
+ sorter cmpr;
+
+ public SortedVector() {
+ theList = new Vector();
+ cmpr = null;
+ }
+
+ public SortedVector( sorter s ) {
+ theList = new Vector();
+ cmpr = s;
+ }
+
+ public int addElement( Object theId ) {
+ int i, cmp;
+ for( i = 0; i < theList.size(); i++ ) {
+
+ if( cmpr == null )
+ cmp = ((String)theList.elementAt(i)).compareTo( (String)theId );
+ else
+ cmp = cmpr.cmp( theList.elementAt( i ), theId );
+
+ if( cmp == 0 ) return -1;
+ if( cmp > 0 )
+ break;
+ }
+
+ theList.insertElementAt( theId, i );
+ return i;
+ }
+
+ public Object firstElement() {
+ return theList.firstElement();
+ }
+
+ public Object removeItem( int index ) {
+ if( (index < 0) || (index > theList.size()) )
+ return null;
+
+ Object theItem = theList.elementAt( index );
+ theList.removeElementAt( index );
+ return theItem;
+ }
+
+ public int removeElement( Object theItem ) {
+ for( int i = 0; i < theList.size(); i++ ) {
+ boolean equal;
+ if( cmpr == null )
+ equal = ((String)theList.elementAt( i )).equalsIgnoreCase( (String)theItem );
+ else
+ equal = cmpr.cmp( theList.elementAt( i ), theItem ) == 0;
+ if( equal ) {
+ theList.removeElementAt( i );
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ public boolean isElement( Object theItem ) {
+ for( int i = 0; i < theList.size(); i++ ) {
+ if( cmpr == null ) {
+ if( ((String)theList.elementAt( i )).equalsIgnoreCase( (String)theItem ) )
+ return true;
+ } else {
+ if( cmpr.cmp( theList.elementAt( i ), theItem ) == 0 )
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void removeAllElements() {
+ theList.removeAllElements();
+ }
+
+ public Enumeration elements() {
+ return theList.elements();
+ }
+}
+
public class roomMap {
Hashtable floors, rooms;
- Vector nrm, srm;
+ SortedVector nrm, srm;
List nrmL, srmL;
boolean refreshed;
nrmL = null;
srmL = null;
- nrm = new Vector();
- srm = new Vector();
+ nrm = new SortedVector( new roomCmp() );
+ srm = new SortedVector( new roomCmp() );
floors = null;
rooms = null;
floors = new Hashtable();
citReply r = citadel.me.getReply( "LFLR" );
+ if( r.error() ) return;
+
String l;
for( int i = 0; (l = r.getLine( i )) != null; i++ ) {
floor f = new floor( l );
rooms = new Hashtable();
- nrm = new Vector();
+ nrm = new SortedVector( new roomCmp() );
nrmL.clear();
parseRooms( nrm, nrmL, citadel.me.getReply( "LKRN" ) );
- srm = new Vector();
+ srm = new SortedVector( new roomCmp() );
srmL.clear();
parseRooms( srm, srmL, citadel.me.getReply( "LKRO" ) );
refreshed = true;
}
- public void parseRooms( Vector v, List l, citReply r ) {
+ public void parseRooms( SortedVector v, List l, citReply r ) {
int i=0;
String s;
if( rm.valid() ) {
rooms.put( rm.name(), rm );
addRoomToFloor( rm );
- l.addItem( rm.name() );
v.addElement( rm );
}
}
+
+ for( Enumeration e=v.elements(); e.hasMoreElements(); ) {
+ room rm = (room)e.nextElement();
+ l.addItem( rm.name() );
+ }
}
public void addRoomToFloor( room theRoom ) {
}
}
- public void visited( String room ) {
+ public void visited( String rm ) {
if( (nrmL == null) || (srmL == null) ) return;
for( int i = 0; i < nrmL.countItems(); i++ ) {
- if( room.equals( nrmL.getItem( i ) ) ) {
+ if( rm.equals( nrmL.getItem( i ) ) ) {
nrmL.delItem( i );
- srmL.addItem( room );
+ srmL.addItem( rm );
}
}
- for( Enumeration e = nrm.elements(); e.hasMoreElements(); ) {
- room r = (room)e.nextElement();
- if( r.is( room ) ) {
- nrm.removeElement( r );
- srm.addElement( r );
- }
- }
+ room r = getRoom( rm );
+ if( nrm.removeElement( r ) != -1 )
+ srm.addElement( r );
+ }
+}
+
+class roomCmp extends sorter {
+ public int cmp( Object o1, Object o2 ) {
+ room r1 = (room)o1;
+ room r2 = (room)o2;
+
+ /* Do I want to sort on floors here, even if users don't use it? */
+
+ if( r1.order < r2.order ) return -1;
+ else if( r1.order == r2.order )
+ return r1.name().compareTo( r2.name() );
+ return 1;
}
}
class floor {
String name, num;
int number, ref_count;
- Vector rooms;
+ SortedVector rooms;
public floor( String l ) {
- rooms = new Vector();
+ rooms = new SortedVector( new roomCmp() );
int i = l.indexOf( '|' );
num = l.substring( 0, i );
}
public void addRoom( room r ) {
- rooms.addElement( r );
+ if( !rooms.isElement( r ) )
+ rooms.addElement( r );
}
}