-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic hash and some use case for search implementation
- Loading branch information
1 parent
f8f45a5
commit 551dd67
Showing
2 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
unsigned long simpleHash(const string& input) { | ||
unsigned long hash = 0; | ||
for(char c : input) { | ||
hash = hash * 31 + c; | ||
} | ||
return hash%10; | ||
} | ||
|
||
bool isContained (const vector<string>& dataArr, string data){ | ||
if (dataArr[simpleHash(data)]==data){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
int main() { | ||
vector<string> dataArr(10); | ||
for (int i = 0; i < 10; ++i) { | ||
dataArr[i] = " string "; | ||
} | ||
for (int i = 0; i < 10; ++i){ | ||
cout << dataArr[i] << ", "; | ||
} | ||
cout << endl; | ||
|
||
string data = "Hello, world!"; | ||
unsigned long hashValue = simpleHash(data); | ||
cout << "Hash Value: " << hashValue << endl; | ||
|
||
dataArr[hashValue] = data; | ||
|
||
for (int i = 0; i < 10; ++i){ | ||
cout << dataArr[i] << ", "; | ||
} | ||
cout << endl; | ||
|
||
bool option = isContained(dataArr,data); | ||
|
||
if (option){ | ||
cout << "data is contained"; | ||
} else { | ||
cout << "not found"; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
// Use case is Search for "Hello World" in side the data set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters