dimanche 28 mai 2017

Vector of Vectors chained hash table

I'm supposed to implement a chained hash table using a vector of vectors and I am really unfamiliar with a lot of these concepts and can't seem to figure out how to make the second constructor and the put function. I'm hopinf to get some suggestions on how to implement these two funcitons. Here is the entry.h file, my table.h file, and my table.cpp implementation file of what i have so far. Thanks.

entry.h:

#ifndef entry_h
#define entry_h

// entry.h - defines class Entry for CS 32 PA1              


#include <string>
#include <iosfwd>

class Entry {

public:
    // constructor                                          
    Entry(unsigned int key = 0, std::string data = "");

    // access and mutator functions                         
    unsigned int get_key() const;
    std::string get_data() const;
    static unsigned int access_count();
    void set_key(unsigned int k);
    void set_data(std::string d);

    // operator conversion function simplifies comparisons  
    operator unsigned int () const;

    // input and output friends                             
    friend std::istream& operator>>
    (std::istream& inp, Entry &e);
    friend std::ostream& operator<<
    (std::ostream& out, Entry &e);

private:
    unsigned int key;
    std::string data;
    static unsigned int accesses;
};

#endif /* entry_h */

table.h:

//  table.h                                                 
//  Lab05                                                   
//                                                          
#ifndef table_h
#define table_h

#include <string>
#include <vector>
#include "entry.h"

using namespace std;

class Table {

 public:
  Table(unsigned int max_entries = 100);
  //Builds empty table to hold 100 entries                  
  Table(unsigned int entries, std::istream& input);
  //Builds table to hold entries, reads and puts them 1 at \
a time                                                      
  ~Table();
  //Destructor                                              
  void put(unsigned int key, std::string data);
  //Creates new entry for the table                         
  //Updates if key is used                                  
  void put(Entry e);
  //Creates a copy of e in the table                        
  string get(unsigned int key) const;
  //Returns string associated with key                      
  //Returns empty string if key isnt used                   
  bool remove(unsigned int key) const;
  //Removes entry in given key                              
  //Returns true of removed, false of no entry              
  friend std::ostream& operator<< (std::ostream& out, const\
 Table& t);
  //Prints each entry in the table in order of their key    


 private:
  int size;
  int max_entries;
  int hashkey(int key);

  vector<vector<Entry*> > A;
};

#endif /* table_h */

table.cpp:

//table.cpp                                                                                                             

#include<iostream>
#include<vector>
#include "table.h"

using namespace std;


Table::Table(unsigned int max_entries){
  max_entries = 100;
  size = max_entries * 2;
  A.resize(size);
}

Table::Table(unsigned int entries, std::istream& input){
  size = entries*2;
  A.resize(size);

}

Table::~Table() {
  for (int i = 0; i <size; i++) {
    for (int j = 0; j < A[i].size(); j++) {
      delete A[i][j];
    }
  }
}

void Table::put(unsigned int key, std::string data){
  Entry e;
  e.set_key(key);
  e.set_data(data);
  put(e);
}

void Table::put(Entry e){

}

string Table::get(unsigned int key) const {
  return "stub";
}

bool Table::remove(unsigned int key) const {
  return true;
}

ostream& operator << (ostream& out, const Table& t) {
  out << "stub";
  return out;
}

int Table::hashkey(int key){
  return key % size;
}

Aucun commentaire:

Enregistrer un commentaire