swarm-support
[Top][All Lists]
Advanced

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

Re: [Swarm-Support] QSort and its compare function


From: Marcus G. Daniels
Subject: Re: [Swarm-Support] QSort and its compare function
Date: Fri, 11 Nov 2005 09:09:51 -0700
User-agent: Mozilla Thunderbird 1.0.5 (Windows/20050711)

Steve Railsback wrote:

I'm not getting QSort to work in Java. The 'compare' method is never called, so I suspect I have not given it the right name. (I can also provide a tiny test code but I suspect the problem is simple.)

Here is my compare function, in the agent class:   [clip]

Not enough context, sorry. But here's a test case to show what to do. It works for Swarm collections but not for Java collections. Swarm is probably a missing method or somesuch in the proxy index for Java collections. Someone so motivated should file this as a bug on Savannah.

import swarm.Globals;
import swarm.collections.List;
import swarm.collections.ListImpl;
/*
import java.util.List;
import java.util.LinkedList;
*/

import swarm.simtools.QSort;
import swarm.simtools.QSortImpl;
import swarm.Selector;

public class TestQSort {
    class Agent {
        public int value;

        Agent (int value) {
            this.value = value;
        }
        public int compareAgents (Agent peer) {
            return value - peer.value;
        }
    }

    List list;
    TestQSort () {
        list = new ListImpl (Globals.env.globalZone);
        list.addLast (new Agent (5));
        list.addLast (new Agent (1));
        list.addLast (new Agent (10));
        /*
        list = new LinkedList ();
        list.add (new Agent (5));
        list.add (new Agent (1));
        list.add (new Agent (10));
        */
    }
    
    void doSort () {
        Selector sel;
        try {
            sel = new Selector (Agent.class, "compareAgents", false);
            QSort qsort = new QSortImpl (Globals.env.globalZone);
            qsort.sortObjectsIn$using (list, sel);
        } catch (Exception e) {
            System.err.println ("could not find selector " + e);
            System.exit (1);
        }
        
        for (int i = 0; i < 3; i++)
            System.out.println (((Agent) (list.atOffset (i))).value);
        /*
        for (int i = 0; i < 3; i++)
            System.out.println (((Agent) (list.get (i))).value);
        */
    }

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

        new TestQSort ().doSort ();
    }
}

reply via email to

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