@Override
  public Vec clone() {
    SparseVector copy = new SparseVector(length, Math.max(used, 10));

    System.arraycopy(this.values, 0, copy.values, 0, this.used);
    System.arraycopy(this.indexes, 0, copy.indexes, 0, this.used);
    copy.used = this.used;

    return copy;
  }
  @Override
  public Vec sortedCopy() {
    IndexTable it = new IndexTable(DoubleList.unmodifiableView(values, used));

    double[] newValues = new double[used];
    int[] newIndecies = new int[used];

    int lessThanZero = 0;
    for (int i = 0; i < used; i++) {
      int origIndex = it.index(i);
      newValues[i] = values[origIndex];
      if (newValues[i] < 0) lessThanZero++;
      newIndecies[i] = i;
    }
    // all < 0 values are right, now correct > 0 values
    for (int i = lessThanZero; i < used; i++)
      newIndecies[i] = length - (used - lessThanZero) + (i - lessThanZero);

    SparseVector sv = new SparseVector(length);
    sv.used = this.used;
    sv.values = newValues;
    sv.indexes = newIndecies;
    return sv;
  }