]> code.citadel.org Git - citadel.git/commitdiff
cleaned up roomMap a lot. We now sort rooms based on their sort order.
authorChilly <chilly@uncensored.citadel.org>
Thu, 29 Jul 1999 21:35:36 +0000 (21:35 +0000)
committerChilly <chilly@uncensored.citadel.org>
Thu, 29 Jul 1999 21:35:36 +0000 (21:35 +0000)
shaggy/STATUS
shaggy/SortedVector.java [new file with mode: 0644]
shaggy/roomMap.java
shaggy/sorter.java [new file with mode: 0644]

index 0be01304dd632790a048a62a5f889921560394df..940ee01ff8d4072ec024b03ca21d3f49cd510e6f 100644 (file)
@@ -18,7 +18,7 @@ Basic:
 "main"
 - Splits rooms up into new and no-new
  * limited floor support (shows in message panel)
- * doesn't sort the room names, even though there's "no pressing reason not to"
+- rooms are now sorted
 
 - Go to next room with messages
 - go to any old room by double-clicking from list
diff --git a/shaggy/SortedVector.java b/shaggy/SortedVector.java
new file mode 100644 (file)
index 0000000..529f745
--- /dev/null
@@ -0,0 +1,89 @@
+/* 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();
+  }
+}
+
index 50add9d5f693966916fe4749c7ff7be2f1b42b1d..006606be80ee5a645b2406cec9becaff8aacf137 100644 (file)
@@ -7,7 +7,7 @@ import java.awt.List;
 
 public class roomMap {
   Hashtable    floors, rooms;
-  Vector       nrm, srm;
+  SortedVector nrm, srm;
   List         nrmL, srmL;
   boolean      refreshed;
 
@@ -15,8 +15,8 @@ public class roomMap {
     nrmL = null;
     srmL = null;
 
-    nrm = new Vector();
-    srm = new Vector();
+    nrm = new SortedVector( new roomCmp() );
+    srm = new SortedVector( new roomCmp() );
     floors = null;
     rooms = null;
 
@@ -27,6 +27,8 @@ public class roomMap {
     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 );
@@ -54,17 +56,17 @@ public class roomMap {
 
     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;
 
@@ -73,10 +75,14 @@ public class roomMap {
       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 ) {
@@ -116,22 +122,32 @@ public class roomMap {
     }
   }
 
-  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;
   }
 }
 
@@ -176,10 +192,10 @@ class room {
 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 );
@@ -204,6 +220,7 @@ class floor {
   }
 
   public void addRoom( room r ) {
-    rooms.addElement( r );
+    if( !rooms.isElement( r ) )
+      rooms.addElement( r );
   }
 }
diff --git a/shaggy/sorter.java b/shaggy/sorter.java
new file mode 100644 (file)
index 0000000..997f019
--- /dev/null
@@ -0,0 +1,7 @@
+/* sorted.java
+ * interface for use in SortedVector
+ */
+
+public abstract class sorter {
+  public abstract int cmp( Object o1, Object o2 );     /* -1, 0 (equal), 1 */
+}