Skip to content

Commit

Permalink
Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhufk committed Jun 30, 2016
1 parent 0730820 commit ba2a6c3
Show file tree
Hide file tree
Showing 10 changed files with 749 additions and 0 deletions.
12 changes: 12 additions & 0 deletions HashTable.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp \
additivedictionary.cpp

HEADERS += \
hash.h \
adword.h \
additivedictionary.h
316 changes: 316 additions & 0 deletions HashTable.pro.user

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions additivedictionary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "additivedictionary.h"

#include <fstream>

AdditiveDictionary::AdditiveDictionary()
{

}


bool AdditiveDictionary::compare(std::string A, std::string B){
return A.length() > B.length();
}

bool comp(ADWord A,ADWord B){
return A.getAmount() > B.getAmount();
}

unsigned hash_function(ADWord *X){
return 1;
}

bool equ(ADWord *A, ADWord *B){
return (A->getValue().compare(B->getValue()) == 0);
}


bool AdditiveDictionary::addValue(std::string value, unsigned amount){
ADWord V(value, amount);
ADWord *in = hashTable::find(new ADWord(value, amount), hash_function, equ);
if (in){
in->setAmount(in->getAmount() + amount);
}else{
hashTable::add(&V, hash_function);
}
return true;
}

bool AdditiveDictionary::addValueFromFile(std::string file_name){
std::ifstream file;
file.open(file_name);

std::string word;
while (file>>word) {
addValue(word, 1);
}
return true;
}
20 changes: 20 additions & 0 deletions additivedictionary.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef ADDITIVEDICTIONARY_H
#define ADDITIVEDICTIONARY_H

#include <hash.h>

#include <string>
#include <adword.h>


class AdditiveDictionary: public hashTable<ADWord>
{
public:
AdditiveDictionary();
bool addValue(std::string, unsigned);
bool addValueFromFile(std::string);
bool compare(std::string, std::string);
bool equal(std::string, std::string);
};

#endif // ADDITIVEDICTIONARY_H
42 changes: 42 additions & 0 deletions adword.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef ADWORD_H
#define ADWORD_H

#include <string>

class ADWord
{
std::string value;
unsigned amount;
public:
ADWord(){
this->value = "";
this->amount = 0;
}

ADWord(std::string value, unsigned amount){
this->value = value;
this->amount = amount;
}
std::string getValue(){
return value;
}

void setAmount(unsigned amount){
this->amount = amount;
}

unsigned getAmount(){
return amount;
}

bool operator>(ADWord B){
return value.compare(B.value) > 0;
}

bool operator==(ADWord B){
return value.compare(B.value) == 0;
}

};

#endif // ADWORD_H
50 changes: 50 additions & 0 deletions hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "hash.h"
/*
template <class valueType> hashTable<valueType>::hashTable()
{
amount = 0;
}
template <class valueType>hashTable<valueType>::hashTable(unsigned int n){
amount = n;
for (unsigned i = 0; i < n; ++i){
value.push_back(NULL);
}
}
template <class valueType> bool hashTable<valueType>::add(valueType *element, unsigned (*hash_function)(valueType)){
unsigned index = hash_function(&element);
index %= amount;
unsigned count = 0;
while (value[index]!=NULL){
++count;
if (count > amount){
return false;
}
++index;
index %= amount;
}
value[index] = element;
return true;
}
template <class valueType> valueType* hashTable<valueType>::find(valueType *X, unsigned (*hash_function)(valueType), bool (*comp)(valueType, valueType)){
unsigned index = hash_function(&X);
index %= amount;
unsigned count = 0;
while (!comp(value[index], X)){
++count;
if (count > amount){
return NULL;
}
++index;
index %= amount;
}
return value[index];
}
*/
100 changes: 100 additions & 0 deletions hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#ifndef HASH_H
#define HASH_H

#include <deque>
#include <string>

template <class valueType>
class hashTable
{
std::deque<valueType*> value;
unsigned amount;

public:
hashTable(){
amount = 0;
}

hashTable(unsigned n){
amount = n;
for (unsigned i = 0; i < n; ++i){
value.push_back(NULL);
}
}

bool add(valueType *element, unsigned (*hash_function)(valueType*)){
unsigned index = hash_function(element);
index %= amount;
unsigned count = 0;
while (value[index]!=NULL){
++count;
if (count > amount){
return false;
}

++index;
index %= amount;
}

value[index] = element;
return true;
}

valueType *find(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
unsigned index = hash_function(X);
index %= amount;
unsigned count = 0;
while (!comp(value[index], X) && value[index]!=NULL){
++count;
if (count > amount){
return NULL;
}

++index;
index %= amount;
}

return value[index];
}

bool del(valueType *X, unsigned (*hash_function)(valueType*), bool (*comp)(valueType*, valueType*)){
//FIND
unsigned index = hash_function(X);
index %= amount;
unsigned count = 0;
while (!comp(value[index], X) && value[index]!=NULL){
++count;
if (count > amount){
return false;
}

++index;
index %= amount;
}

unsigned del = index;
++index;
index %= amount;

unsigned prev;
while(value[index] != NULL && del != index){
if (hash_function(value[del]) == hash_function(value[index])){
if (index == 0){
prev = amount - 1;
}else{
prev = index - 1;
}

value[prev] = value[index];
//value[index] = NULL;

}
++index;
index %= amount;
}
value[index] = NULL;
return true;
}
};

#endif // HASH_H
100 changes: 100 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <iostream>

#include <hash.h>
#include <road.h>

using namespace std;

unsigned hashF(std::string key){
unsigned h = 0;
for (unsigned i = 0; i < key.length(); ++i){
h = 33 * h ^ key[i];
}
return h;
}

unsigned hashN(unsigned N){
unsigned h = 0;
h = 33 * h ^ N;
return h;
}

unsigned hashF(unsigned key){
unsigned h = 0;
h = 33 * h ^ key;
return h;
}

unsigned hash_by_start_point(Road *X){
return hashF(X->getA());
}

unsigned hash_by_num(Road *X){
return 2;
}

bool compare_by_number(Road *X, Road *Y){
return X->getN() == Y->getN();
}

int main()
{
hashTable<Road> Ukraine(20);
Ukraine.add(new Road(1, std::string("Chernivtci"), std::string("Lviv")), hash_by_num);
Ukraine.add(new Road(2, std::string("Lviv"), std::string("Ternopil")), hash_by_num);
Ukraine.add(new Road(3, std::string("Ternopil"), std::string("Ivano-Frankivsk")), hash_by_num);
Ukraine.add(new Road(4, std::string("Ivano-Frankivsk"), std::string("Khmelnutskyy")), hash_by_num);
Ukraine.add(new Road(5, std::string("Khmelnutskyy"), std::string("Vinnutsa")), hash_by_num);
Ukraine.add(new Road(6, std::string("Vinnutsa"), std::string("Zytomur")), hash_by_num);
Ukraine.add(new Road(7, std::string("Zytomur"), std::string("Rivne")), hash_by_num);
Ukraine.add(new Road(8, std::string("Rivne"), std::string("Kiev")), hash_by_num);
Ukraine.add(new Road(9, std::string("Kiev"), std::string("Chernigiv")), hash_by_num);
Ukraine.add(new Road(10, std::string("Chernigiv"), std::string("Sumy")), hash_by_num);
Ukraine.add(new Road(11, std::string("Sumy"), std::string("Cherkasu")), hash_by_num);
Ukraine.add(new Road(12, std::string("Cherkasu"), std::string("Poltava")), hash_by_num);
Ukraine.add(new Road(13, std::string("Poltava"), std::string("Kirovograd")), hash_by_num);
Ukraine.add(new Road(14, std::string("Kirovograd"), std::string("Dnipropetrovsk")), hash_by_num);
Ukraine.add(new Road(15, std::string("Dnipropetrovsk"), std::string("Zaporija")), hash_by_num);
Ukraine.add(new Road(16, std::string("Zaporija"), std::string("Kherson")), hash_by_num);
Ukraine.add(new Road(17, std::string("Kherson"), std::string("Mukolayiv")), hash_by_num);
Ukraine.add(new Road(18, std::string("Mukolayiv"), std::string("Odesa")), hash_by_num);
Ukraine.add(new Road(19, std::string("Kharkiv"), std::string("Rivne")), hash_by_num);
Ukraine.add(new Road(20, std::string("Rivne"), std::string("Lutsk")), hash_by_num);

/*
unsigned N;
bool hasBreak = false;
std::cout<<"Set N...";
std::cin>>N;
unsigned way;
std::cin>>way;
Road *W;
if (way != 0){
W = Ukraine.find(new Road(way, std::string(""), std::string("")), hash_by_num, compare_by_number);
}
Road *start = W;
Road *Prev;
unsigned count = 1;
for (unsigned i = 1; i < N; ++i){
++count;
std::cin>>way;
if (!hasBreak){
Prev = W;
W = Ukraine.find(new Road(way, std::string(""), std::string("")), hash_by_num, compare_by_number);
if (!Prev->isNeighborDirectly(*W)){
hasBreak = true;
}
}
}
if (count == N){
std::cout<<"NO!";
}else{
std::cout<<start->getA()<<" to "<<Prev->getB();
}*/
Ukraine.del(new Road(2, "", ""), hash_by_num, compare_by_number);
return 0;
}
6 changes: 6 additions & 0 deletions road.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "road.h"

Road::Road()
{

}
Loading

0 comments on commit ba2a6c3

Please sign in to comment.