Handle <CollectionTraceWithoutHeapIncrement> EnsureTime(ref OffsetLength offsetLength, int timeIndex) { var handle = increments.Dereference(offsetLength); for (int i = 0; i < handle.Length; i++) { // if we found the time, it is ensured and we can return if (handle.Array[handle.Offset + i].TimeIndex == timeIndex) { return(handle); } // if we found an empty slot, new it up and return if (handle.Array[handle.Offset + i].IsEmpty) { handle.Array[handle.Offset + i] = new CollectionTraceWithoutHeapIncrement(timeIndex); return(handle); } } // if we didn't find it, and no empty space for it var oldLength = handle.Length; handle = increments.EnsureAllocation(ref offsetLength, handle.Length + 1); handle.Array[handle.Offset + oldLength] = new CollectionTraceWithoutHeapIncrement(timeIndex); return(handle); }
void IntroduceAsUnsortedList(ref OffsetLength offsetLength, S element, Int64 weight) { if (offsetLength.IsEmpty) { var handle = records.EnsureAllocation(ref offsetLength, 1); handle.Array[handle.Offset] = new Weighted <S>(element, weight); } else { var handle = records.Dereference(offsetLength); if (handle.Length >= HashTableThresholdCount) { throw new Exception("???"); } for (int i = 0; i < handle.Length; i++) { if (handle.Array[handle.Offset + i].weight != 0 && handle.Array[handle.Offset + i].record.Equals(element)) { handle.Array[handle.Offset + i].weight += weight; if (handle.Array[handle.Offset + i].weight == 0) { for (int j = i + 1; j < handle.Length; j++) { handle.Array[handle.Offset + j - 1] = handle.Array[handle.Offset + j]; } handle.Array[handle.Offset + handle.Length - 1] = new Weighted <S>(); if (handle.Array[handle.Offset].weight == 0) { records.Release(ref offsetLength); } } return; } } // if the allocation is null, or if it is full, get some more allocation if (handle.Length == 0 || handle.Array[handle.Offset + handle.Length - 1].weight != 0) { handle = EnsureAllocation(ref offsetLength, handle.Length + 1); } // we may have crossed into hash territory... if (handle.Length >= HashTableThresholdCount) { IntroduceAsHashTable(ref offsetLength, element, weight); } else { for (int i = 0; i < handle.Length; i++) { if (handle.Array[handle.Offset + i].weight == 0) { handle.Array[handle.Offset + i] = new Weighted <S>(element, weight); return; } } } } }