I have to write an ArrayDictionary.cpp
file that follows the ArrayDictionary.h
file listed below. The purpose of the .h
file is an array-based implementation of the ADT dictionary that organizes its data items in sorted search-key order. Search keys in the dictionary are unique.
The code includes ArrayDictionary.h
and ArrayDictionary.cpp
. The ArrayDictionary.h
file also includes other .h
files. Please let me know if you need them to help me.
I'm having trouble with variables that cannot be identify in the ArrayDicionary.cpp
file. The variables are items and entryCount that are having errors.
//ArrayDictionary.h
#pragma once
#ifndef _ARRAY_DICTIONARY
#define _ARRAY_DICTIONARY
#include "DictionaryInterface.h"
#include "Entry.h"
#include "NotFoundException.h"
#include "PrecondViolatedExcept.h"
template < class KeyType, class ValueType>
class ArrayDictionary : public DictionaryInterface < KeyType, ValueType>
{
private:
static const int DEFAULT_CAPACITY = 21; // Small capacity to test for
// a full dictionary
Entry<KeyType, ValueType>* entries; // Array of dictionary entries
int entryCount; // Maximum capacity of the dictionary
void destroyDictionary();
int findEntryIndex(int firstIndex, int lastIndex, const KeyType & searchKey) const;
int maxEntries;
public:
ArrayDictionary();
ArrayDictionary(int maxNumberOfEntries);
ArrayDictionary(const ArrayDictionary<KeyType, ValueType>&dictionary);
virtual ~ArrayDictionary();
bool isEmpty() const;
int getNumberOfEntries() const;
bool add(const KeyType& searchKey, const ValueType& newValue) throw (PrecondViolatedExcep);
bool remove(const KeyType& searchKey);
void clear();
ValueType getValue(const KeyType& searchKey) const throw (NotFoundException);
bool contains(const KeyType& searchKey) const;
/** Traverses the items in this dictionary in sorted search-key order
and calls a given client function once for each item. */
void traverse(void visit(ValueType&)) const;
}; // end ArrayDictionary
#include "ArrayDictionary.cpp"
#endif
#include <iostream>
#include "ArrayDictionary.h"
#include "PrecondViolatedExcept.h"
template < class KeyType, class ValueType>
void ArrayDictionary<KeyType, ValueType>::destroyDictionary()
{
delete[] items;
items = new Entry[maxEntries];
entryCount = 0;
}
template < class KeyType, class ValueType>
inline int findEntryIndex(int firstIndex, int lastIndex, const KeyType& searchKey)
{
int IndexMiddle = firstIndex + (lastIndex - firstIndex) / 2;
if (firstIndex > lastIndex)
return -1;
else if (searchKey == items[IndexMiddle].getKey())
return IndexMiddle;
else if (searchKey < items[IndexMiddle].getKey())
return findEntryIndex(firstIndex, IndexMiddle - 1, searchKey);
else
return findEntryIndex(IndexMiddle + 1, lastIndex, searchKey);
}
template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType, ValueType>::ArrayDictionary() : entryCount(0), maxEntries(DEFAULT_CAPACITY)
{
items = new Entry[DEFAULT_CAPACITY];
}
template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType,ValueType>::ArrayDictionary(int maxNumberOfEntries) :
entryCount(0), maxEntries(maxNumberOfEntries)
{
items = new Entry[maxNumberOfEntries];
}
template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType,ValueType>::ArrayDictionary(const ArrayDictionary& dictionary) :
entryCount(dictionary.itemCount), maxEntries(dictionary.maxEntries)
{
items = new Entry[dictionary.maxEntries];
for (int index = 0; index < dictionary.entryCount; index++)
{
items[index] = dictionary.items[index];
}
}
template < class KeyType, class ValueType>
inline ArrayDictionary<KeyType, ValueType>::~ArrayDictionary()
{
destroyDictionary();
}
template < class KeyType, class ValueType>
inline bool isEmpty()
{
return (entryCount == 0);
}
template < class KeyType, class ValueType>
inline int getNumberOfItems()
{
return entryCount;
}
template < class KeyType, class ValueType>
inline void ArrayDictionary<KeyType, ValueType>:: clear()
{
destroyDictionary();
}
template < class KeyType, class ValueType>
inline bool ArrayDictionary<KeyType, ValueType>::add(const KeyType& searchKey, const ValueType& newValue) throw (PrecondViolatedExcep)
{
bool ableToInsert = (entryCount < maxEntries);
if (ableToInsert)
{
// Make room for new entry by shifting all entries at
// positions >= newPosition toward the end of the array
// (no shift if newPosition == itemCount + 1). Performing
// a binary search doesn’t help here, because we need to
// shift the entries while looking for the insertion location.
int index = entryCount;
// Short-circuit evaluation is important
while ((index > 0) && (searchKey < items[index - 1].getKey()))
{
items[index] = items[index - 1];
index--;
} // end while
if (searchKey != items[index - 1].getKey())
{
items[index] = Entry<KeyType, ValueType>(searchKey, newValue);
entryCount++;
}
else
{
auto message = "Attempt to add entry whose search key exits in dictionary.";
throw (PrecondViolatedExcep(message);
}
return ableToInsert;
} // end add
}
template < class KeyType, class ValueType>
inline bool ArrayDictionary<KeyType, ValueType>:: remove(const const KeyType& itemKey)
{
int currentIndex = findEntryIndex(0, itemCount - 1, itemKey);
bool deletable = !isEmpty() && (currentIndex >= 0);
if (deletable)
{
while (currentIndex < entryCount - 1)
{
items[currentIndex] = items[currentIndex + 1];
currentIndex++;
}
itemCount--;
}
return deletable;
}
template < class KeyType, class ValueType>
inline ValueType getValue(const KeyType& searchKey) throw(NotFoundException)
{
int currentIndex = findEntryIndex(0, itemCount - 1, searchKey);
if (currentIndex < 0)
throw NotFoundException("nnItemis not in the Dictionary!nn");
return items[currentIndex].getItem();
}
template < class KeyType, class ValueType>
inline bool contains(const KeyType& searchKey)
{
return (findEntryIndex(0, entryCount - 1, itemKey) >= 0)
}
template < class KeyType, class ValueType>
inline void traverse(void visit(ValueType&))
{
for (int itr = 0; itr < entryCount; itr++)
{
ValueType currentItem = items[itr].getItem();
visit(currentItem);
}
}