// Maintains a list of ints. If you attempt to add() an int which has a // lower priority than any of the particles in the list, it will not be added; // otherwise, it will be added, and one of the existing items dropped. // Priorities must be positive. class PriorityList { int count; int[] items; float[] priorities; PriorityList(int listLength) { count = listLength; items = new int[count]; priorities = new float[count]; for (int i = 0; i < count; i++) { items[i] = 0; priorities[i] = -1; } } void add(int item, float priority) { int index = 0; boolean addIt = false; for (int i = 0; i < count; i++) { // Find the item with the lowest priority in the list if (priorities[i] < 0.0) { index = i; addIt = true; break; } if (priority < priorities[i]) { index = i; addIt = true; } } if (addIt) { items[index] = item; priorities[index] = priority; } } }