//+------------------------------------------------------------------+
//| TicksForesterOpt.mqh v1.00                                       |
//| Benchmark adapter for the OPTIMIZED TickCompressorOpt.mqh.       |
//|   TICKS_FORESTER_OPT::Compress(ticks, out[], digits, blockSize)  |
//|   TICKS_FORESTER_OPT::Decompress(in[], ticks[], digits)          |
//|   TICKS_FORESTER_OPT::DecompressRange(in[], ticks[], from, to, digits)|
//| Decode goes straight into the caller's array (DeCompressToArray).|
//| Can be included together with TicksForester.mqh: class, version  |
//| and helper macros in TickCompressorOpt.mqh are renamed.          |
//| Needs TickCompressorOpt.mqh. It includes TicksForester.mqh first |
//| because that file owns the single OnTickDecompressed callback    |
//| (so TickCompressor.mqh must also be in the folder).              |
//+------------------------------------------------------------------+
#ifndef TICKS_FORESTER_OPT_MQH
#define TICKS_FORESTER_OPT_MQH

#include "TicksForester.mqh"     // defines NormalizeMqlTick, TickCompressor and the callback
#include "TickCompressorOpt.mqh"

class TICKS_FORESTER_OPT
  {
public:
   static int Compress(MqlTick &ticks[], uchar &out[], const int iDigits = -1, const int blockSize = 512)
     {
      const int n = ArraySize(ticks);
      ArrayResize(out, 0);
      if(n <= 0) return 0;
      int digits = (iDigits >= 0) ? iDigits : _Digits;
      int bs = (blockSize < 2) ? 512 : blockSize;
      TickCompressorOpt tc;
      tc.Start(1, MathPow(10.0, -digits), 0.01, digits);
      int pos = 0;
      for(int i = 0; i < n; i += bs)
        {
         int cnt = MathMin(bs, n - i);
         pos = tc.Compress(ticks, out, i, cnt, pos);
        }
      ArrayResize(out, pos);
      return pos;
     }

   static int Decompress(uchar &in[], MqlTick &out[], const int iDigits = -1)
     {
      return DecompressRange(in, out, 0, 0, iDigits);
     }

   // from/to in seconds (0 = open). Whole blocks outside the range are skipped.
   static int DecompressRange(uchar &in[], MqlTick &out[], const datetime from, const datetime to, const int iDigits = -1)
     {
      int digits = (iDigits >= 0) ? iDigits : _Digits;
      TickCompressorOpt tc;
      tc.Start(1, MathPow(10.0, -digits), 0.01, digits);
      return tc.DeCompressToArray(in, ArraySize(in), out, from, to);
     }
  };
#endif
