Skip to content

Instantly share code, notes, and snippets.

@djpeach
Created November 20, 2018 19:22
Show Gist options
  • Save djpeach/25a4fb56cd35acbe28d0567302bb982b to your computer and use it in GitHub Desktop.
Save djpeach/25a4fb56cd35acbe28d0567302bb982b to your computer and use it in GitHub Desktop.
Hashing program for 362
the
of
and
a
to
bb
in
is
just
j
jj
you
that
it
z
zz
Zed
zed
Z
he
was
for
on
are
as
with
his
they
I
at
be
this
Therapy
have
from
or
one
had
by
words
but
not
what
all
were
we
when
your
can
said
there
use
an
each
which
she
do
how
their
if
will
up
other
about
out
many
then
them
these
so
some
her
would
make
like
him
into
time
has
look
two
more
write
go
see
number
no
way
could
people
my
than
first
water
been
called
who
oil
sit
now
find
long
down
day
did
get
come
made
may
part
over
new
sound
take
only
little
work
know
place
years
live
me
back
give
most
very
after
things
our
just
name
good
sentence
man
think
say
great
where
help
through
much
before
line
right
too
means
old
any
same
tell
boy
follow
came
want
show
also
around
form
three
small
set
put
end
does
another
well
large
must
big
even
such
because
turn
here
why
ask
went
men
read
need
land
different
home
us
move
try
kind
hand
picture
again
change
off
play
spell
air
away
animal
house
point
page
letter
mother
answer
found
study
still
learn
should
America
world
#include <list>
#include <iostream>
#include <string>
#include "Timer.h"
#include "HashTable.h"
HashTable::HashTable(int size)
{
this->tableSize = size;
this->hashTable = new std::list<std::string>[this->tableSize];
Timer timer = Timer();
float exec_time;
}
int HashTable::hashWord(std::string word)
{
int index;
if(word.size() > 1) {
index = (((int(word.at(0)) % 32) - 1) * 26) + ((int(word.at(1)) % 32) - 1);
} else {
index = (((int(word.at(0)) % 32) - 1) * 26) + ((int(word.at(0)) % 32) - 1);
}
return index;
}
void HashTable::addWord(std::string word)
{
int index = hashWord(word);
this->hashTable[index].push_back(word);
}
bool HashTable::findWord(std::string word)
{
timer.start();
int index = hashWord(word);
std::list<std::string> wordList = hashTable[index];
for(std::list<std::string>::iterator iter = wordList.begin(); iter != wordList.end(); iter++) {
if(*iter == word) {
return true;
}
}
timer.stop();
this->execTime = timer.getInterval();
return false;
}
void HashTable::printSimilar(std::string word)
{
int index = hashWord(word);
std::list<std::string> wordList = hashTable[index];
for(std::list<std::string>::iterator iter = wordList.begin(); iter != wordList.end(); iter++) {
std::cout << " " << *iter << "\n";
}
timer.stop();
this->execTime = timer.getInterval();
}
void HashTable::printTable()
{
for(int i = 0; i < tableSize; i++) {
std::list<std::string> wordList = hashTable[i];
if(wordList.size() > 0) {
std::cout << "\n" << i;
} else {
std::cout << ".";
}
for(std::list<std::string>::iterator iter = wordList.begin(); iter != wordList.end(); iter++) {
std::cout << " --> " << *iter;
}
if(wordList.size() > 0) {
std::cout << "\n";
}
}
}
#ifndef _HASH_TABLE_
#define _HASH_TABLE_
#include <list>
#include <iostream>
#include <string>
#include "Timer.h"
class HashTable
{
int tableSize;
std::list<std::string> *hashTable;
Timer timer;
int hashWord(std::string);
public:
HashTable(int);
void addWord(std::string);
bool findWord(std::string);
void printSimilar(std::string);
float execTime;
void printTable();
};
#endif
#include <fstream>
#include <sstream>
#include "HashTable.h"
int main() {
HashTable hashTable(676);
std::ifstream fileStream;
std::stringstream buffer;
std::string word;
std::string hashableWord;
fileStream.open("Dictionary.txt", std::ifstream::in);
if(fileStream.is_open()) {
while(fileStream.good()) {
std::getline(fileStream, word, '\n');
buffer.clear();
buffer.str("");
buffer << word;
buffer >> hashableWord;
hashTable.addWord(hashableWord);
}
}
// hashTable.printTable();
std::string keyWord;
std::cout << "\nEnter a word for me to find: ";
std::cin >> keyWord;
std::cout << "\n Word is in the hashTable: ";
if(hashTable.findWord(keyWord)) {
std::cout << "True\n\n";
hashTable.printSimilar(keyWord);
std::cout << "\n";
} else {
std::cout << "False\n\n";
}
std::cout << " " << hashTable.execTime << " micro-sec\n\n";
return 0;
}
a.out: Driver.o HashTable.o Timer.o
g++ -std=c++11 Driver.o HashTable.o Timer.o
Driver.o: main.cpp
g++ -c main.cpp -o Driver.o
HashTable.o: HashTable.cpp HashTable.h Timer.o
g++ -c HashTable.cpp
Timer.o: Timer.cpp Timer.h
g++ -c Timer.cpp -o Timer.o
run: a.out
./a.out
clean:
rm *.o a.out
rerun:
make clean
make run
#include <sys/time.h>
#include <cstddef>
#include "Timer.h"
Timer::Timer(){}
void Timer::start()
{
gettimeofday(&this->start_time, NULL);
}
void Timer::stop()
{
gettimeofday(&this->end_time, NULL);
}
float Timer::getInterval()
{
float t =(float)(end_time.tv_sec-start_time.tv_sec)*1000000.0+(float)(end_time.tv_usec-start_time.tv_usec);
return t;
}
#ifndef _TIME_INTERVAL_
#define _TIME_INTERVAL_
#include <sys/time.h>
#include <cstddef>
class Timer{
public:
timeval start_time;
timeval end_time;
public:
Timer();
void start();
void stop();
float getInterval();
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment