swarm-support
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: QSort question from yesterday still unsolved


From: Marcus G. Daniels
Subject: Re: QSort question from yesterday still unsolved
Date: 02 Aug 2001 15:13:44 -0600
User-agent: Gnus/5.070084 (Pterodactyl Gnus v0.84) Emacs/20.7

Here's an example of how to use QSort in Java. 

Some particulars:

  1) Use (and if necessary, implement) the compareTo method in the Java class.
     Note it doesn't have two arguments.  

  2) Make the Selector against the object type you'll be comparing

  3) Use a Swarm List.  The JavaCollectionIndexProxy in Swarm uses a 
     ordinary Java Iterator, which doesn't provide the `set' that 
     QSort needs in order to rearrange the list. 

import swarm.Globals; 
import swarm.simtools.NSelectImpl; 
import swarm.simtools.QSortImpl;
import swarm.simtools.QSort;
import swarm.Selector;
import swarm.collections.List;
import swarm.collections.ListImpl;
import swarm.collections.Index;
import java.lang.Integer;

public class TestQSort { 

    static void main (String args[]) { 
        Globals.env.initSwarm ("TestQSort",
                               "address@hidden",
                               "0.0",
                               args); 

        List aList = new ListImpl (Globals.env.globalZone);
        
        for (int i = 0; i < 10; i++) 
            aList.addFirst (new Integer (i)); 

        printList (aList);
        Selector sel = getASelector (Integer.class, "compareTo");

        new QSortImpl().sortObjectsIn$using (aList, sel); 
        System.out.println ("sorted:");

        printList (aList);

        aList.drop ();
    }

    static void printList (List aList)
    {
        Index index = aList.listBegin (Globals.env.scratchZone);
        for (Object obj = index.next ();
             index.getLoc () == Globals.env.Member;
             obj = index.next ())
            System.out.println (obj);
        index.drop ();
    }
    
    static Selector getASelector (Class a, String s) {
        Selector sel = null;
        try {
            sel = new Selector (a,s,false);
        }
        catch (Exception e) {
            e.printStackTrace (System.err);
            System.exit (1);
        }
        return sel;
    }
}

                  ==================================
   Swarm-Support is for discussion of the technical details of the day
   to day usage of Swarm.  For list administration needs (esp.
   [un]subscribing), please send a message to <address@hidden>
   with "help" in the body of the message.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]