LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface - DeltaArray.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 36 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 8 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_DELTAARRAY
      19             : #define INCLUDED_DELTAARRAY
      20             : 
      21             : #include "lib/posix/posix_types.h"
      22             : 
      23             : #include <cstddef>
      24             : #include <functional>
      25             : #include <unordered_map>
      26             : #include <utility>
      27             : 
      28           0 : template<typename T> class DeltaArray2D
      29             : {
      30             : public:
      31           0 :     virtual ~DeltaArray2D() {}
      32             : 
      33             :     T get(ssize_t x, ssize_t y);
      34             :     void set(ssize_t x, ssize_t y, const T& val);
      35             : 
      36             :     void OverlayWith(const DeltaArray2D<T>& overlayer);
      37             :     void Undo();
      38             :     void Redo();
      39             : 
      40             : protected:
      41             :     virtual T getOld(ssize_t x, ssize_t y) = 0;
      42             :     virtual void setNew(ssize_t x, ssize_t y, const T& val) = 0;
      43             : 
      44             : private:
      45             :     // TODO: more efficient representation
      46             :     using Data = std::unordered_map<std::pair<ssize_t, ssize_t>, std::pair<T, T> >; // map of <x,y> -> <old_val, new_val>
      47             :     Data m_Data;
      48             : };
      49             : 
      50             : namespace std
      51             : {
      52             : template<>
      53             : struct hash<std::pair<ssize_t, ssize_t> >
      54             : {
      55           0 :     std::size_t operator()(const std::pair<ssize_t, ssize_t>& p) const
      56             :     {
      57           0 :         std::size_t seed = 0;
      58           0 :         hash_combine(seed, p.first << 16);
      59           0 :         hash_combine(seed, p.second);
      60           0 :         return seed;
      61             :     }
      62             : 
      63             :     // Same as boost::hash_combine
      64           0 :     void hash_combine(std::size_t& seed, const ssize_t val) const
      65             :     {
      66           0 :         seed ^= m_SizeHash(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
      67             :     }
      68             : 
      69             : private:
      70             :     std::hash<ssize_t> m_SizeHash;
      71             : };
      72             : }
      73             : 
      74             : //////////////////////////////////////////////////////////////////////////
      75             : 
      76             : template<typename T>
      77           0 : T DeltaArray2D<T>::get(ssize_t x, ssize_t y)
      78             : {
      79           0 :     typename Data::iterator it = m_Data.find(std::make_pair(x, y));
      80           0 :     if (it == m_Data.end())
      81           0 :         return getOld(x, y);
      82             :     else
      83           0 :         return it->second.second;
      84             : }
      85             : 
      86             : template<typename T>
      87           0 : void DeltaArray2D<T>::set(ssize_t x, ssize_t y, const T& val)
      88             : {
      89           0 :     typename Data::iterator it = m_Data.find(std::make_pair(x, y));
      90           0 :     if (it == m_Data.end())
      91           0 :         m_Data.insert(std::make_pair(std::make_pair(x, y), std::make_pair(getOld(x, y), val)));
      92             :     else
      93           0 :         it->second.second = val;
      94           0 :     setNew(x, y, val);
      95           0 : }
      96             : 
      97             : template <typename T>
      98           0 : void DeltaArray2D<T>::OverlayWith(const DeltaArray2D<T>& overlayer)
      99             : {
     100           0 :     for (typename Data::const_iterator it = overlayer.m_Data.begin(); it != overlayer.m_Data.end(); ++it)
     101             :     {
     102           0 :         typename Data::iterator it2 = m_Data.find(it->first);
     103           0 :         if (it2 == m_Data.end())
     104           0 :             m_Data.insert(*it);
     105             :         else
     106             :         {
     107             :             //ENSURE(it2->second.second == it->second.first);
     108           0 :             it2->second.second = it->second.second;
     109             :         }
     110             :     }
     111             : 
     112           0 : }
     113             : 
     114             : template <typename T>
     115           0 : void DeltaArray2D<T>::Undo()
     116             : {
     117           0 :     for (typename Data::iterator it = m_Data.begin(); it != m_Data.end(); ++it)
     118           0 :         setNew(it->first.first, it->first.second, it->second.first);
     119           0 : }
     120             : 
     121             : template <typename T>
     122           0 : void DeltaArray2D<T>::Redo()
     123             : {
     124           0 :     for (typename Data::iterator it = m_Data.begin(); it != m_Data.end(); ++it)
     125           0 :         setNew(it->first.first, it->first.second, it->second.second);
     126           0 : }
     127             : 
     128             : #endif // INCLUDED_DELTAARRAY

Generated by: LCOV version 1.16