@Test public void placeAds_inOrder_shouldUpdatePositions() { checkInsertionPositions(10, adsAt15repeating, 1, 4, 6, 8, 10); checkPlacedPositions(20, adsAt15repeating); adsAt15repeating.placeAd(1, mockNativeAdData); checkInsertionPositions(10, adsAt15repeating, 5, 7, 9); checkPlacedPositions(20, adsAt15repeating, 1); adsAt15repeating.placeAd(5, mockNativeAdData); checkInsertionPositions(10, adsAt15repeating, 8, 10); checkPlacedPositions(20, adsAt15repeating, 1, 5); adsAt15repeating.placeAd(8, mockNativeAdData); checkInsertionPositions(10, adsAt15repeating); checkPlacedPositions(20, adsAt15repeating, 1, 5, 8); }
void placeAd(final int adjustedPosition, final NativeAdData adData) { final int desiredIndex = binarySearchFirstEquals( mDesiredInsertionPositions, mDesiredCount, adjustedPosition); if (desiredIndex == mDesiredCount || mDesiredInsertionPositions[desiredIndex] != adjustedPosition) { MoPubLog.w("Attempted to insert an ad at an invalid position"); return; } final int originalPosition = mDesiredOriginalPositions[desiredIndex]; int placeIndex = binarySearchGreaterThan( mOriginalAdPositions, mPlacedCount, originalPosition); if (placeIndex < mPlacedCount) { final int num = mPlacedCount - placeIndex; System.arraycopy(mOriginalAdPositions, placeIndex, mOriginalAdPositions, placeIndex + 1, num); System.arraycopy(mAdjustedAdPositions, placeIndex, mAdjustedAdPositions, placeIndex + 1, num); System.arraycopy(mAdDataObjects, placeIndex, mAdDataObjects, placeIndex + 1, num); } mOriginalAdPositions[placeIndex] = originalPosition; mAdjustedAdPositions[placeIndex] = adjustedPosition; mAdDataObjects[placeIndex] = adData; mPlacedCount++; final int num = mDesiredCount - desiredIndex - 1; System.arraycopy(mDesiredInsertionPositions, desiredIndex + 1, mDesiredInsertionPositions, desiredIndex, num); System.arraycopy(mDesiredOriginalPositions, desiredIndex + 1, mDesiredOriginalPositions, desiredIndex, num); mDesiredCount--; for (int i = desiredIndex; i < mDesiredCount; ++i) { mDesiredInsertionPositions[i]++; } for (int i = placeIndex + 1; i < mPlacedCount; ++i) { mAdjustedAdPositions[i]++; } }
PlacementData { void placeAd(final int adjustedPosition, final NativeAdData adData) { final int desiredIndex = binarySearchFirstEquals( mDesiredInsertionPositions, mDesiredCount, adjustedPosition); if (desiredIndex == mDesiredCount || mDesiredInsertionPositions[desiredIndex] != adjustedPosition) { MoPubLog.w("Attempted to insert an ad at an invalid position"); return; } final int originalPosition = mDesiredOriginalPositions[desiredIndex]; int placeIndex = binarySearchGreaterThan( mOriginalAdPositions, mPlacedCount, originalPosition); if (placeIndex < mPlacedCount) { final int num = mPlacedCount - placeIndex; System.arraycopy(mOriginalAdPositions, placeIndex, mOriginalAdPositions, placeIndex + 1, num); System.arraycopy(mAdjustedAdPositions, placeIndex, mAdjustedAdPositions, placeIndex + 1, num); System.arraycopy(mAdDataObjects, placeIndex, mAdDataObjects, placeIndex + 1, num); } mOriginalAdPositions[placeIndex] = originalPosition; mAdjustedAdPositions[placeIndex] = adjustedPosition; mAdDataObjects[placeIndex] = adData; mPlacedCount++; final int num = mDesiredCount - desiredIndex - 1; System.arraycopy(mDesiredInsertionPositions, desiredIndex + 1, mDesiredInsertionPositions, desiredIndex, num); System.arraycopy(mDesiredOriginalPositions, desiredIndex + 1, mDesiredOriginalPositions, desiredIndex, num); mDesiredCount--; for (int i = desiredIndex; i < mDesiredCount; ++i) { mDesiredInsertionPositions[i]++; } for (int i = placeIndex + 1; i < mPlacedCount; ++i) { mAdjustedAdPositions[i]++; } } }
PlacementData { void placeAd(final int adjustedPosition, final NativeAdData adData) { final int desiredIndex = binarySearchFirstEquals( mDesiredInsertionPositions, mDesiredCount, adjustedPosition); if (desiredIndex == mDesiredCount || mDesiredInsertionPositions[desiredIndex] != adjustedPosition) { MoPubLog.w("Attempted to insert an ad at an invalid position"); return; } final int originalPosition = mDesiredOriginalPositions[desiredIndex]; int placeIndex = binarySearchGreaterThan( mOriginalAdPositions, mPlacedCount, originalPosition); if (placeIndex < mPlacedCount) { final int num = mPlacedCount - placeIndex; System.arraycopy(mOriginalAdPositions, placeIndex, mOriginalAdPositions, placeIndex + 1, num); System.arraycopy(mAdjustedAdPositions, placeIndex, mAdjustedAdPositions, placeIndex + 1, num); System.arraycopy(mAdDataObjects, placeIndex, mAdDataObjects, placeIndex + 1, num); } mOriginalAdPositions[placeIndex] = originalPosition; mAdjustedAdPositions[placeIndex] = adjustedPosition; mAdDataObjects[placeIndex] = adData; mPlacedCount++; final int num = mDesiredCount - desiredIndex - 1; System.arraycopy(mDesiredInsertionPositions, desiredIndex + 1, mDesiredInsertionPositions, desiredIndex, num); System.arraycopy(mDesiredOriginalPositions, desiredIndex + 1, mDesiredOriginalPositions, desiredIndex, num); mDesiredCount--; for (int i = desiredIndex; i < mDesiredCount; ++i) { mDesiredInsertionPositions[i]++; } for (int i = placeIndex + 1; i < mPlacedCount; ++i) { mAdjustedAdPositions[i]++; } } private  PlacementData(@NonNull final int[] desiredInsertionPositions); }
PlacementData { void placeAd(final int adjustedPosition, final NativeAdData adData) { final int desiredIndex = binarySearchFirstEquals( mDesiredInsertionPositions, mDesiredCount, adjustedPosition); if (desiredIndex == mDesiredCount || mDesiredInsertionPositions[desiredIndex] != adjustedPosition) { MoPubLog.w("Attempted to insert an ad at an invalid position"); return; } final int originalPosition = mDesiredOriginalPositions[desiredIndex]; int placeIndex = binarySearchGreaterThan( mOriginalAdPositions, mPlacedCount, originalPosition); if (placeIndex < mPlacedCount) { final int num = mPlacedCount - placeIndex; System.arraycopy(mOriginalAdPositions, placeIndex, mOriginalAdPositions, placeIndex + 1, num); System.arraycopy(mAdjustedAdPositions, placeIndex, mAdjustedAdPositions, placeIndex + 1, num); System.arraycopy(mAdDataObjects, placeIndex, mAdDataObjects, placeIndex + 1, num); } mOriginalAdPositions[placeIndex] = originalPosition; mAdjustedAdPositions[placeIndex] = adjustedPosition; mAdDataObjects[placeIndex] = adData; mPlacedCount++; final int num = mDesiredCount - desiredIndex - 1; System.arraycopy(mDesiredInsertionPositions, desiredIndex + 1, mDesiredInsertionPositions, desiredIndex, num); System.arraycopy(mDesiredOriginalPositions, desiredIndex + 1, mDesiredOriginalPositions, desiredIndex, num); mDesiredCount--; for (int i = desiredIndex; i < mDesiredCount; ++i) { mDesiredInsertionPositions[i]++; } for (int i = placeIndex + 1; i < mPlacedCount; ++i) { mAdjustedAdPositions[i]++; } } private  PlacementData(@NonNull final int[] desiredInsertionPositions);  }
PlacementData { void placeAd(final int adjustedPosition, final NativeAdData adData) { final int desiredIndex = binarySearchFirstEquals( mDesiredInsertionPositions, mDesiredCount, adjustedPosition); if (desiredIndex == mDesiredCount || mDesiredInsertionPositions[desiredIndex] != adjustedPosition) { MoPubLog.w("Attempted to insert an ad at an invalid position"); return; } final int originalPosition = mDesiredOriginalPositions[desiredIndex]; int placeIndex = binarySearchGreaterThan( mOriginalAdPositions, mPlacedCount, originalPosition); if (placeIndex < mPlacedCount) { final int num = mPlacedCount - placeIndex; System.arraycopy(mOriginalAdPositions, placeIndex, mOriginalAdPositions, placeIndex + 1, num); System.arraycopy(mAdjustedAdPositions, placeIndex, mAdjustedAdPositions, placeIndex + 1, num); System.arraycopy(mAdDataObjects, placeIndex, mAdDataObjects, placeIndex + 1, num); } mOriginalAdPositions[placeIndex] = originalPosition; mAdjustedAdPositions[placeIndex] = adjustedPosition; mAdDataObjects[placeIndex] = adData; mPlacedCount++; final int num = mDesiredCount - desiredIndex - 1; System.arraycopy(mDesiredInsertionPositions, desiredIndex + 1, mDesiredInsertionPositions, desiredIndex, num); System.arraycopy(mDesiredOriginalPositions, desiredIndex + 1, mDesiredOriginalPositions, desiredIndex, num); mDesiredCount--; for (int i = desiredIndex; i < mDesiredCount; ++i) { mDesiredInsertionPositions[i]++; } for (int i = placeIndex + 1; i < mPlacedCount; ++i) { mAdjustedAdPositions[i]++; } } private  PlacementData(@NonNull final int[] desiredInsertionPositions);  final static int NOT_FOUND; }