Skip to content

Commit c883125

Browse files
authored
Lab9
1 parent 2db45f0 commit c883125

23 files changed

+329
-0
lines changed

Lab9/JakubRadzikLab9.zip

223 KB
Binary file not shown.

Lab9/Lab9.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

Lab9/Wyniki.odt

15.2 KB
Binary file not shown.

Lab9/Wyniki.pdf

214 KB
Binary file not shown.
800 Bytes
Binary file not shown.
3.31 KB
Binary file not shown.
527 Bytes
Binary file not shown.
Binary file not shown.

Lab9/out/production/Lab9/Node.class

862 Bytes
Binary file not shown.

Lab9/out/production/Lab9/Tab.class

217 Bytes
Binary file not shown.

Lab9/out/production/Lab9/Tests.class

2.87 KB
Binary file not shown.

Lab9/out/production/Lab9/main1.class

2.49 KB
Binary file not shown.
1.31 KB
Binary file not shown.

Lab9/src/ArrayWithDoubleHash.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class ArrayWithDoubleHash extends ArrayWithHashing{
2+
private int temporaryKey = 0;
3+
public ArrayWithDoubleHash(int SIZE) {
4+
super(SIZE);
5+
}
6+
public ArrayWithDoubleHash() {
7+
}
8+
public int hash(int i) {
9+
temporaryKey = i;
10+
return super.hash(i);
11+
}
12+
@Override
13+
public int hash2(int i){
14+
return (i%49)+(i/5)+i*5;
15+
}
16+
public int getOffset(int i, int hash) {
17+
return (((hash+(i*hash2(temporaryKey)))%tab.length)+tab.length)%tab.length;
18+
}
19+
20+
}

Lab9/src/ArrayWithHashing.java

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
public abstract class ArrayWithHashing implements Tab{
2+
private int tries = 0;
3+
public int getTries() {
4+
return tries;
5+
}
6+
public void setTries(int tries) {
7+
this.tries = tries;
8+
}
9+
10+
protected static final int SIZE = 30;
11+
Node[] tab;
12+
public ArrayWithHashing(int SIZE) {
13+
this.tab = new Node[SIZE];
14+
}
15+
public ArrayWithHashing() {
16+
tab = new Node[SIZE];
17+
} //WITH SIZE = 30
18+
19+
public int hash(int key) {
20+
return ((key%8)+key/3)%tab.length;
21+
}
22+
public int hash2(int key) {
23+
return (key*5)+2 % tab.length;
24+
}
25+
public int put(int k, int v) throws Exception{
26+
int hash = hash(k);
27+
int i = 0;
28+
if (tab[hash] == null) {
29+
tab[hash] = new Node(k,v);
30+
} else {
31+
while (tab[getOffset(i, hash)] != null) {
32+
33+
if (tab[getOffset(i, hash)].getKey() == k) {
34+
int temp = tab[getOffset(i, hash)].getValue();
35+
tab[getOffset(i, hash)].setValue(v);
36+
setTries(i);
37+
return temp;
38+
}
39+
if (size() == tab.length)
40+
throw new Exception("Array full");
41+
i++;
42+
}
43+
tab[getOffset(i, hash)] = new Node(k,v);
44+
}
45+
setTries(i);
46+
return -1;
47+
}
48+
public int get(int k) throws Exception{
49+
int hash = hash(k);
50+
int i = 0;
51+
52+
if(tab[hash]!=null && tab[hash].getKey()==k){
53+
setTries(i);
54+
return tab[hash].getValue();
55+
}
56+
57+
while (tab[getOffset(i, hash)] != null && tab[getOffset(i, hash)].getKey() != k) {
58+
if(i>tab.length){
59+
setTries(i);
60+
return -1;
61+
}
62+
i++;
63+
}
64+
setTries(i);
65+
66+
if(tab[getOffset(i, hash)]!=null){
67+
return tab[getOffset(i, hash)].getValue();
68+
}else{
69+
return -1;
70+
}
71+
72+
}
73+
public int size() {
74+
int c = 0;
75+
for (int i = 0; i < tab.length; i++) {
76+
if(tab[i]!=null){
77+
c++;
78+
}
79+
}
80+
return c;
81+
}
82+
public boolean isEmpty() {
83+
for(int i = 0; i < tab.length; i++) {
84+
if(tab[i]!=null){
85+
return false;
86+
}
87+
}
88+
return true;
89+
}
90+
public void resize(int newSize) {
91+
Node[] newTab = new Node[newSize];
92+
for (int i = 0; i < tab.length && i<newTab.length; i++) {
93+
newTab[i] = tab[i];
94+
}
95+
tab = newTab;
96+
}
97+
public void dump() {
98+
System.out.print('[');
99+
for (int i = 0; i < tab.length; i++) {
100+
if(tab[i]==null){
101+
System.out.print("[X,X] , ");
102+
}else{
103+
System.out.print("["+tab[i].getKey()+":"+tab[i].getValue()+"], ");
104+
}
105+
}
106+
System.out.println("]");
107+
}
108+
public int getOffset(int i, int hash){
109+
return 0;
110+
}
111+
@Override
112+
public boolean containsKey(int key) {
113+
int hash = hash(key);
114+
for (int i = hash; i < tab.length; i++) {
115+
if(tab[i]!=null && tab[i].getKey()==key){
116+
return true;
117+
}
118+
}
119+
return false;
120+
}
121+
}

Lab9/src/ArrayWithLineHash.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class ArrayWithLineHash extends ArrayWithHashing{
2+
3+
public ArrayWithLineHash(int SIZE) {
4+
super(SIZE);
5+
}
6+
public ArrayWithLineHash() {
7+
}
8+
9+
@Override
10+
public int getOffset(int i, int hash) {
11+
return (hash+i)%(tab.length);
12+
}
13+
14+
}

Lab9/src/ArrayWithQuadraticHash.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ArrayWithQuadraticHash extends ArrayWithHashing{
2+
3+
public ArrayWithQuadraticHash(int SIZE) {
4+
super(SIZE);
5+
}
6+
public ArrayWithQuadraticHash() {
7+
}
8+
public int getOffset(int i, int hash){
9+
return (((hash + ((-1^(i-1))*(((i+1)/2))^2))%tab.length)+tab.length )%tab.length;
10+
}
11+
12+
}

Lab9/src/Node.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Node {
2+
private Integer key;
3+
private Integer value;
4+
5+
public Node(int key, int value) {
6+
this.key = key;
7+
this.value = value;
8+
}
9+
10+
public Node() {
11+
12+
}
13+
14+
public int getKey() {
15+
return key;
16+
}
17+
18+
public void setKey(int key) {
19+
this.key = key;
20+
}
21+
22+
public int getValue() {
23+
return value;
24+
}
25+
26+
public void setValue(int value) {
27+
this.value = value;
28+
}
29+
}

Lab9/src/Tab.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public interface Tab {
2+
int get(int key) throws Exception;
3+
int put(int k, int v) throws Exception;
4+
boolean containsKey(int key);
5+
}

Lab9/src/Tests.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.Random;
2+
3+
public class Tests {
4+
private static final int SIZE = 2000;
5+
private double[] alfa = {0.7,0.8,0.9};
6+
int[] data;
7+
int[] search;
8+
9+
public Tests() {
10+
data = new int[SIZE];
11+
search = new int[100];
12+
Random r = new Random(SIZE);
13+
14+
for (int i = 0; i < search.length; i++) {
15+
search[i] = r.nextInt(SIZE);
16+
}
17+
18+
for (int i = 0; i < data.length; i++) {
19+
data[i] = r.nextInt(SIZE);
20+
}
21+
TestStart();
22+
}
23+
24+
public void TestStart(){
25+
ArrayWithLineHash line = new ArrayWithLineHash(SIZE);
26+
ArrayWithQuadraticHash quad = new ArrayWithQuadraticHash(SIZE);
27+
ArrayWithDoubleHash dual = new ArrayWithDoubleHash(SIZE);
28+
29+
for (int i = 0; i < alfa.length; i++) {
30+
System.out.println("TEST DLA ZAPELNIENIA alfa = "+alfa[i]);
31+
System.out.println("Linear Hashing: "+ result(line, alfa[i]));
32+
System.out.println("Quadratic Hashing: "+ result(quad, alfa[i]));
33+
System.out.println("Double Hashing: "+ result(dual, alfa[i]));
34+
System.out.println("===========================================");
35+
}
36+
}
37+
//COUNT SECTION=======================================================
38+
private int tries;
39+
private int success_tries;
40+
private int fail_tries;
41+
public String result(ArrayWithHashing ah, double alfa){
42+
43+
try{
44+
tries = 0;
45+
success_tries = 0;
46+
fail_tries = 0;
47+
48+
for (int i = 0; i < (int)(data.length*alfa); i++) {
49+
ah.put(data[i], data[i]*10);
50+
}
51+
52+
for (int i = 0; i < search.length; i++) {
53+
int find = ah.get(search[i]);
54+
tries+= ah.getTries();
55+
56+
if(find==-1){
57+
fail_tries += ah.getTries();
58+
} else{
59+
success_tries += ah.getTries();
60+
}
61+
ah.setTries(0);
62+
}
63+
64+
}catch (Exception e){
65+
e.printStackTrace();
66+
}
67+
68+
return "ALL: "+(tries/(double)search.length)+" S:"+(success_tries/(double)search.length)+" F:"+(fail_tries/(double)search.length);
69+
}
70+
}

Lab9/src/main1.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class main1 {
2+
public static void main(String[] args) throws Exception {
3+
4+
ArrayWithLineHash linear = new ArrayWithLineHash(30);
5+
ArrayWithQuadraticHash quad = new ArrayWithQuadraticHash(30);
6+
ArrayWithDoubleHash dual = new ArrayWithDoubleHash(30);
7+
8+
System.out.println("WRZUCANIE ELEMENTÓW WRAZ Z PODGLĄDEM TABLICY dumb()");
9+
for (int i = 0; i < 20; i++) {
10+
linear.put(i,i*10);
11+
quad.put(i,i*10);
12+
dual.put(i,i*10);
13+
14+
System.out.println("i: " + i +" linear_hash(i): "+linear.hash(i));
15+
System.out.println("LINEAR: ");
16+
linear.dump();
17+
System.out.println("QUAD: ");
18+
quad.dump();
19+
System.out.println("DUAL: ");
20+
dual.dump();
21+
System.out.println("==============================================================================================");
22+
}
23+
24+
System.out.println("WYNIKI DLA OPERACJI GET:");
25+
for (int i = 0; i < 20; i++) {
26+
System.out.println(linear.get(i)+" "+quad.get(i)+" "+dual.get(i));
27+
}
28+
System.out.println(linear.get(22)+" "+quad.get(22)+" "+dual.get(22));
29+
30+
System.out.println("DALSZE OPERACJE:");
31+
System.out.println("ROZMIAR:");
32+
System.out.println(linear.size()+" "+quad.size()+" "+dual.size());
33+
System.out.println("IS EMPTY:");
34+
System.out.println(linear.isEmpty()+" "+quad.isEmpty()+" "+dual.isEmpty());
35+
System.out.println("CONTAINS 99:");
36+
System.out.println(linear.containsKey(99)+" "+quad.containsKey(99)+" "+dual.containsKey(99));
37+
System.out.println("RESIZE() na rozmiar 5");
38+
linear.resize(5);
39+
quad.resize(5);
40+
dual.resize(5);
41+
linear.dump();
42+
quad.dump();
43+
dual.dump();
44+
System.out.println("\n\nTESTY PORÓWNAN: ");
45+
new Tests();
46+
}
47+
}

Lab9/wyniki.ods

4.2 KB
Binary file not shown.

Lab9/wyniki.xlsx

23.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)