Commit e7a645a8 authored by Kamal Khodabhai's avatar Kamal Khodabhai

Upload finger.cpp

parent e5ffc566
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> idPortMapping;
map<int, int> fingureTable;
vector<int> idList;
void genrateFingerTable(int id, int n) {
string line;
idList.clear();
idPortMapping.clear();
ifstream id_port("id_port.csv");
while (getline(id_port, line)) {
char temp[line.length()];
strcpy(temp, line.c_str());
char *token1 = strtok(temp, ",");
char *token2 = strtok(NULL, "-");
idPortMapping[atoi(token1)] = atoi(token2);
idList.push_back(atoi(token1));
}
sort(idList.begin(), idList.end());
id_port.close();
int modulo = pow(2, n);
for(int i = 0; i < n; i++) {
int tmp = (int)(id + pow(2, i)) % modulo;
auto lb = lower_bound(idList.begin(), idList.end(), tmp);
fingureTable[tmp] = idPortMapping[lb == idList.end() ? idList[0] : *lb];
}
}
int main() {
int id;
cin>>id;
genrateFingerTable(id, 8);
for(auto i : fingureTable)
cout<<i.first<<" => "<<i.second<<endl;
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment