Back to index

wims  3.65+svn20090927
Public Member Functions | Static Public Member Functions | Package Attributes
rene.util.MyVector Class Reference

This is a more effective replacement of the Vector class. More...

Inheritance diagram for rene.util.MyVector:
Inheritance graph
[legend]
Collaboration diagram for rene.util.MyVector:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MyVector (int initsize)
 MyVector ()
void addElement (Object o)
 Add an element.
void extend ()
 Extend the array, or get space by compressing it.
void compress ()
 Compress the array.
Enumeration elements ()
 Get an enumeration of this array.
boolean hasMoreElements ()
 Method for Enumeration.
Object nextElement ()
 Method for Enumeration.
void removeAllElements ()
 Clear this array, but keep its memory!
void removeElement (Object o)
 Remove a single element.
int indexOf (Object o)
 Find an element.
int size ()
Object elementAt (int n)
 Get the element at a given position.
Object[] getArray ()
 Get the array itself (compressed).
void copyInto (Object o[])
 Copy the array into an object array of at least the same size.
boolean equals (MyVector V)
 Test for equality with another vector, using equals.
boolean equalsIdentical (MyVector V)
 Test for equality with another vector, using object equality.
void truncate (int n)
 Trancate the vector to n elements, if it has more.

Static Public Member Functions

static void main (String args[])

Package Attributes

Object O []
int OSize
int ON
int OLast
int Gap
int EN = 0

Detailed Description

This is a more effective replacement of the Vector class.

It is based on a growing array. If an object is removed, it is replaced by null. The class knows about the first null object (the gap). Searching for elements or other operations automatically compress the array by copying the elements upwards, but only as far as they need to go anyway.

Accessing an element is very effective, at least the second time. If you want to make sure, it is always effective, compress first. The most effective way is to get the object array itself.

The objects can be enumerated. The object returned by nextElement() is found very rapidly. E.g. it can be deleted at once.

Enumeration is not reentrant. Do it only once each time.

Nothing in this class is synchronized!

Definition at line 25 of file MyVector.java.


Constructor & Destructor Documentation

rene.util.MyVector.MyVector ( int  initsize) [inline]

Definition at line 31 of file MyVector.java.

       {      O=new Object[initsize];
              OSize=initsize;
              OLast=ON=0;
              Gap=-1;
       }

Definition at line 37 of file MyVector.java.

       {      this(8);
       }

Here is the caller graph for this function:


Member Function Documentation

void rene.util.MyVector.addElement ( Object  o) [inline]

Add an element.

Extend the array, if necessary.

Definition at line 44 of file MyVector.java.

       {      if (OLast>=OSize) extend();
              O[OLast++]=o;
              ON++;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

void rene.util.MyVector.compress ( ) [inline]

Compress the array.

Definition at line 66 of file MyVector.java.

       {      if (Gap<0) return;
              int k=Gap;
              for (int i=Gap; i<OLast; i++)
              {      if (O[i]==null) continue;
                     O[k++]=O[i];
              }
              ON=k;
              for (int i=k; i<OLast; i++) O[i]=null;
              Gap=-1; OLast=ON;
       }

Here is the caller graph for this function:

void rene.util.MyVector.copyInto ( Object  o[]) [inline]

Copy the array into an object array of at least the same size.

Definition at line 205 of file MyVector.java.

       {      compress();
              System.arraycopy(O,0,o,0,ON);
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Object rene.util.MyVector.elementAt ( int  n) [inline]

Get the element at a given position.

Second access will always be effective. First access compresses. Throws an exception, if the index is invalid.

Definition at line 169 of file MyVector.java.

       {      if (n<0 || n>=ON)
                     throw new ArrayIndexOutOfBoundsException(n);
              if (Gap<0 || n<Gap) return O[n];
              int k=Gap;
              for (int i=Gap; i<OLast; i++)
              {      if (O[i]==null) continue;
                     O[k]=O[i]; O[i]=null;
                     if (k==n)
                     {      Object ret=O[k];
                            k++; Gap=k;
                            if (Gap>=ON)
                            {      for (int j=Gap; j<OLast; j++) O[j]=null;
                                   OLast=ON; Gap=-1;
                            }
                            return ret;
                     }
                     k++;
              }
              // never happens
              throw new ArrayIndexOutOfBoundsException(n); 
       }

Here is the caller graph for this function:

Enumeration rene.util.MyVector.elements ( ) [inline]

Get an enumeration of this array.

Definition at line 81 of file MyVector.java.

       {      compress(); EN=0;
              return this;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Test for equality with another vector, using equals.

Definition at line 213 of file MyVector.java.

       {      if (V.ON!=ON) return false;
              V.compress(); compress();
              for (int i=0; i<ON; i++)
              {      if (!V.O[i].equals(O[i])) return false;
              }
              return true;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Test for equality with another vector, using object equality.

Definition at line 225 of file MyVector.java.

       {      if (V.ON!=ON) return false;
              V.compress(); compress();
              for (int i=0; i<ON; i++)
              {      if (V.O[i]!=O[i]) return false;
              }
              return true;
       }

Here is the call graph for this function:

void rene.util.MyVector.extend ( ) [inline]

Extend the array, or get space by compressing it.

Definition at line 53 of file MyVector.java.

       {      if (ON<OLast/2)
              {      compress(); return;
              }
              Object o[]=new Object[2*OSize];
              System.arraycopy(O,0,o,0,OLast);
              OSize*=2;
              O=o;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Object [] rene.util.MyVector.getArray ( ) [inline]

Get the array itself (compressed).

Make sure, you also use size() to determine the true length of the array. Do not change objects beyond the size! Do not set objects to null!

Definition at line 197 of file MyVector.java.

       {      compress();
              return O;
       }

Here is the call graph for this function:

Method for Enumeration.

Definition at line 89 of file MyVector.java.

       {      while (EN<OLast && O[EN]==null) EN++;
              return EN<OLast;
       }

Here is the caller graph for this function:

int rene.util.MyVector.indexOf ( Object  o) [inline]

Find an element.

Compress on the way. Check for the last element, returned by nextElement() first. Equality is checked with the equal() function.

Returns:
-1, if not found.

Definition at line 131 of file MyVector.java.

       {      if (EN>0 && EN<=OLast && O[EN-1].equals(o)) return EN-1;
              if (Gap<0)
              {      for (int i=0; i<OLast; i++)
                     {      if (O[i].equals(o)) return i;
                     }
                     return -1;
              }
              for (int i=0; i<Gap; i++)
              {      if (O[i].equals(o)) return i;
              }
              int k=Gap;
              for (int i=Gap; i<OLast; i++)
              {      if (O[i]==null) continue;
                     if (O[i].equals(o))
                     {      Gap=k;
                            return i;
                     }
                     O[k++]=O[i]; O[i]=null;
              }
              ON=k;
              for (int i=k; i<OLast; i++) O[i]=null;
              Gap=-1; OLast=ON;
              return -1;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

static void rene.util.MyVector.main ( String  args[]) [inline, static]

Definition at line 244 of file MyVector.java.

       {      MyVector V=new MyVector();
              for (int i=1; i<=10; i++)
                     V.addElement("Element "+i);
              for (int i=4; i<=9; i++)
                     V.removeElement("Element "+i);
              System.out.println("--> "+V.elementAt(3));
              System.out.println(V.ON+" elements, "+V.OLast+" used, "+V.Gap+" gap.");
              System.out.println("--> "+V.elementAt(3));
              System.out.println(V.ON+" elements, "+V.OLast+" used, "+V.Gap+" gap.");
              for (int i=11; i<=20; i++)
                     V.addElement("Element "+i);
              System.out.println(V.ON+" elements, "+V.OLast+" used ,"+V.Gap+" gap.");
              Enumeration E=V.elements();
              while (E.hasMoreElements())
              {      System.out.println((String)E.nextElement());
              }
              System.out.println(V.ON+" elements, "+V.OLast+" used, "+V.Gap+" gap.");
       }

Here is the call graph for this function:

Object rene.util.MyVector.nextElement ( ) [inline]

Method for Enumeration.

Definition at line 97 of file MyVector.java.

       {      if (!hasMoreElements())
                     throw new ArrayIndexOutOfBoundsException(OLast);
              return O[EN++];
       }

Here is the call graph for this function:

Clear this array, but keep its memory!

Definition at line 106 of file MyVector.java.

       {      for (int i=0; i<OLast; i++) O[i]=null;
              ON=OLast=0; Gap=-1;
       }

Here is the caller graph for this function:

void rene.util.MyVector.removeElement ( Object  o) [inline]

Remove a single element.

This will also compress the part below the element, or all, if it is not found.

Definition at line 115 of file MyVector.java.

       {      int i=indexOf(o);
              if (i<0) return;
              O[i]=null; ON--;
              if (Gap<0 || Gap>i) Gap=i;
              if (i==OLast-1) OLast--;
              while (OLast>0 && O[OLast-1]==null) OLast--;
              if (Gap>=OLast) Gap=-1;
       }

Here is the call graph for this function:

Here is the caller graph for this function:

int rene.util.MyVector.size ( ) [inline]
Returns:
the number of objects in the vector.

Definition at line 160 of file MyVector.java.

       {      return ON;
       }

Here is the caller graph for this function:

void rene.util.MyVector.truncate ( int  n) [inline]

Trancate the vector to n elements, if it has more.

Definition at line 237 of file MyVector.java.

       {      if (n>=ON) return;
              compress();
              for (int i=n; i<OLast; i++) O[i]=null;
              OLast=ON=n;
       }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

int rene.util.MyVector.EN = 0 [package]

Definition at line 29 of file MyVector.java.

int rene.util.MyVector.Gap [package]

Definition at line 28 of file MyVector.java.

Object rene.util.MyVector.O[] [package]

Definition at line 27 of file MyVector.java.

int rene.util.MyVector.OLast [package]

Definition at line 28 of file MyVector.java.

int rene.util.MyVector.ON [package]

Definition at line 28 of file MyVector.java.

int rene.util.MyVector.OSize [package]

Definition at line 28 of file MyVector.java.


The documentation for this class was generated from the following file: