⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashcodesignedminimalperfecthash.java

📁 MG4J (Managing Gigabytes for Java) is a free full-text search engine for large document collections
💻 JAVA
字号:
package it.unimi.dsi.mg4j.util;/*		  * MG4J: Managing Gigabytes for Java * * Copyright (C) 2002-2007 Sebastiano Vigna  * *  This library is free software; you can redistribute it and/or modify it *  under the terms of the GNU Lesser General Public License as published by the Free *  Software Foundation; either version 2.1 of the License, or (at your option) *  any later version. * *  This library is distributed in the hope that it will be useful, but *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License *  for more details. * *  You should have received a copy of the GNU Lesser General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.Iterator;import java.util.zip.GZIPInputStream;import com.martiansoftware.jsap.JSAPException;/** {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash tables. * * <P>This class source exemplifies a signed minimal perfect hash table that * signes each term with the hash code that would be returned by  * {@link java.lang.String#hashCode()} if the word were a {@link String}, thus avoiding false positives with high * probability. *  *  * @author Sebastiano Vigna * @since 0.8 * @deprecated Use the new hashing stuff in Sux4J. */@Deprecatedpublic class HashCodeSignedMinimalPerfectHash extends SignedMinimalPerfectHash {	private static final long serialVersionUID = 1L;	/** The array of signatures. */    protected int[] signature;        /** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table 	 * for the given terms using the given number of weights.	 *	 * @param terms some terms to hash; it is assumed that no terms share a common prefix of	 * <code>weightLength</code> characters.	 * @param weightLength the number of weights used generating the	 * intermediate hash functions.	 * @see MinimalPerfectHash#MinimalPerfectHash(Iterable, int)	 */	public HashCodeSignedMinimalPerfectHash( final Iterable<? extends CharSequence> terms, final int weightLength ) {		super( terms, weightLength );	}	/** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table for the given	 * terms, using as many weights as the longest term in the collection.     *	 * @param terms some terms to hash; it is assumed that there are no duplicates.	 * @see MinimalPerfectHash#MinimalPerfectHash(Iterable)	 *	 	 */    public HashCodeSignedMinimalPerfectHash( final Iterable<? extends CharSequence> terms ) {		super( terms );    }	/** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table for the given file 	 * of terms using the given number of weights.	 *	 * @param termFile an UTF-8 file containing one term on each line; it is assumed that 	 * it does not contain terms with a common prefix of	 * <code>weightLength</code> characters.	 * @param encoding the encoding of <code>wordFile</code>; if <code>null</code>, it	 * is assumed to be the platform default encoding.	 * @param weightLength the number of weights used generating the	 * intermediate hash functions.	 * @see MinimalPerfectHash#MinimalPerfectHash(String, String, int)	 */	public HashCodeSignedMinimalPerfectHash( final String termFile, final String encoding, final int weightLength ) {		super( termFile, encoding, weightLength );	}	/** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table for the given file 	 * of terms.	 *	 * @param termFile an UTF-8 file containing one term on each line; it is assumed that 	 * it does not contain terms with a common prefix of	 * <code>weightLength</code> characters.	 * @param encoding the encoding of <code>wordFile</code>; if <code>null</code>, it	 * is assumed to be the platform default encoding.	 * @see MinimalPerfectHash#MinimalPerfectHash(String, String)	 */	public HashCodeSignedMinimalPerfectHash( final String termFile, final String encoding ) {		super( termFile, encoding );	}	/** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table for the (possibly <samp>gzip</samp>'d) given file 	 * of terms using the given number of weights.	 *	 * @param termFile an UTF-8 file containing one term on each line; it is assumed that 	 * it does not contain terms with a common prefix of	 * <code>weightLength</code> characters.	 * @param encoding the encoding of <code>wordFile</code>; if <code>null</code>, it	 * is assumed to be the platform default encoding.	 * @param weightLength the number of weights used generating the	 * intermediate hash functions.	 * @param zipped if true, the provided file is zipped and will be opened using a {@link GZIPInputStream}.	 * @see MinimalPerfectHash#MinimalPerfectHash(String, String, int)	 */	public HashCodeSignedMinimalPerfectHash( final String termFile, final String encoding, final int weightLength, boolean zipped ) {		super( termFile, encoding, weightLength, zipped );	}	/** Creates a new {@link java.lang.String#hashCode()}-signed order-preserving minimal perfect hash table for the (possibly <samp>gzip</samp>'d) given file 	 * of terms.	 *	 * @param termFile an UTF-8 file containing one term on each line; it is assumed that 	 * it does not contain terms with a common prefix of	 * <code>weightLength</code> characters.	 * @param encoding the encoding of <code>wordFile</code>; if <code>null</code>, it	 * is assumed to be the platform default encoding.	 * @param zipped if true, the provided file is zipped and will be opened using a {@link GZIPInputStream}.	 * @see MinimalPerfectHash#MinimalPerfectHash(String, String)	 */	public HashCodeSignedMinimalPerfectHash( final String termFile, final String encoding, boolean zipped ) {		super( termFile, encoding, zipped );	}	@Override	public void initSignatures( final Iterable<? extends CharSequence> terms ) {		CharSequence s;		int i, j, h, l;			signature = new int[ n ];			j = 0;		for ( Iterator<? extends CharSequence> e = terms.iterator(); e.hasNext(); j++ ) {			s = e.next();			l = s.length();			for ( i = h = 0; i < l; i++ ) h = 31 * h + s.charAt( i );			signature[ j ] = h;		}	}	@Override	public boolean checkSignature( final CharSequence word, final int index ) {		if ( n == 0 ) return false; // Empty maps contain no word		int i, h, l;		l = word.length();		for ( i = h = 0; i < l; i++ ) h = 31 * h + word.charAt( i );		return signature[ index ] == h;	}	@Override	public boolean checkSignature( final byte[] a, int off, int len, final int index ) {		if ( n == 0 ) return false; // Empty maps contain no word		int i, h;		for ( i = h = 0; i < len; i++ ) h = 31 * h + ( a[ off + i ] & 0xFF );		return signature[ index ] == h;	}    public static void main( final String[] arg ) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException, JSAPException, ClassNotFoundException {    	MinimalPerfectHash.main( HashCodeSignedMinimalPerfectHash.class, arg );    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -