#ifndef LISTKEYVAL_HPP #define LISTKEYVAL_HPP #include "KeyVal.hpp" #include /********************************************************************* * This is an example header without any...

1 answer below »
The program has to run on Centos Linux.I need a Makefile.Comments explaining the code.Deadline: 24 hours please (Urgent)All instructions are included in the document file. Please look at the rubric.


#ifndef LISTKEYVAL_HPP #define LISTKEYVAL_HPP #include "KeyVal.hpp" #include /********************************************************************* * This is an example header without any implementation that you * may find useful in your effort to create an associative array. * * You are not required to conform to this header in any way, you are * only required to conform to the overall interface header provided * in KeyVal.hpp. * * However, there are helpful hints contained here that may make both * this PEX and future PEXes which build on the base interface easier * for you to implement. A careful study of this architecture is * highly recommended. * ********************************************************************/ //forward declaration template class ListKeyVal; /** * @class ListKeyValNode * * The purpose of this class is to store key value pairs in a sorted list. * Note that all methods and members are private and only accessible by the * friend class ListKeyVal. * * The reason that the ListKeyValNode class and the ListKeyVal class are * separate is to make usage cleaner. We must be able to create ListKeyVal * objects on the stack just as we would any other object. Since additions and * deletions to the list may change the root node, it is not effective to * create a root node as the base object * * This separate private class also allows us to remove some default constuctors * and use only deep copy assignment in this class while exposing all options * in the public class. */ template class ListKeyValNode : public std::enable_shared_from_this<> > { friend class ListKeyVal; private: /** * @brief Default Constructor */ ListKeyValNode() = default; /** * @brief Remove the copy constructor */ ListKeyValNode(const ListKeyValNode &other) = delete; /** * @brief provide an assignment operator which deep copies our * data structure * * @param other Ref to RHS object; deep copy this structure * @return ListKeyNodeVal ref for chaining assignments */ ListKeyValNode &operator=(const ListKeyValNode &other); /** @brief Ptr to key; may be null for last item in list */ std::shared_ptr m_key; /** @brief Ptr to value; may be null for last item in list */ std::shared_ptr m_val; /** @brief Ptr to next node in list; may be null for last item in list */ std::shared_ptr m_next; /** @brief Weak ptr to prev node in list; may be null for first item in list note that weak ref is used to avoid mem leak islands*/ std::weak_ptr<> > m_prev; }; /** * @class ListKeyVal */ template class ListKeyVal : public KeyVal { public: /** * @brief Constructor * * This ctor creates a valid root node */ ListKeyVal(); /** * @brief Copy ctor * * Creates a deep copy of entire data structure * * @param other Data structure to copy */ ListKeyVal(const ListKeyVal &other); /** * @brief Assignment operator * * Creates a deep copy of entire data structure * * @param other Data structure to copy * @return ref to populated object for assignment chaining */ ListKeyVal &operator=(const ListKeyVal &other); /** * @brief Insert an object * * This will place a COPY of the val object * into the associative array * * Note that since an insert may change the root node, I have * created an "internal" function that returns the new root. * this function will call the internal version and then reset * the root node if needed. This model will make follow on * PEXs where recursion is required more clean/understandable. * * @param key Key associated with value * @param val Value which is stored at location key */ virtual void insert(const K &key, const V &val) override; /** * @brief Remove an object from the associative array * * This will remove the key/value pair from the array * If the key is not found, no action is taken * * Note that since a delete may change the root node, I have * created an "internal" function that returns the new root. * this function will call the internal version and then reset * the root node if needed. This model will make follow on * PEXs where recursion is required more clean/understandable. * * @param key Key for which key/val pair is removed */ virtual void del(const K &key) override; /** * @brief Get a pointer to value * * Given a key, a shared_ptr to a value is returned. * note that if the key did not exist, then the returned * ptr is not valid * * @param key Key for which value is returned * @return ptr to value if key existed */ virtual std::shared_ptr get(const K &key) override; /** * @brief Execute callback for each entry * * Rather than force students to create an inner iterator class, * this functiona allows a callback function to be executed for * every item in the associative array. Note that callbacks should * be called in order of keys sorted least to greatest * * @param callback Function to be called with each item in the associative array */ virtual void forEach(std::function callback) override; private: /** * @brief Insert a node and return new root * * @param key Key to insert * @param val Value associated with key * @return New root of node list */ std::shared_ptr > insertInternal(const K &key, const V &val); /** * @brief Delete a node and return new root * * @param key Key to insert * @return New root of node list */ std::shared_ptr > delInternal(const K &key); /** @breif Track root node */ std::shared_ptr<> > m_rootNode; }; #endif /* LISTKEYVAL */ #include "ListKeyVal.hpp" #include #include int main() { ListKeyVal > outer; ListKeyVal lkv; //populate an LKV lkv.insert("one", 1); lkv.insert("three", 3); lkv.insert("four", 4); lkv.insert("two", 2); //put two copies map into outer outer.insert("outer1", lkv); outer.insert("outer2", lkv); //iterate over outer then inner std::cout < std::endl="">< std::endl="">< "full="" iteration:"="">< std::endl;="" outer.foreach([](const="" std::string="" &key,=""> &val){ std::cout < key="">< std::endl;="" val.foreach([](const="" std::string="" &key,="" int="" &val){="" std::cout="">< "="" key:="" "="">< key="">< std::endl;="" std::cout="">< "="" val:="" "="">< val="">< std::endl;="" });="" });="" make="" a="" copy="" of="" one="" lkv,="" delete="" an="" item,="" then="" iterate="" std::cout="">< std::endl="">< std::endl="">< "deleted="" item="" \"three\":"="">< std::endl;=""><> > pLkv = outer.get("outer1"); pLkv->del("three"); pLkv->forEach([](const std::string &key, int &val){ std::cout < "="" key:="" "="">< key="">< std::endl;="" std::cout="">< "="" val:="" "="">< val="">< std::endl;="" });="" increment="" each="" val="" in="" second="" lkv="" plkv="outer.get("outer2");" plkv-="">forEach([](const std::string &key, int &val){ val++; }); //final iteration, should contain three in first set and //each val in second should be incremented std::cout < std::endl="">< std::endl="">< "after="" each="" item="" incremented:"="">< std::endl;="" outer.foreach([](const="" std::string="" &key,=""> &val){ std::cout < key="">< std::endl;="" val.foreach([](const="" std::string="" &key,="" int="" &val){="" std::cout="">< "="" key:="" "="">< key="">< std::endl;="" std::cout="">< "="" val:="" "="">< val="">< std::endl; }); }); } programming exercise #8 (associative arrays: simple implementation) should run on centos linux .hpp should be included main.cpp file should be included makefile should be included. overview in this programming exercise the coder will create an associative array data structure. an associative array accepts a key and returns an associated value. it is an extremely useful data structure which may also be referred to as a map, dictionary, or symbol table. the first phase of this exercise deals with the interface and creating the correct behavior of the data structure. follow on phases will make the data structure more efficient. as a practical use of the data structure, the coder will construct a database of electronic components. each of the components will have a number of features/specifications, and each feature/specification will have a value. for example, here are a couple of resistors that may be found in our parts database: name: std::endl;="" });="" });="" }="" programming="" exercise="" #8="" (associative="" arrays:="" simple="" implementation)="" should="" run="" on="" centos="" linux="" .hpp="" should="" be="" included="" main.cpp="" file="" should="" be="" included="" makefile="" should="" be="" included.="" overview="" in="" this="" programming="" exercise="" the="" coder="" will="" create="" an="" associative="" array="" data="" structure.="" an="" associative="" array="" accepts="" a="" key="" and="" returns="" an="" associated="" value.="" it="" is="" an="" extremely="" useful="" data="" structure="" which="" may="" also="" be="" referred="" to="" as="" a="" map,="" dictionary,="" or="" symbol="" table.="" the="" first="" phase="" of="" this="" exercise="" deals="" with="" the="" interface="" and="" creating="" the="" correct="" behavior="" of="" the="" data="" structure.="" follow="" on="" phases="" will="" make="" the="" data="" structure="" more="" efficient.="" as="" a="" practical="" use="" of="" the="" data="" structure,="" the="" coder="" will="" construct="" a="" database="" of="" electronic="" components.="" each="" of="" the="" components="" will="" have="" a="" number="" of="" features/specifications,="" and="" each="" feature/specification="" will="" have="" a="" value.="" for="" example,="" here="" are="" a="" couple="" of="" resistors="" that="" may="" be="" found="" in="" our="" parts="" database:="">
Answered Same DayDec 08, 2021

Answer To: #ifndef LISTKEYVAL_HPP #define LISTKEYVAL_HPP #include "KeyVal.hpp" #include...

Aditi answered on Dec 10 2021
144 Votes
Solution/KeyVal.hpp
#ifndef KEYVAL_HPP
#define KEYVAL_HPP
#include
#include
/**
* @class KeyVal
*
* This class is an interface to a system that can
* arbitrarily track key/value pairs. The abiltiy to
* build an "associative array" will be invaluable
* to a coder
*
* An associative array allows the coder to fetch
* an object by using a key. This essentially makes
* a content addressable data structure.
*
* All derived class
es must use this template verbatim
*
* Notes:
* 1. Objects that are used as keys in the table
* must have the less than operator defined.
* 2. KeyVal objects should be creatable on both the
* stack and the heap
* 3. KeyVal objects should be assignable and copy constructible
* and should make deep copies as appropriate
* 4. The forEach method should iterate over the structure in
* sorted order least to greatest according to key value
*/
template
class KeyVal
{
public:
/**
* @brief Insert an object
*
* This will place a COPY of the val object
* into the associative array
*
* @param key Key associated with value
* @param val Value which is stored at location key
*/
virtual void insert(const K &key,
const V &val){
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
                     }
/**
* @brief Remove an object from the associative array
*
* This will remove the key/value pair from the array
* If the key is not found, no action is taken
*
* @param key Key for which key/val pair is removed
*/
virtual void del(const K &key){
    if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;

if (position == 0)
{
*head_ref = temp->next;
free(temp);
return;
}

for (int i=0; temp!=NULL && i temp = temp->next;

if (temp == NULL || temp->next == NULL)
return;

struct Node *next = temp->next->next;

free(temp->next);

temp->next = next;
}
/**
* @brief Get a pointer to value
*
* Given a key, a shared_ptr to a value is returned.
* note that if the key did not exist, then the returned
* ptr is not valid
*
* @param key Key for which value is returned
* @return ptr to value if key existed
*/
virtual std::shared_ptr get(const K &key){
    return head_ref[key];
}
/**
* @brief Execute callback for each entry
*
* Rather than force students to create an inner iterator class,
* this functiona allows a callback function to be executed for
* every item in the associative array. Note that callbacks should
* be called in order of keys sorted least to greatest
*
* Note that keys are passed by const ref; they cannot be changed, yet
* values are passed by non-const ref. They can be changed and that change
* should be reflected in the underlying data structure.
*
* @param callback Function to be called with each item in the associative array
*/
virtual void forEach(std::function callback){
     struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
};
#endif /* KEYVAL_HPP */
Solution/ListKeyVal.hpp
#ifndef LISTKEYVAL_HPP
#define LISTKEYVAL_HPP
#include "KeyVal.hpp"
#include
/*********************************************************************
* This is an example header without any implementation that you
* may find useful in your effort to create an associative array.
*
* You are not required to conform to this header in any way, you are
* only required to conform to the overall interface header provided
* in KeyVal.hpp.
*
* However, there are helpful hints contained here that may make both
* this PEX and future PEXes which build on the base interface easier
* for you to implement. A careful study of this architecture is
* highly recommended. *
********************************************************************/
//forward declaration
template
class ListKeyVal;
/**
* @class ListKeyValNode
*
* The purpose of this class is to store key value pairs in a sorted list.
* Note that all methods and members are private and only accessible by the
* friend class ListKeyVal.
*
* The reason that the ListKeyValNode class and the ListKeyVal class are
* separate is to make usage cleaner. We must be able to create ListKeyVal
* objects on the stack just as we...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here