OrderedIntDoubleMapping implements Serializable, Cloneable { public void merge(OrderedIntDoubleMapping updates) { int[] updateIndices = updates.getIndices(); double[] updateValues = updates.getValues(); int newNumMappings = numMappings + updates.getNumMappings(); int newCapacity = Math.max((int) (1.2 * newNumMappings), newNumMappings + 1); int[] newIndices = new int[newCapacity]; double[] newValues = new double[newCapacity]; int k = 0; int i = 0, j = 0; for (; i < numMappings && j < updates.getNumMappings(); ++k) { if (indices[i] < updateIndices[j]) { newIndices[k] = indices[i]; newValues[k] = values[i]; ++i; } else if (indices[i] > updateIndices[j]) { newIndices[k] = updateIndices[j]; newValues[k] = updateValues[j]; ++j; } else { newIndices[k] = updateIndices[j]; newValues[k] = updateValues[j]; ++i; ++j; } } for (; i < numMappings; ++i, ++k) { newIndices[k] = indices[i]; newValues[k] = values[i]; } for (; j < updates.getNumMappings(); ++j, ++k) { newIndices[k] = updateIndices[j]; newValues[k] = updateValues[j]; } indices = newIndices; values = newValues; numMappings = k; } OrderedIntDoubleMapping(boolean noDefault);   OrderedIntDoubleMapping();   OrderedIntDoubleMapping(int capacity);   OrderedIntDoubleMapping(int[] indices, double[] values, int numMappings); int[] getIndices(); int indexAt(int offset); void setIndexAt(int offset, int index); double[] getValues(); void setValueAt(int offset, double value); int getNumMappings(); double get(int index); void set(int index, double value); void merge(OrderedIntDoubleMapping updates); @Override int hashCode(); @Override boolean equals(Object o); @Override String toString(); @SuppressWarnings("CloneDoesntCallSuperClone") @Override OrderedIntDoubleMapping clone(); void increment(int index, double increment);  }
@Test public void testMerge() { OrderedIntDoubleMapping mappingOne = new OrderedIntDoubleMapping(false); mappingOne.set(0, 0); mappingOne.set(2, 2); mappingOne.set(4, 4); mappingOne.set(10, 10); OrderedIntDoubleMapping mappingTwo = new OrderedIntDoubleMapping(); mappingTwo.set(1, 1); mappingTwo.set(3, 3); mappingTwo.set(5, 5); mappingTwo.set(10, 20); mappingOne.merge(mappingTwo); assertEquals(7, mappingOne.getNumMappings()); for (int i = 0; i < 6; ++i) { assertEquals(i, mappingOne.get(i), i); } assertEquals(20, mappingOne.get(10), 0); }