|
|
COT 5405 Advanced Algorithms Chris Lacher Notes 6: Associative ADTs: Set, Map, Table, AssociativeArray |
Abstract Container Types Container P|A Iterator Distinguishing Operations Vector P RandomAccess PushBack, PopBack, SetSize, Indexed BracketOperator List P BiDirectional PushFront, PopFront, PushBack, PopBack, Insert, Remove Deque P RandomAccess PushFront, PopFront, PushBack, PopBack, Indexed BracketOperator Stack P Push, Pop, Top Queue P Push, Pop, Front PriorityQueue A Push, Pop, Front OUSet A BiDirectional Ordered UniSet: Insert, Remove, Includes OMSet A BiDirectional Ordered MultiSet: Insert, Remove, Includes UUSet A BiDirectional Unordered UniSet: Insert, Remove, Includes UMSet A BiDirectional Unordered MultiSet: Insert, Remove, Includes AssociativeArray A BiDirectional Unordered Associative Bracket Operator For each operation: behavior specification, runtime constraint, runspace constraint
For each container: reason(s) to select it over the other choices
//--------------------------
// class Set<T,C>
//--------------------------
template < typename T , class C >
class Set
{
public:
// Access support: SetIterator
// Terminology support: ValueType, Iterator
// Proper type: constructors, destructors, assignment
// element operations - modality determined by C
Iterator Insert (const ValueType& t);
size_t Remove (const ValueType& t);
// locator operations
Iterator Includes (const ValueType& t) const;
Iterator Begin () const;
Iterator End () const;
Iterator rBegin () const;
Iterator rEnd () const;
// size operations
int Empty () const;
size_t Size () const;
protected:
C c;
} ; // class Set
//----------------------------------
// class SetIterator<T,C>
//----------------------------------
template <typename T , class C>
class SetIterator // ConstIterator
{
public:
// Access support: Set
// Terminology support: ValueType
// Proper type: constructors, destructors, assignment
// operators
int operator == (const SetIterator<T,C>& I2) const;
int operator != (const SetIterator<T,C>& I2) const;
const ValueType& operator * () const; // Return reference to current value
SetIterator<T,C>& operator = (const SetIterator <T,C> & I);
SetIterator<T,C>& operator ++ (); // prefix
SetIterator<T,C> operator ++ (int); // postfix
SetIterator<T,C>& operator -- (); // prefix
SetIterator<T,C> operator -- (int); // postfix
protected:
typename C::Iterator i;
} ; // class SetIterator
for (C::Iterator i = c.Begin(); i != c.End(); ++i)
{
// any code in the body
}
Data& operator [] (const Key& k);
operation Associative Container
--------- ---------------------------------------------------------------
OUSet OMSet UUSet UMSet OUMap OMMap UUMap UMMap
Insert(t) log n log n AO(1) AO(1)
Insert(k,d) log n log n AO(1) AO(1)
Remove(t) log n log n AO(1) AO(1)
Remove(k) log n log n AO(1) AO(1)
Includes(t) log n log n AO(1) AO(1)
Includes(k) log n log n AO(1) AO(1)
Includes(k,d) log n log n AO(1) AO(1)
--------------
AO(1) = amortized constant runtime,
depends on hash function and internal sizing parameters
-----------------------------------------------------------------------------
Iterator support: iterators are Bidirectional ConstIterators
AssociativeArrayPriorityQueue