简体   繁体   中英

Hash table - hash function implementation

I have this project where I need to implement an hash table. I have two classes: Fans and tickets. Fans can have tickets and each ticket is associated to a fans' email.

My question is, what is gonna be the key and where should I implement my hash function? My guess is that it is gonna be at Ticket.h but I still dunno how I'm gonna associate the ticket to the fan (owner) email.

I dont think that any code is needed but I'll post some if any doubt comes up.

Best Regards

Class Fan ("Adepto")

class Adepto {

int uid;
unordered_set<string> email;
static int newID;
string nome;
string nEquipa;

public:

Adepto(string nome);
//Adepto(string nome, Equipa* e1, vector<Bilhete*> bilhetes);
Adepto();

unsigned int getID() const;

string getNome() const;
void setNome(string n);

string getEquipa() const;
void setEquipa(string nEq);

string getEmail() const;
void setEmail(string novoEmail);

Ticket class(bilhete)

struct hash_adeptos{
int operator() (const Adepto &a1) const{
    return a1.getEmail()().size(); }

bool operator() (const Adepto & a1, const Adepto & a2) const{
    return a1.getEmail() == a2.getEmail();}

};


typedef tr1::unordered_set<Adepto, hash_adeptos, hash_adeptos> TabelaAdeptos;

 class Bilhete{
TabelaAdeptos adeptos;

int uid;
static int newID;
date validade;
string dono;
bool vendido;

 public:

Bilhete(date validade, string dono, bool vendido);
Bilhete();

int getID() const;
void setID(int id);

date getValidade() const;
void setValidade(date date);

string imprimeBilhete() const;

//Adepto* getDono() const;
//void setDono (Adepto &a1);

bool getEstado() const;
bool setVendido(Bilhete &b1);
};

My question is, what is gonna be the key

I think that the key is ticket. So you can get information about ticket holder by ticket number.

and where should I implement my hash function

I don't think that matters. I will probably create another pair of files: TicketHash.hpp and TicketHash.cpp

I still dunno how I'm gonna associate the ticket to the fan (owner) email

Hash function must take ticket as parameter and return number of cell ( or pointer to cell ) in hash table with corresponding information about ticket holder.

I think you can even make function like this unsigned int hash(Ticket& ticket) { return ticket.number; } unsigned int hash(Ticket& ticket) { return ticket.number; } , but then it will be just an array instead of hash table.

For examples of hash functions see this question

I'd have implemented this in the following manner:

Class Tickets{
    String number;
    //Other details...
}

Class Fans{
    ArrayList<Tickets> list;
    String email;
    int index;      //this is an auto increment field which I'd have used in my implementation, just for the ease of further operations. This actually helps.
    //Other details
}

Class Hash{
    //KEY
    String[] key;    //index and email in `Fans` class are unique values
    //Value
    ArrayList<Tickets>[] value;

    //function to assign values
    void assign(String id, Ticket ticket){
        if(key.contains(id))
            value<index of id>.add(Ticket);
        else
            value<new index>.add(Ticket);
    }

    //function that returns value
    Arraylist<Tickets> value(String id){
        return value<index of id>;
    }
}

EDIT:

Sorry that I did not see the tag c++. I've written it in JAVA-Like syntax, but since it is crude logic, it should be understandable. If any confusion is there, please post a comment below. In place of ArrayList, you can use vector<> or list<> in cpp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM