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 37 0.0 %
Date: 2023-01-19 00:18:29 Functions: 0 17 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2019 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           0 : template<typename T> class DeltaArray2D
      22             : {
      23             : public:
      24           0 :     virtual ~DeltaArray2D() {}
      25             : 
      26             :     T get(ssize_t x, ssize_t y);
      27             :     void set(ssize_t x, ssize_t y, const T& val);
      28             : 
      29             :     void OverlayWith(const DeltaArray2D<T>& overlayer);
      30             :     void Undo();
      31             :     void Redo();
      32             : 
      33             : protected:
      34             :     virtual T getOld(ssize_t x, ssize_t y) = 0;
      35             :     virtual void setNew(ssize_t x, ssize_t y, const T& val) = 0;
      36             : 
      37             : private:
      38             :     // TODO: more efficient representation
      39             :     using Data = std::unordered_map<std::pair<ssize_t, ssize_t>, std::pair<T, T> >; // map of <x,y> -> <old_val, new_val>
      40             :     Data m_Data;
      41             : };
      42             : 
      43             : namespace std
      44             : {
      45             : template<>
      46             : struct hash<std::pair<ssize_t, ssize_t> >
      47             : {
      48           0 :     std::size_t operator()(const std::pair<ssize_t, ssize_t>& p) const
      49             :     {
      50           0 :         std::size_t seed = 0;
      51           0 :         hash_combine(seed, p.first << 16);
      52           0 :         hash_combine(seed, p.second);
      53           0 :         return seed;
      54             :     }
      55             : 
      56             :     // Same as boost::hash_combine
      57           0 :     void hash_combine(std::size_t& seed, const ssize_t val) const
      58             :     {
      59           0 :         seed ^= m_SizeHash(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
      60           0 :     }
      61             : 
      62             : private:
      63             :     std::hash<ssize_t> m_SizeHash;
      64             : };
      65             : }
      66             : 
      67             : //////////////////////////////////////////////////////////////////////////
      68             : 
      69             : template<typename T>
      70           0 : T DeltaArray2D<T>::get(ssize_t x, ssize_t y)
      71             : {
      72           0 :     typename Data::iterator it = m_Data.find(std::make_pair(x, y));
      73           0 :     if (it == m_Data.end())
      74           0 :         return getOld(x, y);
      75             :     else
      76           0 :         return it->second.second;
      77             : }
      78             : 
      79             : template<typename T>
      80           0 : void DeltaArray2D<T>::set(ssize_t x, ssize_t y, const T& val)
      81             : {
      82           0 :     typename Data::iterator it = m_Data.find(std::make_pair(x, y));
      83           0 :     if (it == m_Data.end())
      84           0 :         m_Data.insert(std::make_pair(std::make_pair(x, y), std::make_pair(getOld(x, y), val)));
      85             :     else
      86           0 :         it->second.second = val;
      87           0 :     setNew(x, y, val);
      88           0 : }
      89             : 
      90             : template <typename T>
      91           0 : void DeltaArray2D<T>::OverlayWith(const DeltaArray2D<T>& overlayer)
      92             : {
      93           0 :     for (typename Data::const_iterator it = overlayer.m_Data.begin(); it != overlayer.m_Data.end(); ++it)
      94             :     {
      95           0 :         typename Data::iterator it2 = m_Data.find(it->first);
      96           0 :         if (it2 == m_Data.end())
      97           0 :             m_Data.insert(*it);
      98             :         else
      99             :         {
     100             :             //ENSURE(it2->second.second == it->second.first);
     101           0 :             it2->second.second = it->second.second;
     102             :         }
     103             :     }
     104             : 
     105           0 : }
     106             : 
     107             : template <typename T>
     108           0 : void DeltaArray2D<T>::Undo()
     109             : {
     110           0 :     for (typename Data::iterator it = m_Data.begin(); it != m_Data.end(); ++it)
     111           0 :         setNew(it->first.first, it->first.second, it->second.first);
     112           0 : }
     113             : 
     114             : template <typename T>
     115           0 : void DeltaArray2D<T>::Redo()
     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.second);
     119           0 : }
     120             : 
     121             : #endif // INCLUDED_DELTAARRAY

Generated by: LCOV version 1.13