Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ datastructures_la_SOURCES = \
src/binaryheap.c \
src/binaryheap.h \
src/datastructures.c \
src/hashfun.c \
src/hashtable-avl.c \
src/hashtable-avl.h

Expand Down
3 changes: 2 additions & 1 deletion gap/hash.gd
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ DeclareOperation( "DS_HTGrow", [ IsDS_HashTab, IsObject ] );

DeclareOperation( "ChooseHashFunction", [IsObject, IsInt] );

DeclareGlobalFunction( "ORB_HashFunctionReturn1" );
DeclareGlobalFunction( "ORB_HashFunctionForShortGF2Vectors" );
DeclareGlobalFunction( "ORB_HashFunctionForShort8BitVectors" );
DeclareGlobalFunction( "ORB_HashFunctionForGF2Vectors" );
Expand All @@ -63,8 +62,10 @@ DeclareGlobalFunction( "ORB_HashFunctionModWrapper" );
DeclareGlobalFunction( "ORB_HashFunctionForMatList" );
DeclareGlobalFunction( "ORB_HashFunctionForPlainFlatList" );
DeclareGlobalFunction( "ORB_HashFunctionForTransformations" );
DeclareGlobalFunction( "ORB_HashFunctionForPartialPerms" );
DeclareGlobalFunction( "MakeHashFunctionForPlainFlatList" );


##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down
46 changes: 43 additions & 3 deletions gap/hash.gi
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ InstallGlobalFunction( GrowDS_HT, function(ht,x)
ht.hfd := [ht.hfbig,ht.hfdbig,ht.len];
else
ht.hf := ChooseHashFunction(x,ht.len);
if ht.hf = fail then
Error("Could not find hash function for sample object");
return fail;
fi;
ht.hfd := ht.hf.data;
ht.hf := ht.hf.func;
fi;
Expand Down Expand Up @@ -535,6 +539,10 @@ InstallMethod( DS_HTGrow, "for a tree hash table and an object",
ht!.hfd := [ht!.hfbig,ht!.hfdbig,ht!.len];
else
ht!.hf := ChooseHashFunction(x,ht!.len);
if ht!.hf = fail then
Error("Could not find hash function for sample object");
return fail;
fi;
ht!.hfd := ht!.hf.data;
ht!.hf := ht!.hf.func;
fi;
Expand Down Expand Up @@ -623,10 +631,10 @@ function(v,data)
return HashKeyBag(v,101,3*GAPInfo.BytesPerVariable,data[2]) mod data[1] + 1;
end );

InstallMethod( ChooseHashFunction, "failure method if all fails",
InstallMethod( ChooseHashFunction, "failure method",
[IsObject,IsInt],
function(p,hashlen)
Error("Could not guess a suitable hash function");
return fail;
end );

# Now the choosing methods for compressed vectors:
Expand Down Expand Up @@ -788,7 +796,6 @@ InstallMethod( ChooseHashFunction, "for permutations",
return rec( func := ORB_HashFunctionForPermutations, data := hashlen );
end );


InstallMethod( ChooseHashFunction, "for transformations",
[IsTransformation, IsInt],
function(t,hashlen)
Expand Down Expand Up @@ -867,6 +874,39 @@ InstallMethod( ChooseHashFunction,
TryNextMethod();
end );

if IsBound(HASH_FUNC_FOR_PPERM) then
InstallGlobalFunction( ORB_HashFunctionForPartialPerms, HASH_FUNC_FOR_PPERM);
elif IsBound(IsPPerm2Rep) and IsBound(IsPPerm4Rep) then
InstallGlobalFunction( ORB_HashFunctionForPartialPerms,
function(t, data)
local codeg;
if IsPPerm4Rep(t) then
codeg:=CodegreeOfPartialPerm(t);
if codeg<65536 then
TrimPartialPerm(t);
else
return HashKeyBag(t,255,4,4*DegreeOfPartialPerm(t)) mod data + 1;
fi;
fi;
return HashKeyBag(t,255,2,2*DegreeOfPartialPerm(t)) mod data + 1;
end);
fi;

if IsBound(IsPartialPerm) then
InstallMethod( ChooseHashFunction, "for partial perms",
[IsPartialPerm, IsInt],
function(t,hashlen)
return rec( func := ORB_HashFunctionForPartialPerms, data := hashlen );
end );
fi;

InstallMethod(ChooseHashFunction, "for a blist and pos int",
[IsBlistRep, IsPosInt],
function(x, hashlen)
return rec(func := HASH_FUNC_FOR_BLIST,
data := hashlen);
end);

##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down
50 changes: 50 additions & 0 deletions gap/hashfun.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#############################################################################
##
## datastructures package
##
## Copyright 2016 by the package authors.
## Licensed under the GPL 2 or later.
##
#############################################################################


#########################################################################
# Infrastructure for choosing hash functions looking at example objects:
#########################################################################


#
# Approach #1: Provide hash functions as methods for an operation.
#
# FIXME: turn this into an attribute?
# The difference only matters for attribute storing objects. These
# are, however, the ones most likely to be NOT hashable. OTOH, they
# just might be after all, but the hash is expensive (think "IdGroup"
# for small finite groups), and so being able to conveniently cash it
# would be useful
#
DeclareOperation( "HashValue", [IsObject] );

#
# Approach #2: Precompute a hash function, given an "example object",
# then use that.
#
DeclareOperation( "ChooseHashFunction", [IsObject] );

DeclareGlobalFunction( "DATA_HashFunctionForShortGF2Vectors" );
DeclareGlobalFunction( "DATA_HashFunctionForShort8BitVectors" );
DeclareGlobalFunction( "DATA_HashFunctionForGF2Vectors" );
DeclareGlobalFunction( "DATA_HashFunctionFor8BitVectors" );
DeclareGlobalFunction( "DATA_HashFunctionForCompressedMats" );
DeclareGlobalFunction( "DATA_HashFunctionForIntegers" );
DeclareGlobalFunction( "DATA_HashFunctionForMemory" );
DeclareGlobalFunction( "DATA_HashFunctionForPermutations" );
DeclareGlobalFunction( "DATA_HashFunctionForIntList" );
DeclareGlobalFunction( "DATA_HashFunctionForNBitsPcWord" );
DeclareGlobalFunction( "DATA_HashFunctionForMatList" );
DeclareGlobalFunction( "DATA_HashFunctionForPlainFlatList" );
DeclareGlobalFunction( "DATA_HashFunctionForTransformations" );
DeclareGlobalFunction( "DATA_HashFunctionForPartialPerms" );


#DeclareGlobalFunction( "MakeHashFunctionForPlainFlatList" );
Loading