- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
Latest commit
83 lines (76 loc) · 3.53 KB
/
Copy pathEncoder.java
File metadata and controls
83 lines (76 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Sara Hrnciar
* Dr. K
* Program 5
* Encoder class
* Does ziv encoding. Maybe I could add other encoding methods later
*/
importjava.util.ArrayList;
publicclassEncoder {
publicEncoder(){
}
publicStringZivEncode(Stringmessage){ //combines the the steps. idk why exactly i made so many subroutines but what's done is done.
ArrayList<String> uniqueList = getUnique(message); //get the list of unique substrs
String[] toRet = newString[uniqueList.size()]; //returns an array of encoded reps of the unique items
intlargestSize = uniqueList.get(0).length();
for (inti = 1; i<uniqueList.size(); i++){
if (largestSize < uniqueList.get(i).length()){
largestSize = uniqueList.get(i).length();
}
} //finds the largest substring in the list
intminBits = (int)((Math.log(uniqueList.size())) / Math.log(2) + 1); //use that to find the min bits to repr each substr
//toRet[0] = "1";
System.out.println(minBits);
toRet[0] = "0";//(new Numb("1", 10, 2, 1)).convertBasetoR();
toRet[1] = "1";//(new Numb("2", 10, 2, 1)).convertBasetoR();
intwhichOneAmI = -1;
StringfinalEncode;
Stringcur;
for (inti = 2; i<uniqueList.size(); i++){
cur = uniqueList.get(i);
intcount = i-1;
while (count>=0){ //from the current substr, look backwards to find which one cur is based off of. ex. fourth substring will look at third, then second, then first item to determine.
if (uniqueList.get(count).equals(cur.substring(0,cur.length()-1))){
//must turn count into binary
whichOneAmI = count;
break;
}
count-=1;
}
finalEncode = (newNumb(Integer.toString(whichOneAmI + 1), 10, 2, minBits)).convertBasetoR();
finalEncode+=cur.charAt(cur.length()-1);
toRet[i] = finalEncode;
}
StringencodedString = "";
for (Stringthing:toRet){ //string version for final encoded msg
encodedString+=thing;
}
returnencodedString;
}
publicArrayList<String> getUnique(Stringmessage){ //returns a list of all of the unique substrings of the list
intlength = message.length();
intcharsLeft = length;
ArrayList<String> unique = newArrayList<>();
StringremainingString;
StringuniqueInstance;
intcount = 0;
while (charsLeft != 0){ //while we still have chars in the original msg to look at
remainingString = message.substring(length-charsLeft, length); //find whats left to look at
uniqueInstance = remainingString.substring(0,1); //initial unique str
count = 1;
for (Stringinstance:unique){ //check to make sure you cant add more chars to the unique string
if (instance.equals(uniqueInstance)){
count++;
if (count>remainingString.length()){
count = remainingString.length();
}
uniqueInstance = remainingString.substring(0,count);
}
}
count = 0; //reset count
charsLeft -= uniqueInstance.length(); //keep track of how many chars there are left to look at
unique.add(uniqueInstance); //add to the arr
}
returnunique;
}
}