From f1e6f189396a203a6846b1a9f85f5f811b2ef24a Mon Sep 17 00:00:00 2001 From: Giovanni Di Grezia Date: Sat, 17 May 2014 23:17:11 +0200 Subject: [PATCH] Fixed bugs to Dictionary Classes --- com/xgiovio/ChainingHashTableTest.java | 2 +- com/xgiovio/LinearProbingHashTableTest.java | 6 +- dictionary/ChainingHashTable.java | 18 +++- dictionary/LinearProbingHashTable.java | 94 +++++++++++++-------- dictionary/Logfile.java | 12 +++ set/OrderedListSet.java | 3 +- 6 files changed, 96 insertions(+), 39 deletions(-) diff --git a/com/xgiovio/ChainingHashTableTest.java b/com/xgiovio/ChainingHashTableTest.java index 87bf30d..66d70cb 100644 --- a/com/xgiovio/ChainingHashTableTest.java +++ b/com/xgiovio/ChainingHashTableTest.java @@ -17,7 +17,7 @@ public class ChainingHashTableTest { public static void main(String[] args) throws InvalidKeyException{ - ChainingHashTable h = new ChainingHashTable(); + ChainingHashTable h = new ChainingHashTable(1); System.out.println(h.load_factor()); diff --git a/com/xgiovio/LinearProbingHashTableTest.java b/com/xgiovio/LinearProbingHashTableTest.java index 60bf02d..124e6b3 100644 --- a/com/xgiovio/LinearProbingHashTableTest.java +++ b/com/xgiovio/LinearProbingHashTableTest.java @@ -17,7 +17,8 @@ public class LinearProbingHashTableTest { public static void main(String[] args) throws InvalidKeyException{ - LinearProbingHashTable h = new LinearProbingHashTable(); + LinearProbingHashTable h = new LinearProbingHashTable(1); + System.out.println(h.load_factor()); @@ -89,12 +90,15 @@ public class LinearProbingHashTableTest { Entry f = h.insert("7", 100); + System.out.println(h); + h.remove(f); System.out.println(h); + } } diff --git a/dictionary/ChainingHashTable.java b/dictionary/ChainingHashTable.java index 643ce62..53ad9f2 100644 --- a/dictionary/ChainingHashTable.java +++ b/dictionary/ChainingHashTable.java @@ -122,6 +122,22 @@ public class ChainingHashTable implements Dictionary { } } + protected Entry insert_refactor(Entry in_e) throws InvalidKeyException { + // in_e not checked because used internally + int h = hash(in_e.getKey()); + if (table.get(h) == AVAIBLE) { + LogFile n = new LogFile(); + table.set(h,n); + avaible_slots--; + elements++; + return n.insert(in_e); + } else { + LogFile n = table.get(h); + elements++; + return n.insert(in_e); + } + } + @Override public Entry remove(Entry e) throws InvalidEntryException { if (e == null) @@ -180,7 +196,7 @@ public class ChainingHashTable implements Dictionary { table = table2; for (Entry e : ite){ - insert(e.getKey(),e.getValue()); + insert_refactor(e); } } diff --git a/dictionary/LinearProbingHashTable.java b/dictionary/LinearProbingHashTable.java index 8b9b9fb..6665921 100644 --- a/dictionary/LinearProbingHashTable.java +++ b/dictionary/LinearProbingHashTable.java @@ -90,13 +90,13 @@ public class LinearProbingHashTable implements Dictionary { checkKey(key); int h = hash(key); int count = 0; - int i = 0; for (;count < raw_size();){ - if (table.get(h+i) == AVAIBLE) + if (table.get( (h+count)%raw_size() ) == AVAIBLE) return null; - if (table.get(h+i).getKey().equals(key)) - return table.get(h+i); - i= (i + 1)% raw_size(); + if (table.get((h+count)%raw_size()) == null) + continue; + if (table.get((h+count)%raw_size()).getKey().equals(key)) + return table.get((h+count)%raw_size()); count++; } return null; @@ -108,13 +108,13 @@ public class LinearProbingHashTable implements Dictionary { NodePositionList> to_return = new NodePositionList>(); int h = hash(key); int count = 0; - int i = 0; for (;count < raw_size();){ - if (table.get(h+i) == AVAIBLE) + if (table.get((h+count)%raw_size()) == AVAIBLE) return to_return; - if (table.get(h+i).getKey().equals(key)) - to_return.addLast( table.get(h+i)); - i= (i + 1)% raw_size(); + if (table.get((h+count)%raw_size()) == null) + continue; + if (table.get((h+count)%raw_size()).getKey().equals(key)) + to_return.addLast( table.get((h+count)%raw_size())); count++; } return to_return; @@ -126,29 +126,51 @@ public class LinearProbingHashTable implements Dictionary { if (load_factor() >= 0.5 ) reformat(); int h = hash(key); - if (table.get(h) == AVAIBLE) { + if (table.get(h) == AVAIBLE || table.get(h ) == null ) { MyEntry n = new MyEntry(key,value); table.set(h,n); avaible_slots--; elements++; return n; } else { - int i = 1; int count = 1; for ( ; count < raw_size() ;) { - if (table.get(h + i) == AVAIBLE) { + if (table.get((h+count)%raw_size()) == AVAIBLE || table.get((h+count)%raw_size()) == null ) { MyEntry n = new MyEntry(key, value); - table.set(h + i, n); + table.set((h+count)%raw_size(), n); avaible_slots--; elements++; return n; } - i = (i + 1) % raw_size(); count++; } return null; // non dovresti essere qui a causa del load factor } } + protected Entry insert_refactor(Entry in_e) throws InvalidKeyException { + // no check of in_e because this function is used internally from reformat + int h = hash(in_e.getKey()); + if (table.get(h) == AVAIBLE || table.get(h ) == null ) { + Entry n = in_e; + table.set(h,n); + avaible_slots--; + elements++; + return n; + } else { + int count = 1; + for ( ; count < raw_size() ;) { + if (table.get((h+count)%raw_size()) == AVAIBLE || table.get((h+count)%raw_size()) == null ) { + Entry n = in_e; + table.set((h+count)%raw_size(), n); + avaible_slots--; + elements++; + return n; + } + count++; + } + return null; // non dovresti essere qui a causa del load factor + } + } @Override public Entry remove(Entry e) throws InvalidEntryException { @@ -157,25 +179,29 @@ public class LinearProbingHashTable implements Dictionary { int h = hash(e.getKey()); if (table.get(h) == AVAIBLE) throw new InvalidEntryException(); - if (table.get(h) == e) { - Entry removed = table.get(h); - table.remove(h); - table.set(h,AVAIBLE); - elements--; - avaible_slots++; - return removed; - } - int count = 1; - for (int i = 1; count < raw_size() ; i = (i + 1) % raw_size()){ - if (table.get(h + i) == AVAIBLE) - throw new InvalidEntryException(); - if (table.get(h + i) == e) { - Entry removed = table.get(h + i); - table.remove(h + i); - table.set(h + i,AVAIBLE); + if (table.get(h) == null){ + + } else { + if (table.get(h) == e) { + table.set(h, null); elements--; avaible_slots++; - return removed; + return e; + } + } + int count = 1; + for (; count < raw_size() ;){ + if (table.get((h+count)%raw_size()) == AVAIBLE) + throw new InvalidEntryException(); + if (table.get((h+count)%raw_size()) == null){ + + } else { + if (table.get((h+count)%raw_size()) == e) { + table.set((h+count)%raw_size(), null); + elements--; + avaible_slots++; + return e; + } } count++; } @@ -189,7 +215,7 @@ public class LinearProbingHashTable implements Dictionary { NodePositionList> ret = new NodePositionList>(); for (int i = 0 ; i< raw_size() ;i++){ - if (table.get(i) != AVAIBLE){ + if (table.get(i) != AVAIBLE && table.get(i) != null ){ ret.addLast(table.get(i)); } } @@ -220,7 +246,7 @@ public class LinearProbingHashTable implements Dictionary { table = table2; for (Entry e : ite){ - insert(e.getKey(),e.getValue()); + insert_refactor(e); } } diff --git a/dictionary/Logfile.java b/dictionary/Logfile.java index 702afb8..9335705 100644 --- a/dictionary/Logfile.java +++ b/dictionary/Logfile.java @@ -62,6 +62,18 @@ public class LogFile implements Dictionary { return t; } + protected Entry insert(Entry in_e) throws InvalidKeyException { + //in_e not used because used internally + LocationAwareEntry t = (LocationAwareEntry)in_e; + list.addLast(t); + t.setLocation(list.last()); + return t; + } + + + + + @Override public Entry remove(Entry e) throws InvalidEntryException { LocationAwareEntry t = checkEntry(e); diff --git a/set/OrderedListSet.java b/set/OrderedListSet.java index fa7f1c4..e20f1a3 100644 --- a/set/OrderedListSet.java +++ b/set/OrderedListSet.java @@ -47,8 +47,7 @@ public class OrderedListSet implements Set { @Override - public Set union(Set B) { - new MergeUnion(this.L, ,c); + public Set union(Set B) {return null; } @Override