Skip to main content
se añadieron 23 caracteres en el cuerpo
Origen Enlace
Trauma
  • 26.9k
  • 5
  • 42
  • 68
template<class K, class V>
class HashTabla {
private:
    std::function< size_t( const K &, int ) > hashear;

    static size_t haser_por_defecto( const K &k, int size ) {
        returnstatic Hashing() h;

        return h.dinamicHashing<K>dinamicHashing< K >( key, size_size ); 
    }

public:
    HashTabla( ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];        
        hashear = haser_por_defecto;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    HashTabla( std::function< size_t( const K &, int ) > &&haser ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        hashear = std::move( haser ); 

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    template< typename CLASS >
    HashTabla( CLASS *instance, size_t (CLASS::*method)( const K &, int ) ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        auto lambda = [instance, method]( const K &k, int size ) -> size_t {
            return (instance->*method)( k, size );
        };
        hashear = lambda;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }
};
template<class K, class V>
class HashTabla {
private:
    std::function< size_t( const K &, int ) > hashear;

    static size_t haser_por_defecto( const K &k, int size ) {
        return Hashing().dinamicHashing<K>(key, size_); 
    }

public:
    HashTabla( ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];        
        hashear = haser_por_defecto;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    HashTabla( std::function< size_t( const K &, int ) > &&haser ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        hashear = std::move( haser ); 

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    template< typename CLASS >
    HashTabla( CLASS *instance, size_t (CLASS::*method)( const K &, int ) ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        auto lambda = [instance, method]( const K &k, int size ) -> size_t {
            return (instance->*method)( k, size );
        };
        hashear = lambda;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }
};
template<class K, class V>
class HashTabla {
private:
    std::function< size_t( const K &, int ) > hashear;

    static size_t haser_por_defecto( const K &k, int size ) {
        static Hashing h;

        return h.dinamicHashing< K >( key, size );
    }

public:
    HashTabla( ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];        
        hashear = haser_por_defecto;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    HashTabla( std::function< size_t( const K &, int ) > &&haser ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        hashear = std::move( haser ); 

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    template< typename CLASS >
    HashTabla( CLASS *instance, size_t (CLASS::*method)( const K &, int ) ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        auto lambda = [instance, method]( const K &k, int size ) -> size_t {
            return (instance->*method)( k, size );
        };
        hashear = lambda;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }
};
Origen Enlace
Trauma
  • 26.9k
  • 5
  • 42
  • 68

Una posible solución pasa por aumentar el número de constructores de tu código:

template<class K, class V>
class HashTabla {
private:
    std::function< size_t( const K &, int ) > hashear;

    static size_t haser_por_defecto( const K &k, int size ) {
        return Hashing().dinamicHashing<K>(key, size_); 
    }

public:
    HashTabla( ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];        
        hashear = haser_por_defecto;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    HashTabla( std::function< size_t( const K &, int ) > &&haser ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        hashear = std::move( haser ); 

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }

    template< typename CLASS >
    HashTabla( CLASS *instance, size_t (CLASS::*method)( const K &, int ) ) {
        TABLE_SIZE = genradorPrimos.current();
        tabla = new HashEntidad<K, V>* [TABLE_SIZE];
        auto lambda = [instance, method]( const K &k, int size ) -> size_t {
            return (instance->*method)( k, size );
        };
        hashear = lambda;

        for (int i = 0; i < TABLE_SIZE; ++i)
            tabla[i] = nullptr;
    }
};

Cambiamos tu variable-miembro hashear, y creamos un constructor distinto dependiendo de como lo quieras usar:

  • Constructor por defecto, para usar la función de hash por defecto.
  • Usando una lambda, lo que te permite usar funciones estáticas y, en general, funciones-no-miembro.
  • Contructor template, que genera una lambda al-vuelo, y necesita un puntero a la instancia que quieras usar, junto a un puntero a su función miembro.

Este último caso se usaria así:

struct MiHash {
    size_t calcular( const int &, int );
};

MiHash mihash;
HashTabla< int, std::string > Hash( &mihash, &MiHash::calcular );

Y listo, ya se puede usar a placer.