1

I have several vectors of different elements but all extending a class which has a specific function, lets say for example

Vector<classone> one;
Vector<classtwo> two;
Vector<classthree> three;

and classone, classtwo and classthree extend Number, and number has two functions:

doThing()
getValue()

And what i want is to call doThing in the order of the getValues received from all the vectors.

One cheap solution would be to concatenate all the vectors in a single Vector, sort it by value and iterate to call the function, but that makes me have to create a huge new vector, occupying new ram, and since the doThing will happen 60 times a second, if the vectors become big, it might be an overkill, i dont really want to create a new vector just to sort it, is there any other solution using the already existing vectors?

Its Java btw.

Xkynar
  • 935
  • 1
  • 10
  • 31
  • Could those be inserted into one vector at the beginning? They have to be maintained split up in separate vectors? – Grzegorz Piwowarek Jul 07 '13 at 05:33
  • I am not sure what you are doing. You might have some thought that something is big on ram or there are many calculations; however, they might be small to your computer. Please provide us with more info of the problem/solution. – Multithreader Jul 07 '13 at 05:43

2 Answers2

1

Premature optimizations are generally a bad idea.

Try the method that came to mind first: creating a giant Vector1 ArrayList and sorting it. If it turns out to be a performance issue, then you can start trying new things.

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • To be honest there wasnt any performance drop, but since im trying to make it as perfect as I can i wanted to continue that direction instead of going for such a cheap solution on this "problem" =p Thank you for the tip – Xkynar Jul 07 '13 at 05:57
1

If one, two and three are sorted, you could create an custom iterator that checks for a given set of lists what the smallest value at the current position is and proceed there.

Should look similar to this (not tested):

class MultiListIterator {
  List<Number>[] lists;
  int[] positions;

  MultiListIterator(List<Number>... lists) {
    this.lists = lists;
    positions = new int[lists.length];
  }

  boolean hasNext() {
    for (int i = 0; i < lists.length; i++) {
      if (positions[i] < lists[i].length) return true;
    }
    return false;
  }

  Number next() {
    int bestIndex = -1;
    Number bestNumber = null;
    for (int i = 0; i < lists.length; i++) {
      var p = positions[i];
      if (p >= positions[i].length) continue;
      Number n = lists[i].get(p);
      if (bestNumber == null || n.getValue() < bestNumber.getValue()) {
        bestIndex = i;
        bestNumer = n;
      }
    }
    if (bestNumber == null) throw new RuntimeException("next() beyond hasNext()");
    positions[bestIndex++];
    return bestNumber;
  }
}

Usage:

MultiListIterator mli = new MultiListIterator(one, two, three);
while (mli.hasNext()) {
  mli.next().doThing();
}

You may want to let MultiListIterator implement Iterator<Number>.

Note that Java already has a built-in class Number. Using the same name for your class might lead to a lot of confusion when you forget to import it somewhere.

Stefan Haustein
  • 18,427
  • 3
  • 36
  • 51
  • It makes sense and since it is the actual answer to my question i accepted it, thank you =) – Xkynar Jul 07 '13 at 05:58
  • Those classnames were invented for the question, i dont have any class named Number thanks for the warning though =p – Xkynar Jul 07 '13 at 13:50