26-CS106X-Practice-Midterm

Youre going to implement a new version of the map

Info icon This preview shows pages 8–9. Sign up to view the full content.

View Full Document Right Arrow Icon
You’re going to implement a new version of the Map using the ideas outlined above. Rather than making it a template, we’ll map string s to double s to make things a little simpler. Here is the class definition you’ll be working with (in practice, there would be containsKey , get , iterator , and mapAll methods as well, but you’re not going to implement or even use them, so they’re being omitted): class Map { public: Map(); ~Map(); bool put(string key, double value); private: struct bucket { bool occupied; string key; double value; }; bucket *buckets; // addresses the array where all data gets stored int numBuckets; // allocated length of the array int count; // number of meaningful key-value pairs in the map int hash(string key, int numBuckets); void rehash(); }; The buckets field points to an array of numBuckets bucket records. The occupied field keeps track of whether or not that particular bucket is occupied by real data. If true , then some meaningful key-value pair occupies the rest of the record; if false , the key and 8
Image of page 8

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
value fields are irrelevant and ignored. (Note that all key-value pairs reside directly within the array addressed by buckets .) a. Implement the Map constructor, which constructs a raw Map to be logically empty but with enough space for 64 key-value pairs. Then implement the destructor, which disposes of all resources maintained by the Map being destroyed. const int kInitNumBuckets = 64; Map::Map() { Map::~Map() { b. Using the supplied hash method, implement the put method, which ensures that the specified key is part of the Map and that it’s associated with the specified value . It uses the quadratic internal probing technique, as described above, to find a home for the new key-value pair. put returns true if the new key-value pairs gets inserted into a previously unoccupied bucket, and false if the new value replaces a previously inserted one. (Don’t worry about rehashing the Map if more then three quarters of the buckets are occupied. You’ll worry about that in part c.) const int kHashMultiplier = 716911; int Map::hash(string key, int numBuckets) { int hashcode = 0; for (int i = 0; i < key.size(); i++) { hashcode = hashcode * kHashMultiplier + key[i]; } return hashcode % numBuckets; } bool Map::put(string key, double value) { if (count > 3 * numBuckets / 4) { rehash(); // you’ll implement this helper method in part c } // complete the implementation using the next page and the space below c. Finally, implement the rehash method, which updates the Map so that it has twice as many buckets and all of its key-value pairs are rehashed to take up residence in a bucket where they could have resided had the new number of buckets been the number of buckets all along. (You’ll benefit by figuring out how to call put to help with the redistribution.) void Map::rehash() { 9
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern