BTW here's a little number I'm working on.... in C++ though.
//datedItems.def
//Written September 24, 2005
//Author: Peter xxxxxx
//A class representing a collection of pairs consisting of a
// date and item objects
#include "datedItems.h"
#include <iostream>
using std::cout;
using std::endl;
//Constructor
//Initializes our datedItems object to an empty collection
template <class Etype>
DatedItems<Etype>::DatedItems() : vectorPairs(), listPairs()
{
//no code required in constructor
}
//addItem
//Parameters: Etype param1: a generic type object that we are attempting to add
// Date param2: a date object which we are attemping to add
//If both parameters are unique, and not found in our collection, we will add
// this pair of of datedItems.
template<class Etype>
void DatedItems<Etype>::addItem(Etype param1, Date param2)
{
bool found=false;
vectorStart = vectorPairs.begin();
vectorFinish = vectorPairs.end();
collectStart = listPairs.begin();
collectFinish = listPairs.end();
pair <Etype, Date> pair1(param1,param2);
vectorFound = findItem (vectorStart, vectorFinish, param2, dateIsEqual);
while (vectorStart!=vectorFinish)
{
if ((!(vectorFound==vectorFinish)) || (vectorStart->first==param1))
{
Warn("addItem: parameters not unique!");
found=true;
vectorStart=vectorFinish;
}
else
{
vectorStart++;
}
}
if (!found)
{
vectorPairs.push_back(pair1);
DateGreater<Etype> dateIsGreater(param2);
collectFound=findFirstSatisfier(collectStart, collectFinish, dateIsGreater
);
listPairs.insert(collectFound,pair1);
}
}
//redateItem
//Parameters: date: old date object which we want to change
// redate: the date which we want to set to the object
//Replaces a date of an item to another one should the old date be found
template <class Etype>
void DatedItems<Etype>::redateItem(Date date, Date redate)
{
collectStart = listPairs.begin();
collectFinish = listPairs.end();
collectFound = collectStart;
collectFound = findItem(collectStart, collectFinish, date, dateIsEqual);
if (collectFound==collectFinish)
{
Warn("redateItem: old date not in collection!");
return;
}
collectFound = findItem(collectStart, collectFinish, redate, dateIsEqual);
if (!(collectFound==collectFinish))
{
Warn("redateItem: new date already in collection!");
return;
}
vectorStart = vectorPairs.begin();
vectorFinish = vectorPairs.end();
vectorFound = findItem(vectorStart, vectorFinish, date, dateIsEqual);
if (!(vectorFound==vectorFinish))
{
vectorFound->second=redate;
}
collectStart = listPairs.begin();
collectFound = findItem(collectStart, collectFinish, date, dateIsEqual);
pair<Etype, Date> pairTemp(collectFound->first, redate);
listPairs.erase(collectFound);
DateGreater<Etype> dateIsGreater(redate);
collectFound = findFirstSatisfier(collectStart, collectFinish, dateIsGreater)
;
listPairs.insert(collectFound, pairTemp);
}
//retrieveItem
//Parameters: paramDate: date for the item we are attempting to retrieve
//Returns a generic item at a given date
//Seeks the current collection for the date given, and returns the item at the
// date
template <class Etype>
Etype DatedItems<Etype>::retrieveItem(Date paramDate)
vectorStart = vectorPairs.begin();
vectorFinish = vectorPairs.end();
vectorFound = findItem(vectorStart, vectorFinish, paramDate, dateIsEqual);
if (vectorFound==vectorFinish)
{
Assert("retrieveItem: date does not exist in collection!");
}
else
{
return vectorFound->first;
}
}
//retrieveDate
//Parameters: A generic item for which the collection will be searched
//Returns a date object at a given item
//Seeks the current collection for the given item and returns the date at that
// given item
template <class Etype>
Date DatedItems<Etype>::retrieveDate(Etype param)
{
vectorStart = vectorPairs.begin();
vectorFinish = vectorPairs.end();
while (vectorStart != vectorFinish)
{
if (param==vectorStart->first)
{
return vectorStart->second;
}
else
{
vectorStart++;
}
}
Assert("retrieveDate: item does not exist in collection!");
}
//numPairs
//This function simply returns the amount of datedItems in our collection
template <class Etype>
int DatedItems<Etype>::numPairs() const
{
return vectorPairs.size();
}
//printShortDates
//This fucntion simply prints out the dates in a specific format in the order
// which they were added
template <class Etype>
void DatedItems<Etype>::printShortDates()
{
vectorStart = vectorPairs.begin();
vectorFinish = vectorPairs.end();
PrintDateWithSlashes<Etype> printSlashes;
print(vectorStart, vectorFinish, printSlashes);
}
//printAll
//This function prints all the datedItems in chornological order
template <class Etype>
void DatedItems<Etype>::printAll()
{
collectStart = listPairs.begin();
collectFinish = listPairs.end();
PrintDateWithMonthName<Etype> printMonthName;
while (collectStart != collectFinish)
{
printMonthName(*collectStart);
cout<<collectStart->first<<endl;
collectStart++;
}
}