Sunday, 15 May 2011

java - Debugging Challenge in regards to TreeSet -



java - Debugging Challenge in regards to TreeSet -

so going question, figured out problem while writing it. perhaps useful others (i remove question if it's duplicate or deemed inappropriate site). know of 2 possible solutions problem, perhaps come improve 1 thought of.

i don't understand why treeset isn't removing first element here. size of treeset supposed remain bounded, appears grow without bound.

here believe relevant code:

this code resides within of double loop. num_groups static final int set 100. newgroups treeset<teamgroup> object initialized (with no elements) before double loop (the variables group , team 2 for-each loops).

final teamgroup newgroup = new teamgroup(group, team); newgroups.add(newgroup); system.err.println("size of newgroups: " + newgroups.size()); if (newgroups.size() > num_groups) { system.err.println("removing first newgroups"); newgroups.remove(newgroups.first()); system.err.println("new size of newgroups: " + newgroups.size()); }

i included debugging statements show problem appear happen. next types of output:

size of newgroups: 44011 removing first newgroups new size of newgroups: 44011

you see although if statement beingness entered, size of treeset<teamgroup> teamgroups isn't beingness decremented. seem me way happen if remove phone call doesn't remove anything--but how can not remove phone call first() should definitely element in treeset?

here compareto method in teamgroup class (score int reasonably same many different teamgroup objects hence why utilize r_id field tie-breaker):

public int compareto(teamgroup o) { // sorts low high when pop off of treeset object, // lowest value gets popped off (and keeps highest values). if (o.score == this.score) homecoming this.r_id - o.r_id; homecoming this.score - o.score; }

here equals method teamgroup class:

@override public boolean equals(final object o) { homecoming this.r_id == ((teamgroup) o).r_id; }

...i'm not worried classcastexception here because pertaining above problem never seek compare teamgroup object teamgroup object--and not problem (at to the lowest degree not classcastexception problem).

the r_id's supposed unique , guarantee following:

private static final double width = (double) integer.max_value - (double) integer.min_value; private static final map<integer, integer> mapped_ids = new hashmap<integer, integer>(50000); ... public final int r_id = teamgroup.getnewid(); ... private static int getnewid() { int randid = randid(); while (mapped_ids.get(randid) != null) { randid = randid(); } mapped_ids.put(randid, randid); homecoming randid; } private static int randid() { homecoming (int) (integer.min_value + math.random() * width); }

the problem here:

homecoming this.r_id - o.r_id;

it should be:

homecoming integer.compare(this.r_id, o.r_id);

taking difference of 2 int or integer values works if values both guaranteed non-negative. however, in example, using id values across entire range of int / integer , means subtraction can lead overflow ... , wrong result compareto.

the wrong implementation leads situations compareto method not reflexive; i.e. integers i1, i2 , i3 compareto method says i1 < i2 , i2 < i3, i3 < i1. when plug treeset, elements inserted tree in wrong place, , unusual behaviours happen. exactly happening hard predict - depend on objects inserted, , order inserted.

treeset.first() should homecoming object belongs set, right?

probably ...

so why can not remove object?

probably because can't find ... because of broken compareto.

to understand going on, been single step through treeset code, etcetera.

java twos-complement

No comments:

Post a Comment