//+------------------------------------------------------------------+
//| TicksForesterOpt.mqh v1.00                                       |
//| Benchmark adapter for the OPTIMIZED TickCompressorOpt.mqh.       |
//| Uses the class-renamed copy TickCompressorOpt.mqh (class         |
//| TickCompressorOpt) so v3.72 can live next to the original v3.72  |
//| TickCompressor.mqh and TickCompressorOpt.mqh in the same folder. |
//|   TICKS_FORESTER_OPT::Compress(ticks, out[], digits, blockSize)  |
//|   TICKS_FORESTER_OPT::Decompress(in[], ticks[], digits)          |
//|   TICKS_FORESTER_OPT::DecompressRange(in[], ticks[], from, to, digits)|
//| Mode: Bid/Ask/time (TickItems = 1, no ZIP). Decode goes straight |
//| into the caller's array via v3.72's native DeCompressStreamToArray|
//| (no per-tick callback, no staging buffer, no extra copy).        |
//| Normalization: exactly as shipped in v3.72 - turbo (LP * point)  |
//| mode by default. Define NormalizePricesStd or                    |
//| NormalizePricesFastAccum before including TickCompressorOpt.mqh  |
//| to switch modes.                                                 |
//+------------------------------------------------------------------+
#ifndef TICKS_FORESTER_OPT_MQH
#define TICKS_FORESTER_OPT_MQH

#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;
     }

   // Decodes all ticks. Returns tick count (also ArraySize(out)).
   static int Decompress(uchar &in[], MqlTick &out[], const int iDigits = -1)
     {
      const int sz = ArraySize(in);
      int digits = (iDigits >= 0) ? iDigits : _Digits;
      TickCompressorOpt tc;
      // the stream does not store digits (unlike v1.03), so pass the same digits used to compress
      tc.Start(1, MathPow(10.0, -digits), 0.01, digits);
      // pre-count ticks from the 16-byte block headers, size the output once
      int total = 0, p = 0;
      while(p + 16 <= sz)
        {
         total += tc.ArrToInt2(in, p + 4);
         p += 16 + tc.ArrToInt2(in, p);
        }
      ArrayResize(out, total);
      if(total <= 0) return 0;
      // decode every block straight into the output array
      int cnt = 0; p = 0;
      while(p + 16 <= sz)
        {
         if(tc.DeCompressStreamToArray(in, p, out, cnt) == 0) break;
        }
      ArrayResize(out, cnt);
      return cnt;
     }

   // from/to in seconds (0 = open). Whole blocks outside the range are skipped;
   // blocks on the range edge are decoded into a small temp buffer and filtered per tick
   // (v3.72 dropped the native from/to filter that v2.7 had inside DeCompressStream).
   static int DecompressRange(uchar &in[], MqlTick &out[], const datetime from, const datetime to, const int iDigits = -1)
     {
      const int sz = ArraySize(in);
      int digits = (iDigits >= 0) ? iDigits : _Digits;
      TickCompressorOpt tc;
      tc.Start(1, MathPow(10.0, -digits), 0.01, digits);
      const long from_ms = (long)from * 1000, to_ms = (long)to * 1000;
      // pass 1: upper-bound count (boundary blocks are counted whole)
      int total = 0, p = 0;
      while(p + 16 <= sz)
        {
         int bsz = tc.ArrToInt2(in, p), cnt = tc.ArrToInt2(in, p + 4);
         uint ft = (uint)tc.ArrToInt2(in, p + 8), lt = (uint)tc.ArrToInt2(in, p + 12);
         if(to > 0 && ft >= (uint)to) break;
         if(!(from > 0 && lt < (uint)from)) total += cnt;
         p += 16 + bsz;
        }
      if(total <= 0) { ArrayResize(out, 0); return 0; }
      ArrayResize(out, total);
      // pass 2: decode inside blocks directly, filter boundary blocks
      MqlTick tmp[];
      int w = 0; p = 0;
      while(p + 16 <= sz)
        {
         int bsz = tc.ArrToInt2(in, p), cnt = tc.ArrToInt2(in, p + 4);
         uint ft = (uint)tc.ArrToInt2(in, p + 8), lt = (uint)tc.ArrToInt2(in, p + 12);
         if(to > 0 && ft >= (uint)to) break;                       // block starts at/after range end
         if(from > 0 && lt < (uint)from) { p += 16 + bsz; continue; } // block fully before range
         bool inside = (from <= 0 || ft >= (uint)from) && (to <= 0 || lt < (uint)to);
         if(inside)
           {
            int oc = w;
            if(tc.DeCompressStreamToArray(in, p, out, oc) == 0) break;  // advances p to the next block
            w = oc;
           }
         else
           {
            ArrayResize(tmp, cnt);
            int tn = 0;
            if(tc.DeCompressStreamToArray(in, p, tmp, tn) == 0) break;  // advances p to the next block
            for(int k = 0; k < tn; k++)
              {
               if((from <= 0 || tmp[k].time_msc >= from_ms) && (to <= 0 || tmp[k].time_msc < to_ms))
                  out[w++] = tmp[k];
              }
           }
        }
      ArrayResize(out, w);
      return w;
     }
  };
#endif // TICKS_FORESTER_OPT_MQH
