0 Replies - 162 Views - Last Post: 03 December 2009 - 11:56 AM

#1 Locke   User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 552
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

A Simple Map (Parallel Arrays)

Posted 03 December 2009 - 11:56 AM

Description: You can create an instance using a simple template declaration...Map<type, type> and supply an input file (opened prior to supplying it to the constructor), or simply provide arrays (and their length) to copy into the Map.This is a class for holding data that maps to another piece of data. For example...zip code to city name, movie name to movie length, etc.
////////////////////// Map.H \\\\\\\\\\\\

#ifndef MAP_H
#define MAP_H



    #include <fstream>

    template<typename From, typename To>
    
    /**
        The map class is used for coordinating data that should be kept in parallel.  It holds the data in two arrays.
    */
    class Map
    {
        public:

            /**
                Constructor to initialize with given arrays.
                
                @param f (The array of 'From' objects.)
                @param t (The array of 'To' objects.)
                @param length (The length of the arrays.)
            */
            Map(From f[], To t[], int length);


            /**
                Constructor to intialize by reading from a file.
                
                @param inFile (The file to read from.)
                
                @pre The file should be opened prior to calling the constructor.
            */

            Map(std::ifstream& inFile);

            /**
                Copy Constructor.
                
                @param map (The map to copy.)
            */
            Map(const Map<From, To>& map);

            /**
                Destructor.
            */
            ~Map();

            /**
                Method to return the length of the arrays.
                
                @return The size of the map.
            */
            int getNumEntries() const;

            /**
                Method to get entries at a specific index, and use references to get them.
                
                @param i (The index to retrieve.)
                @param f (The 'From' object to edit for the reference.)
                @param t (The 'To' object to edit for the reference.)
                
                @throw logic_error (If the index is less than zero, or greater than the size.)
            */
            void getEntry(int i, From& f, To& t) const;

            /**
                Overloading of the [] operator, to use the for programmer's convenience.
                
                @param parameter (The 'From' object to look up in the map.)
                
                @return The 'To' value from the map that corresponds to the parameter.
            */
            To operator [] (const From& parameter);

        private:

            /**
                The pointer (array) to hold the 'From' mappings.
            */
            From *from;
            
            /**
                The pointer (array) to hold the 'To' mappings.
            */
            To *to;
            
            /**
                The size of the map.
            */
            int size;
    };

    #include "Map.cpp"

#endif


////////////////////// Map.CPP \\\\\\\\\\\\

#include <fstream>

#include <stdexcept>


using namespace std;


template<typename From, typename To>
Map<From, To> :: Map(From f[], To t[], int length)
{
    from = new From[length];
    to = new To[length];

    size = length;

    for (unsigned int x = 0; x < length; x++)
    {
        from[x] = f[x];
        to[x] = t[x];
    }
}



template<typename From, typename To>

Map<From, To> :: Map(ifstream& inFile)

{

    inFile >> size;



    from = new From[size];

    to = new To[size];



    for (unsigned int x = 0; x < size; x++)

    {

        inFile >> from[x];

        inFile >> to[x];

    }

}

template<typename From, typename To>
Map<From, To> :: Map(const Map<From, To>& map)
{
    from = new From[map.size];
    to = new To[map.size];

    size = map.size;

    for (unsigned int x = 0; x < size; x++)
    {
        from[x] = map.from[x];
        to[x] = map.to[x];
    }
}

template<typename From, typename To>
Map<From, To> :: ~Map()
{
    delete[] from;
    delete[] to;
}

template<typename From, typename To>
int Map<From, To> :: getNumEntries() const
{
    return size;
}

template<typename From, typename To>
void Map<From, To> :: getEntry(int i, From& f, To& t) const
{
    if (i < 0 || i > size)
        throw std::logic_error("getEntry() -- Index out of bounds.");

    f = from[i];
    t = to[i];
}

template<typename From, typename To>
To Map<From, To> :: operator [] (const From& f)
{
    for (unsigned int x = 0; x < size; x++)
        if (f == from[x])
            return to[x];



    throw runtime_error("No such element in the Map.");
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1