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
|
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include "Information.h"
using namespace std;
class LinkedHashEntry {
private:
int key;
Information info;
LinkedHashEntry *next;
public:
LinkedHashEntry(int key, Information &info) {
this->key = key;
this->info.setCity(info.getCity());
this->info.setState(info.getState());
this->info.setLatitude(info.getLatitude());
this->info.setLongitude(info.getLongitude());
this->info.setPopulation(info.getPopulation());
this->next = NULL;
}
int getKey() {
return this->key;
}
Information getInfo() {
return this->info;
}
void setInfo(Information &info) {
this->info.setCity(info.getCity());
this->info.setState(info.getState());
this->info.setLatitude(info.getLatitude());
this->info.setLongitude(info.getLongitude());
this->info.setPopulation(info.getPopulation());
}
LinkedHashEntry *getNext() {
return next;
}
void setNext(LinkedHashEntry *next) {
this->next = next;
}
};
const int TABLE_SIZE = 1009;
class HashMap {
private:
float threshold;
int maxSize;
int tableSize;
int size;
int loadFactor;
LinkedHashEntry **table;
void resize() {
int oldTableSize = tableSize;
tableSize *= 2;
maxSize = (int) (tableSize * threshold);
LinkedHashEntry **oldTable = table;
table = new LinkedHashEntry*[tableSize];
for (int i = 0; i < tableSize; i++)
table[i] = NULL;
size = 0;
for (int hash = 0; hash < oldTableSize; hash++)
if (oldTable[hash] != NULL) {
LinkedHashEntry *oldEntry;
LinkedHashEntry *entry = oldTable[hash];
while (entry != NULL) {
put(entry->getKey(), entry->getInfo());
oldEntry = entry;
entry = entry->getNext();
delete oldEntry;
}
}
delete[] oldTable;
}
public:
HashMap() {
threshold = 0.75f;
maxSize = 96;
tableSize = TABLE_SIZE;
size = 0;
table = new LinkedHashEntry*[tableSize];
for (int i = 0; i < tableSize; i++)
table[i] = NULL;
}
void setThreshold (float threshold) {
this->threshold = threshold;
maxSize = (int) (tableSize * threshold);
}
long getHashValue(string City) {
long sum = 0;
long length = City.size();
for (long i = 0; i < length; i++) {
sum += (int)City[i];
}
sum *= 13;
sum %= 1009;
if (table[sum] != NULL) {
while(table[sum]->getInfo().getCity() == City) {
sum++;
}
}
return sum;
}
void get (long key, string City) {
if (table[key] == NULL) {
cout << City << " was not found!";
}
else {
LinkedHashEntry *entry = table[key];
for (entry; entry != NULL; entry=entry->getNext()) {
Information info;
info = entry->getInfo();
cout << info.getCity() << ", " << info.getState() << ", " << info.getLatitude() << ", " << info.getLongitude() << ", " << info.getPopulation() << endl;
}
}
}
void put (long key, Information info) {
if (table[key] == NULL) {
table[key] = new LinkedHashEntry(key, info);
size++;
}
else {
LinkedHashEntry *entry = table[key];
while (entry->getNext() != NULL)
entry = entry->getNext();
if (entry->getKey() == key)
entry->setInfo(info);
else
entry->setNext(new LinkedHashEntry(key, info));
}
if (size >= maxSize)
resize();
}
int getLoadFactor() {
loadFactor = size / TABLE_SIZE;
return loadFactor;
}
~HashMap() {
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL) {
LinkedHashEntry *prevEntry = NULL;
LinkedHashEntry *entry = table[i];
while (entry != NULL) {
prevEntry = entry;
entry = entry->getNext();
delete prevEntry;
}
}
delete[] table;
}
};
int main()
{
ifstream inputFile;
HashMap *map;
map = new HashMap;
long key;
int loadFactor;
double latitude, longitude, population;
string inputFileName, line, city, state;
string searchCity = "";
Information *info;
info = new Information;
cout << "Please enter the name of the file: " ;
cin >> inputFileName;
inputFile.open(inputFileName.c_str());
if(inputFile.good())
{
cout << "File was opened!" << endl;
while (inputFile.good())
{
getline(inputFile, line, '|'); //0
getline(inputFile, line, '|'); //1 State
info->setState(line);
getline(inputFile, line, '|'); //2
getline(inputFile, line, '|'); //3
getline(inputFile, line, '|'); //4
getline(inputFile, line, '|'); //5
getline(inputFile, line, '|'); //6
getline(inputFile, line, '|'); //7
getline(inputFile, line, '|'); //8
getline(inputFile, line, '|'); //9 Latitude
latitude = ::atof(line.c_str());
info->setLatitude(latitude);
getline(inputFile, line, '|'); //10 Longitude
longitude =::atof(line.c_str());
info->setLongitude(longitude);
getline(inputFile, line, '|'); //11
getline(inputFile, line, '|'); //12
getline(inputFile, line, '|'); //13
getline(inputFile, line, '|'); //14
getline(inputFile, line, '|'); //15
getline(inputFile, line, '|'); //16 Population
population =::atof(line.c_str());
info->setPopulation(population);
getline(inputFile, line, '|'); //17
getline(inputFile, line, '\n'); //18 City
info->setCity(line);
key = map->getHashValue(info->getCity());
map->put(key, *info);
info = new Information;
}
loadFactor = map->getLoadFactor();
cout << "The Load Factor of the table is: " << loadFactor << endl;
cout << "Enter the name of the city you want to search or enter 0 to end the program." << endl;
while (searchCity != "0") {
cout << "Enter the name of the city you want to search or enter 0 to end the program." << endl;
getline(cin, searchCity);
key = map->getHashValue(searchCity);
map->get(key, searchCity);
}
}
else {
cout << "ERROR! Unable to open the specified file";
}
inputFile.close();
return EXIT_SUCCESS;
}
|