Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. public class AL6_3 {
  7.  
  8.     private Surname[] surnames;
  9.     private Surname[] table;
  10.     private final Surname del = new Surname(0,"DEL");
  11.  
  12.     public AL6_3(String path) throws IOException {
  13.         this.surnames = readFile(path);
  14.     }
  15.  
  16.     public Surname[] getSurnames() {
  17.         return surnames;
  18.     }
  19.  
  20.     public Surname[] readFile(String path) throws IOException {
  21.         FileInputStream fileInputStream = new FileInputStream(path);
  22.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
  23.         Surname[] result;
  24.         String currentLine;
  25.         int arraySize = 0;
  26.         while((currentLine = bufferedReader.readLine()) != null){
  27.             arraySize++;
  28.         }
  29.         result = new Surname[arraySize];
  30.         fileInputStream.getChannel().position(0);
  31.         bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
  32.         for(int i=0; i<arraySize; i++){
  33.             currentLine = bufferedReader.readLine();
  34.             Surname item = new Surname(Integer.parseInt(currentLine.split(" ")[0]),currentLine.split(" ")[1]);
  35.             result[i] = item;
  36.         }
  37.         return result;
  38.     }
  39.  
  40.     public void hashInsert(Surname surname){
  41.         int i = 0;
  42.         int j;
  43.         do {
  44.             j=hash(Utilities.wordToLong(surnames[i].getSurname(),111), table.length,i);
  45.             if(table[j]==null || table[j].getSurname().equals("DEL")){
  46.                 table[j] = surname;
  47.                 return;
  48.             }
  49.             i++;
  50.         }while(i< table.length);
  51.         System.out.println("Błąd - brak miejsca w tablicy");
  52.     }
  53.  
  54.     //adresowanie otwarte liniowe
  55.     //key - wyraz przekonwertowany na longa
  56.     //attempt - kolejna próba haszowania, w przypadku gdy poprzednia dała zajęty indeks w tablicy
  57.     public int hash(long key, int tabLength, int attempt){
  58.         long result = (key % tabLength +attempt)%tabLength;
  59.         return (int)result;
  60.     }
  61.  
  62.  
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement