/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.intellij.vcs.log.graph.utils.impl;

import com.intellij.vcs.log.graph.utils.IntList;
import com.intellij.vcs.log.graph.utils.TimestampGetter;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public class IntTimestampGetter implements TimestampGetter {

  public static final int DEFAULT_BLOCK_SIZE = 30;

  private static final long MAX_DELTA = Integer.MAX_VALUE - 10;
  private static final int BROKEN_DELTA = Integer.MAX_VALUE;

  @NotNull
  public static IntTimestampGetter newInstance(@NotNull TimestampGetter delegateGetter) {
    return newInstance(delegateGetter, DEFAULT_BLOCK_SIZE);
  }

  @NotNull
  public static IntTimestampGetter newInstance(@NotNull TimestampGetter delegateGetter, int blockSize) {
    if (delegateGetter.size() < 0)
      throw new NegativeArraySizeException("delegateGetter.size() < 0: " + delegateGetter.size());
    if (delegateGetter.size() == 0)
      throw new IllegalArgumentException("Empty TimestampGetter not supported");

    long[] saveTimestamps = new long[(delegateGetter.size() - 1) / blockSize + 1];
    for (int i = 0; i < saveTimestamps.length; i++)
      saveTimestamps[i] = delegateGetter.getTimestamp(blockSize * i);

    Map<Integer, Long> brokenDeltas = new HashMap<Integer, Long>();
    int[] deltas = new int[delegateGetter.size() - 1];

    for (int i = 0; i < delegateGetter.size() - 1; i++) {
      long delta = delegateGetter.getTimestamp(i + 1) - delegateGetter.getTimestamp(i);
      int intDelta = deltaToInt(delta);
      deltas[i] = intDelta;
      if (intDelta == BROKEN_DELTA)
        brokenDeltas.put(i, delta);
    }

    return new IntTimestampGetter(deltas, blockSize, saveTimestamps, brokenDeltas);
  }

  private static int deltaToInt(long delta) {
    if (delta >= 0 && delta <= MAX_DELTA)
      return (int)delta;

    if (delta < 0 && -delta <= MAX_DELTA)
      return (int)delta;

    return BROKEN_DELTA;
  }

  // myDeltas[i] = getTimestamp(i + 1) - getTimestamp(i)
  private final IntList myDeltas;

  @NotNull
  private final Map<Integer, Long> myBrokenDeltas;

  private final int myBlockSize;

  // saved 0, blockSize, 2 * blockSize, etc.
  private final long[] mySaveTimestamps;

  public IntTimestampGetter(int[] deltas, int blockSize, long[] saveTimestamps, @NotNull Map<Integer, Long> brokenDeltas) {
    myDeltas = CompressedIntList.newInstance(deltas);
    myBlockSize = blockSize;
    mySaveTimestamps = saveTimestamps;
    myBrokenDeltas = brokenDeltas;
  }

  @Override
  public int size() {
    return myDeltas.size() + 1;
  }

  @Override
  public long getTimestamp(int index) {
    checkRange(index);
    int relativeSaveIndex = index / myBlockSize;
    long timestamp = mySaveTimestamps[relativeSaveIndex];
    for (int i = myBlockSize * relativeSaveIndex; i < index; i++) {
      timestamp += getDelta(i);
    }

    return timestamp;
  }

  private long getDelta(int index) {
    int delta = myDeltas.get(index);
    if (delta != BROKEN_DELTA)
      return delta;

    return myBrokenDeltas.get(index);
  }

  private void checkRange(int index) {
    if (index < 0)
      throw new IndexOutOfBoundsException("index < 0:" + index);
    if (index >= size())
      throw new IndexOutOfBoundsException("index: " + index + " >= size: " + size());
  }
}

@@ -1937,20 +1937,16 @@
 r.size()
- - 1
 %5D;%0A%0A    
@@ -1986,20 +1986,16 @@
 r.size()
- - 1
 ; i++) %7B
@@ -1991,24 +1991,68 @@
 e(); i++) %7B%0A
+      int blockIndex = i - (i %25 blockSize);%0A
       long d
@@ -2083,28 +2083,24 @@
 tTimestamp(i
- + 1
 ) - delegate
@@ -2119,17 +2119,26 @@
 mestamp(
-i
+blockIndex
 );%0A     
@@ -2884,16 +2884,22 @@
 pGetter(
+final 
 int%5B%5D de
@@ -3005,45 +3005,64 @@
 s = 
-CompressedIntList.newInstance
+SmartDeltaCompressor.newInstance(new FullIntList
 (deltas)
 ;%0A  
@@ -3053,24 +3053,25 @@
 List(deltas)
+)
 ;%0A    myBloc
@@ -3228,20 +3228,16 @@
 s.size()
- + 1
 ;%0A  %7D%0A%0A 
@@ -3425,77 +3425,14 @@
     
-for (int i = myBlockSize * relativeSaveIndex; i %3C index; i++) %7B%0A     
+return
  tim
@@ -3439,17 +3439,16 @@
 estamp +
-=
  getDelt
@@ -3454,38 +3454,13 @@
 ta(i
-);%0A    %7D%0A%0A    return timestamp
+ndex)
 ;%0A  

Optimize IntTimestampGetter.