-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
276 lines (255 loc) · 11.9 KB
/
Program.cs
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace MultiThreadedSearch
{
class Program
{
//Define a Static List:
static List<Tuple<int, int>> list = new List<Tuple<int, int>>();
//Define a Static Flage:
static bool flg = false;
//
static string[] TextLines;
//
static Regex rx_For_Pre_Processing;
//
static Regex rx_For_Matching;
//
static int CandidateStringLen;
static void Main(string[] args)
{
/******************** Get Input from the user ********************/
Console.WriteLine("Enter a text File Address");
string TextFileAdress = Console.ReadLine();
//2.String to Search:
Console.WriteLine("Enter a string keyword to search");
string StringToSearch = Console.ReadLine();
//3.Number of threads
Console.WriteLine("Enter the Number of threads");
int nThreads = int.Parse(Console.ReadLine());
//4.The Delta number
Console.WriteLine("Enter the Delta");
int Delta = int.Parse(Console.ReadLine());
//for example:
//string TextFileAdress = @"C:\Users\עומר\source\repos\Ex2_Question4\Ex2_Question4\Text Files for example\Alice.txt";
//2.String to Search:
//string StringToSearch = "*";
//3.Number of threads
//int nThreads = 5;
//4.The Delta number
//int Delta = 9;
//Pre-Processing:
//Step A: Calculate the two regex:
//Step A_1: Calculate the Regex to Identify the Candidates Strings Using the Calculate_Regex_For_Candidate Function:
rx_For_Pre_Processing = new Regex(Calculate_Regex_For_Candidate(StringToSearch));
//Step A_2: Calculate the the Regex Who Identify the matches String:
Tuple<string, int> tup = Calculate_Regex_For_Match(StringToSearch, Delta);
string StringToSearch_Phrase_Pattern = tup.Item1;
rx_For_Matching = new Regex(StringToSearch_Phrase_Pattern);
//Step B: Calculate the Leanght of the string:
CandidateStringLen = tup.Item2;
//Step c:Read the file.txt to a array
TextLines = File.ReadAllLines(TextFileAdress, Encoding.UTF8);
//Part A for the Use in thread-Pre processing Using nThreads
int N = 0;
int M = TextLines.Length;
int Therad_responsible = (M - N) / nThreads;
Thread[] arr = new Thread[nThreads];
int index_start = N;
int index_end = index_start + Therad_responsible;
for (int i = 0; i < nThreads; i++)
{
arr[i] = new Thread(() => Program.Pre_Processing_WorkerFunction(index_start, index_end));
arr[i].Start();
Thread.Sleep(70);
if (i == nThreads - 2)
{
index_start = index_end;
index_end = M;
}
else
{
index_start = index_end;
index_end = index_end + Therad_responsible;
}
}
for (int j = 0; j < nThreads; j++)
{
arr[j].Join();
}
List<Tuple<int, int>> mico = list;
//Part B for the Use in thread for search the Matching
N = 0;
M = list.Count;
Therad_responsible = (M - N) / nThreads;
arr = new Thread[nThreads];
index_start = N;
index_end = index_start + Therad_responsible;
for (int i = 0; i < nThreads; i++)
{
arr[i] = new Thread(() => Program.WorkerFunction(index_start, index_end));
arr[i].Start();
Thread.Sleep(70);
if (i == nThreads - 2)
{
index_start = index_end;
index_end = M;
}
else
{
index_start = index_end;
index_end = index_end + Therad_responsible;
}
}
for (int j = 0; j < nThreads; j++)
{
arr[j].Join();
}
//Check if nothing dont print to the screen:
if (flg == false)
{
Console.WriteLine("not found");
}
}
/******************** Initalizing the Helper Function to the Program********************/
/**
*1.Calculate_Regex_For_Candidate(string StringToSearch)
* Input:
* -(string) StringToSearch:the function get the pharse that need to serach
*
* Output:
* -(string) Identify_Candidate_Pattern: the function will return the regex who should help the candidate string pharse.
*
* For example:From the next input: StringToSearch='HL' =>we get the next Output:regex-[H]
*/
public static string Calculate_Regex_For_Candidate(string StringToSearch)
{
string StringToSearch_First_char = StringToSearch[0].ToString();
String Identify_Candidate_Pattern = @"[" + StringToSearch_First_char + "]";
return Identify_Candidate_Pattern;
}
/**
* 2.Calculate_Regex_For_Match(string StringToSearch, int Delta)
* Input:
* -(string) StringToSearch:the function get the pharse that need to serach
* -(int) Delta: the function get the delta between each char in the string
*
* Output:
* -(string) StringToSearch_Phrase_Pattern: the function will return the regex who represent the candidate string-the regex build according
* the next two input from the user: StringToSearch,Delta.
* -(int) CandidateStringLen: the function will return the Leangth of the Match string
*
* For example:From the next input: StringToSearch='HL',Delta=2 =>we get the next Output:regex-[H]..[L],CandidateStringLen=4
*/
public static Tuple<string, int> Calculate_Regex_For_Match(string StringToSearch, int Delta)
{
string DeltaInRgex = "";
for (int i = 0; i < Delta; i++)
{
DeltaInRgex += ".";
}
String StringToSearch_Phrase_Pattern = @"";
int Len_StringToSearch = StringToSearch.Length;
int CandidateStringLen = 0;
if (Len_StringToSearch <= 1)
{
StringToSearch_Phrase_Pattern += "[" + StringToSearch[0].ToString() + "]";
CandidateStringLen = Len_StringToSearch;
}
else
{
CandidateStringLen = Len_StringToSearch + Delta * (Len_StringToSearch - 1);
for (int i = 0; i < Len_StringToSearch - 1; i++)
{
StringToSearch_Phrase_Pattern += "[" + StringToSearch[i].ToString() + "]";
StringToSearch_Phrase_Pattern += DeltaInRgex;
}
StringToSearch_Phrase_Pattern += "[" + StringToSearch[Len_StringToSearch - 1].ToString() + "]";
}
return Tuple.Create(StringToSearch_Phrase_Pattern, CandidateStringLen);
}
/**
*3.Pre_Processing_WorkerFunction(string[] TextLines, Regex rx_For_Pre_Processing)
* Input:
* -(string[]) TextLines: the functon get an array that each cell represent a row from the text file.
* -(Regex) rx_For_Pre_Processing: the function get a regex that its role is to find the candidate strings.
*
* Output:
* -(List<Tuple<int, int>>) list: the function will return a list of tupple that each cell represent the location
* of a candidate string in the text file on the next order:
* Tuple(row_number,position in the row)
*
* For example:For the next input: TextLines='[hello word,....]',rx_For_Pre_Processing = '[w]' >we will get the next Output:[tup(0,6),......]
*/
public static void Pre_Processing_WorkerFunction(int Start_Search, int EndSearch)
{
//Define the index of line:
string line = "";
for (int k = Start_Search; k < EndSearch; k++)
{
line = TextLines[k];
// Find matches.
MatchCollection Pre_Processing_matches = rx_For_Pre_Processing.Matches(line);
// Report on each match.
foreach (Match match in Pre_Processing_matches)
{
GroupCollection groups = match.Groups;
list.Add(Tuple.Create(k, groups[0].Index));
}
}
}
/**
*4.WorkerFunction(string[] TextLines, List<Tuple<int, int>> list, Regex rx_For_Matching, int CandidateStringLen)
* Input:
* -(string[]) TextLines: the functon get an array that each cell represent a row from the text file.
* -(List<Tuple<int, int>>) list: the function get list of tupple that each cell represent the location
* of a candidate string in the text file on the next order:
* Tuple(row_number,position in the row)
* -(Regex) rx_For_Matching: the function get a regex that its role is to find the string in the text who match.
* -(int) CandidateStringLen: the function get an leangth of the string that had to match
*
* Output:
* -if the candidate string has a match the function will print his propertys-
* other whise the function will dont print nothing!
*
*/
public static void WorkerFunction(int Start_Search, int EndSearch)
{
String Candidate_String = "";
Tuple<int, int> tuple_row_position;
int Row_number;
int Position_number;
for (int t = Start_Search; t < EndSearch; t++)
{
Candidate_String = "";
//get the tupple who represent the candidate string location
tuple_row_position = list[t];
//Get the row number and the position of the candidate string-from the tuple:
Row_number = tuple_row_position.Item1;
Position_number = tuple_row_position.Item2;
Candidate_String += TextLines[Row_number].Substring(Position_number);
int count_HowManyRowsToAdd = 1;
while ((Candidate_String.Length < CandidateStringLen) && (Row_number + count_HowManyRowsToAdd < TextLines.Length))
{
Candidate_String += " " + TextLines[Row_number + count_HowManyRowsToAdd];
count_HowManyRowsToAdd++;
}
MatchCollection matches = rx_For_Matching.Matches(Candidate_String);
foreach (Match match in matches)
{
//Check if the match who found is prefer to the candidate string
GroupCollection groups = match.Groups;
if (groups[0].Index == 0)
{
flg = true;
Console.WriteLine("[" + Row_number + "," + Position_number + "]");
}
}
}
}
}
}