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

📄 stringhash.java

📁 这是一个URL检索的算法
💻 JAVA
字号:
import java.io.*;
public class Stringhash{
public static void main(String []args)
	{	
		   String s=new String();int size=2097143;
		   int count=0;int hashcode;int hashnum=0;int stringnum=0;
		   Stringhash hash=new Stringhash();
		   //String sbuff[]=new String[size];
		   String  sbuff[]=new String[size];
		   long start=0;
		   
           
     	try		{
			File f=new File("D:\\download\\temp\\temp0012.txt");
			FileInputStream fint=new FileInputStream(f);
            BufferedInputStream bint=new BufferedInputStream(fint);
			DataInputStream  in=new DataInputStream (bint);
	   
            System.out.println("parent path"+f.getParent());
		    //long start=System.currentTimeMillis();
	        System.out.println("parent path: "+f.getName());
	        System.out.println("file  length: "+f.length());
	        start=System.currentTimeMillis();
		    while(((s=in.readLine())!=null) &&(count<=320000))
		   	  {
		    
		      hashcode=hash.Hfip(s,size);
		      
		      
		      
		     if (sbuff[hashcode]==null) { sbuff[hashcode]=s;}
		      else {
		      	      hashnum++;}
	   	    	    	  
	   	    
			  count++;
			}//while
				
			in.close();bint.close();fint.close();
		}//try
		catch(IOException ioe)		{System.out.println(ioe);}
		System.out.println("time had "+(System.currentTimeMillis()-start));
		System.out.println("count is "+count);
		System.out.println("stringnum is "+stringnum);
	    System.out.println("hashnum is "+hashnum);
	}




int Hfip(String url,int size)
	{
	  int n=0;
	  byte []b=new byte[4];
	  
		
		int m=n;
		for (int i=0;i<4;i++)
		{
			b[i]=(byte)((m>>8*(3-i))&0xFF);
					
		}
		
		for (int i=0;i<url.length();i++)
		{
			b[i%4]^=url.charAt(i);
					
		}
		
		n=b[0]<<24|b[1]<<16|b[2]<<8|b[3];
		
		
	 if (n>0)	  return n%size;
		  else 
		  {
		  	int tempa=n;long j=1;long a3=0;int t;
	       for(int k=0;k<32;k++,j=(long)Math.pow(2,k))
	        {t=tempa&0x01;
	         a3=a3+t*j;
	          tempa=tempa>>1;
	          
	          }
	          
	        
		  	return (int)(a3%size);
		
			
	        }
	        
	     }


	int ELFhash(String url,int size)
		{ 
		  int h=0;
		  int i=0;
		  while (i<url.length())
		  {
		  	h=(h<<4)+(int)url.charAt(i);
		  	i++;
		  	int g=h&0xF0000000;
		  	if (g!=0)
		  	h^=g>>24;
		  	h&=~g;
		  }
		  
		   if (h>0)	  return h%size;
		  else 
		  {
		  	int tempa=h;long j=1;long a3=0;int t;
	       for(int k=0;k<32;k++,j=(long)Math.pow(2,k))
	        {t=tempa&0x01;
	         a3=a3+t*j;
	          tempa=tempa>>1;
	          
	          }
	          
	          
		  	
		  	return (int)(a3%size);
		  	}
		  
      }

	int hf(String url,int size)
	{
		int result=0;
	    String ptr=new String(url);
	    int c;
	    for(int i=1;i<=ptr.length();i++)
	    {
	    	c=ptr.charAt(i-1);
	    	result +=c*3*i;
	    }
	    if (result<0)
	    result=-result;
	    return result%size;	
	    
	
	}


int Hashcode(String url,int size)
   { int h=0;
   	for (int i=0;i<url.length();i++)
     {  	h=31*h+url.charAt(i);     }
    
 if (h>0) 	  return h%size;
	else 	  {
	  	     int tempa=h;long j=1;long a3=0;int t;
	    for(int k=0;k<32;k++,j=(long)Math.pow(2,k))
	       {	t=tempa&0x01;  a3=a3+t*j; tempa=tempa>>1;  }
	     return (int)(a3%size);
          }
  }





}



class Node
{  
String s;
Node next;
public Node(){}

}





		
	

⌨️ 快捷键说明

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