/** * Implementation of {@link #build()} and {@link #finish()}. * * @param reuse Whether we should try to reuse the builder's storage for the packed list. If * {@code true}, the builder will be invalid after finishing and the packed list will use the * same arrays as the builder if they are full. * @return The packed ID list. */ private PackedScoredIdList finish(boolean reuse) { Preconditions.checkState(ids != null, "builder has been finished"); Map<Symbol, DoubleList> chans; Map<TypedSymbol<?>, List<?>> typedChans; if (size() > 0) { ImmutableMap.Builder<Symbol, DoubleList> cbld = ImmutableMap.builder(); for (ChannelStorage chan : channels.values()) { DoubleArrayList built; if (reuse) { built = chan.values; built.trim(); } else { built = new DoubleArrayList(chan.values); } cbld.put(chan.symbol, built); } chans = cbld.build(); ImmutableMap.Builder<TypedSymbol<?>, List<?>> tcbld = ImmutableMap.builder(); for (TypedChannelStorage<?> chan : typedChannels.values()) { List<?> built; if (reuse) { chan.values.trimToSize(); built = chan.values; } else { built = new ArrayList<Object>(chan.values); } tcbld.put(chan.symbol, built); } typedChans = tcbld.build(); } else { chans = Collections.emptyMap(); typedChans = Collections.emptyMap(); } LongList builtIds; DoubleList builtScores; if (reuse) { ids.trim(); builtIds = ids; scores.trim(); builtScores = scores; clear(); } else { builtIds = new CompactableLongArrayList(ids); builtScores = new DoubleArrayList(scores); } return new PackedScoredIdList(builtIds, builtScores, typedChans, chans); }
@Override public void seal() { ((DoubleArrayList) _innerList).trim(); _elements = ((DoubleArrayList) _innerList).elements(); int negativeIndexCheck = 1; // reverse negative elements, because string order and numeric orders are completely opposite if (_elements.length > negativeIndexCheck && _elements[negativeIndexCheck] < 0) { int endPosition = indexOfWithType((short) 0); if (endPosition < 0) { endPosition = -1 * endPosition - 1; } double tmp; for (int i = 0; i < (endPosition - negativeIndexCheck) / 2; i++) { tmp = _elements[i + negativeIndexCheck]; _elements[i + negativeIndexCheck] = _elements[endPosition - i - 1]; _elements[endPosition - i - 1] = tmp; } } }