Skip to content

Commit 9e01159

Browse files
authored
Finish line is just by the corner.
-Although testing need to occur, seems that the project is now finished. -Implemented searchPage in BinaryFileSearch class (see BinaryFileSearch.java).
1 parent 39d6700 commit 9e01159

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed

src/searchOps/BinaryFileSearch.java

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5+
import java.util.Collections;
56

67
import Files.FilePageAccess;
78
import util.Word;
@@ -28,10 +29,91 @@ public BinaryFileSearch(FilePageAccess f)
2829

2930

3031

32+
/*
33+
* An iterative binary search should expose any keys matching the given one.
34+
*
35+
* */
3136
@Override
32-
public void searchPage(String k)
37+
public void searchPage(String key)
3338
{
3439

40+
41+
//structures to assist.
42+
ArrayList<Word> tokens;
43+
found = new ArrayList<Word>();
44+
45+
//a flag to mark finding event.
46+
boolean isFound = false;
47+
48+
//define right and left parts.
49+
int left = 0;
50+
int right = filePages;
51+
int mid = left +(right-left)/2;
52+
53+
//parse pages, distinguish the middle to be accesed.
54+
while(left <= right && isFound == false)
55+
{
56+
57+
58+
59+
System.out.println("Loading Page: "+mid);
60+
61+
//load the middle page
62+
63+
tokens = loadPage(mid+1);
64+
65+
//parse it to seek for the key
66+
for(int i =0; i < tokens.size(); i++)
67+
{
68+
//check for equality, then append.
69+
if(tokens.get(i).getContext().compareTo(key) == 0)
70+
{
71+
found.add(tokens.get(i));
72+
73+
//last element of any page might have another occurrence on next page.
74+
if((i == tokens.size() - 1) || i == 0)
75+
isFound = false;
76+
else
77+
isFound = true;
78+
}
79+
}
80+
81+
//adjust the new middle position.
82+
if(!isFound)
83+
{
84+
String first = tokens.get(0).getContext();
85+
String last = tokens.get(tokens.size()-1).getContext();
86+
87+
88+
if(!(key.compareTo(first) > 0))
89+
{
90+
right = mid - 1;
91+
/*
92+
* Debug Lines
93+
* */
94+
//System.out.println("<-");
95+
}
96+
else if(!(key.compareTo(last) < 0))
97+
{
98+
99+
left = mid + 1;
100+
/*
101+
* Debug Lines.
102+
*
103+
* */
104+
//System.out.println("->");
105+
}
106+
else
107+
{
108+
//key is not on the given file. yet we mark the finding event to finish searching.
109+
isFound = true;
110+
}
111+
112+
}
113+
mid = left +(right-left)/2;
114+
115+
}
116+
35117

36118

37119
}
@@ -57,9 +139,35 @@ public ArrayList<Word> loadPage(int n)
57139
}
58140

59141
@Override
60-
public void print() {
61-
// TODO Auto-generated method stub
142+
public void print()
143+
{
144+
145+
if(found.size() > 0)
146+
{
147+
148+
Collections.sort(found);
149+
//print the key.
150+
System.out.print("\""+found.get(0).getContext()+"\""+" is on lines: ");
151+
152+
//print the lines.
153+
for(int i = 0; i < found.size(); i++)
154+
{
155+
System.out.print(found.get(i).getLine());
156+
if((i+1) != found.size())
157+
System.out.print(", ");
158+
}
159+
//print disk accesses.
160+
161+
System.out.format("\nDisk Accesses: %s \n",this.diskAccess);
162+
163+
}
164+
else
165+
System.out.println("Key not found");
166+
62167

63168
}
169+
170+
171+
64172

65173
}

src/test/Main.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,20 @@ private static void execCommand(Session u, List lines,Scanner input, FileOps f,F
246246

247247
SerialFileSearch sfs = new SerialFileSearch(fpa);
248248

249+
249250
System.out.print("Type word for search: ");
250251
sfs.searchPage(input.nextLine().trim());
251252
sfs.print();
253+
break;
252254

253-
255+
case "b":
256+
257+
BinaryFileSearch bfs = new BinaryFileSearch(fpa);
258+
System.out.print("Type word for search: ");
259+
bfs.searchPage(input.nextLine().trim());
260+
bfs.print();
261+
262+
break;
254263

255264

256265

@@ -272,7 +281,7 @@ private static String inputCheck(String inp)
272281

273282
//using a regex to check input and another one to check whether to print message to user or not.
274283
String valid = "[atdlnpqwxcvsb=#\\-\\$\\^\\+]";
275-
String printable = "[scvlp=#]";
284+
String printable = "[bscvlp=#]";
276285

277286
Matcher m = Pattern.compile(valid).matcher(inp);
278287
Matcher mp = Pattern.compile(printable).matcher(inp);

src/util/Word.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,25 @@ public void setLine(int line) {
3535

3636

3737

38+
/*
39+
* Sorting by line if context is the same,else by context <String>, this is to enable sorting by line in BinaryFileSearch.java.
40+
*
41+
* It has no effect on sorting by actual context if given strings are different.
42+
*
43+
*/
44+
3845
@Override
3946
public int compareTo(Word w) {
40-
return this.context.compareTo(w.getContext());
47+
48+
int line = this.context.compareTo(w.getContext());
49+
50+
51+
return line == 0 ? this.line - w.getLine() : line;
4152

4253
}
4354

4455

56+
4557

4658

4759

0 commit comments

Comments
 (0)