c++ - How can I make a vector of pairs work like a hash table? -


i'm working on 1-bit bimodal branch prediction simulator class project. thinking of using unordered_map table need able set size, thinking using vector of pairs , table.reserve(tablesize) may way this.

however, leaves me linear search of vector find table entries horribly slow. know way can implement hash function application?

for of not know how branch predictor table works, key pc address 0x12345678 , value t or nt (branch taken or non-taken). need method of collision resolution. if branch taken, bit (bool) set true, prediction next branch. if next branch not taken, bit set false or 0. purpose of simulator measure correct predictions vs total branches accuracy , compare other methods' accuracies. so, ultimate goal use 2 least significant bytes of pc (by masking them out) define position in hash table store prediction value 0 or 1.

any appreciated.

on side note: maximum table size 1024 entries time complexity won't have chance scale high, optimization worth since i'll entering competition.


edit:

decided go unordered_map.

this solution i've got far:

header file

// bimodal 1-bit class bm1pred{     private:             std::ofstream &outputfile;             //hash table bool value mapped addresses             // 0 - not taken, 1- taken             unordered_map<long long, bool> table;             int tablesize;     public:              // constructor             bm1pred(std::ofstream& file, int size)             : outputfile(file), tablesize(size) {}              // deconstructor             ~bm1pred()             {}              // member functions             void parseresults(std::string filename);  }; 

you can still use std::unordered_map. if want reserve specific size, can use std::unordered_map::reserve.

but can't see why std::map wouldn't option. size changes dynamically. why need "set size"?

to answer additional questions, in both cases map[key] returns reference value mapped key equivalent key, performing insertion if such key not exist. insert or update item by

map[key] = value; 

in case, value stored key updated value if key exists in container; otherwise, insertion performed. don't need check yourself.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -