//+------------------------------------------------------------------+
//|                                            DomChangeDetector.mqh |
//+------------------------------------------------------------------+
#ifndef DOM_CHANGE_DETECTOR_MQH
#define DOM_CHANGE_DETECTOR_MQH

//+------------------------------------------------------------------+
//| Tells whether a snapshot differs from the previous one           |
//| Entries are compared in order, so the same entries delivered in  |
//| a different order count as a change. This class never calls the  |
//| terminal.                                                        |
//+------------------------------------------------------------------+
class CDomChangeDetector
  {
private:
   MqlBookInfo       m_prev[];
   int               m_prev_count;
   bool              m_has_prev;
public:
                     CDomChangeDetector(void);
                    ~CDomChangeDetector(void);
   bool              Update(const MqlBookInfo &book[]);
   bool              Reset(void);
  };
//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CDomChangeDetector::CDomChangeDetector(void)
  {
   m_prev_count=0;
   m_has_prev=false;
  }
//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CDomChangeDetector::~CDomChangeDetector(void)
  {
  }
//+------------------------------------------------------------------+
//| Forget the stored snapshot; the next Update() reports a change   |
//+------------------------------------------------------------------+
bool CDomChangeDetector::Reset(void)
  {
//--- with no stored snapshot, the next Update() reports a change
   m_prev_count=0;
   m_has_prev=false;
   return(true);
  }
//+------------------------------------------------------------------+
//| Compare with the stored snapshot, then store the new one         |
//| The first call after construction or Reset() returns true        |
//+------------------------------------------------------------------+
bool CDomChangeDetector::Update(const MqlBookInfo &book[])
  {
   const int count=::ArraySize(book);
//--- the first snapshot, or a different number of entries, is always a change
   bool changed=true;
   if(m_has_prev && count==m_prev_count)
     {
      changed=false;
      for(int i=0; i<count; i++)
        {
         //--- exact comparison of every field, stop at the first difference
         if(book[i].type!=m_prev[i].type ||
            book[i].price!=m_prev[i].price ||
            book[i].volume!=m_prev[i].volume ||
            book[i].volume_real!=m_prev[i].volume_real)
           {
            changed=true;
            break;
           }
        }
     }
//--- keep a copy of the new snapshot for the next comparison
   if(changed)
     {
      //--- if the copy cannot be stored, forget the old one and still report the change
      if(::ArrayResize(m_prev,count)!=count)
        {
         m_prev_count=0;
         m_has_prev=false;
         return(true);
        }
      for(int i=0; i<count; i++)
         m_prev[i]=book[i];
      m_prev_count=count;
      m_has_prev=true;
     }
   return(changed);
  }

#endif
//+------------------------------------------------------------------+