/* Copyright (C) 2007 Andre Seidelt, All Rights Reserved. This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. */ /* * display genome statistics. */ class GenomeDisplay { // indicates visibility private boolean mVisible = false; // start position x private int mX = 20; // start position y private int mY = 20; // width private int mWidth = WIDTH - 40; // height private int mHeight = HEIGHT - 70; // toggle visibility public void setVisible(boolean pVisible) { mVisible = pVisible; } // display stats public void paint() { // not visible -> bail out if(!mVisible) { return; } // create 'window' stroke(255, 255, 50, ALPHA_VALUE); fill(100, 100, 0, ALPHA_VALUE); rectMode(CORNER); rect(mX, mY, mWidth, mHeight); // collect all genomes and count their occurences Map genomes = new HashMap(); Iterator it = sCells.iterator(); while(it.hasNext()) { Cell c = (Cell) it.next(); String g = c.mGenome.toString(); Counter cnt = (Counter) genomes.get(g); if(cnt == null) { cnt = new Counter(g); } else { cnt.inc(); } genomes.put(g, cnt); } ArrayList al = new ArrayList(genomes.values()); Collections.sort(al); // display top30 int pos = 1; String txt="Top 30:\n\n"; it = al.iterator(); while(it.hasNext()) { Counter c = (Counter) it.next(); txt += nf(pos, 2) + " : " + c.mGenome + " = " + nf(c.mCount, 4) + "\n"; pos++; if(pos == 31) { break; } } stroke(40, 255, 55); fill(40, 255, 55); printText(txt, mX+10, mY+FONT_SIZE+12); } } // helper class to count genomes class Counter implements Comparable { // string rep of genome String mGenome; // number of occurences int mCount; // create for first occurence public Counter(String pGen) { mGenome = pGen; mCount = 1; } // inc counter public void inc() { mCount++; } // compare two counter public int compareTo(Object o) { return ((Counter) o).mCount - mCount; } }