#pragma once

#include "core/types.h"

enum repHash_limit { repHashMax = 256 };		// repHash limit

class RepHash
{
public:
    u64 rep[ repHashMax ];
    u8 res[ repHashMax ];		// fifty counter reset flag (1=just captured/moved a pawn)
    u8 start[ repHashMax ];		// starting pointers
    u8 ptr;						// stack ptr
    u8 sptr;					// starting stack ptr

    RepHash()
    {
        clear();
    }

    // clear rep hash
    inline void clear()
    {
        if ( this )
        {
            sptr = ptr = 0;
        }
    }

    // returns current
    inline i32 get( const posHash &h ) const
    {
        i32 i;
        // the position below first resulted from a pawn move/capture
        i32 first = sptr > 0 ? start[ (i32)sptr - 1 ] : 0;
        for (i = (i32)ptr-3; i >= first; i -= 2 )
        {
            if ( rep[i] == h )
                break;
        }
        return i >= first ? 3 : 0;
    }
    inline void push( const posHash &h, bool reset )
    {
        if ( reset )
        {
            ASSERT( sptr < 255 );
            start[ sptr++ ] = ptr;
        }
        res[ ptr ] = reset ? 1 : 0;
        ASSERT( ptr < 255 );
        rep[ ptr++ ] = h;
    }
    inline void pop()
    {
        ASSERT( ptr > 0 );
        if ( res[ --ptr ] )
        {
            ASSERT( sptr > 0 );
            sptr--;
        }
    }
};
