CSCE 221-(507-509)
Programing Assignment 2
Overview MySTL Testing Todos
 All Classes Namespaces Files Functions Variables Typedefs Friends Groups Pages
List of all members | Classes
mystl::map< Key, Value > Class Template Reference

Map ADT based on C++ map implemented with binary search tree. More...

Classes

class  map_iterator
 Forward declare node class. More...
 
struct  node
 Internal structure for binary search tree. More...
 

Public Member Functions

Constructors
 map ()
 Constructor. More...
 
 map (const map &m)
 Copy constructor. More...
 
 ~map ()
 Destructor. More...
 
mapoperator= (const map &m)
 Copy assignment. More...
 
Iterators
iterator begin ()
 
iterator end ()
 
reverse_iterator rbegin ()
 
reverse_iterator rend ()
 
const_iterator cbegin () const
 
const_iterator cend () const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
Capacity
size_t size () const
 
bool empty () const
 
Element Access
Value & operator[] (const Key &k)
 
Value & at (const Key &k)
 
const Value & at (const Key &k) const
 
Modifiers
std::pair< iterator, bool > insert (const value_type &v)
 Insert element into map, i.e., put(k, v) from the Map ADT. More...
 
iterator erase (const_iterator position)
 Remove element at specified position. More...
 
size_t erase (const Key &k)
 Remove element at specified position. More...
 
void clear () noexcept
 Removes all elements. More...
 
Operations
iterator find (const Key &k)
 Search the container for an element with key k. More...
 
const_iterator find (const Key &k) const
 Search the container for an element with key k. More...
 
size_t count (const Key &k) const
 Count elements with specific keys. More...
 

Public Types

Types
typedef Key key_type
 Public access to Key type. More...
 
typedef Value mapped_type
 Public access to Value type. More...
 
typedef std::pair< const
key_type, mapped_type
value_type
 Entry type. More...
 
typedef map_iterator< value_typeiterator
 Bidirectional iterator. More...
 
typedef map_iterator< const
value_type
const_iterator
 Const bidirectional iterator. More...
 
typedef std::reverse_iterator
< iterator
reverse_iterator
 Reverse bidirectional iterator. More...
 
typedef std::reverse_iterator
< const_iterator
const_reverse_iterator
 Const reverse bidirectional iterator. More...
 

Private Member Functions

Helpers
nodefinder (const Key &k) const
 Utility for finding a node with Key k. More...
 
std::pair< node *, bool > inserter (const value_type &v)
 Utility for inserting a new node into the data structure. More...
 
nodeeraser (node *n)
 Erase a node from the tree. More...
 

Private Attributes

Data
noderoot
 
size_t sz
 Number of nodes. More...
 

Detailed Description

template<typename Key, typename Value>
class mystl::map< Key, Value >

Map ADT based on C++ map implemented with binary search tree.

Template Parameters
KeyKey type
ValueValue type

Assumes the following: There is always enough memory for allocations (not a good assumption, just good enough for our purposes); Functions not well-defined on an empty container will exhibit undefined behavior.

Member Typedef Documentation

template<typename Key , typename Value >
typedef Key mystl::map< Key, Value >::key_type

Public access to Key type.

template<typename Key , typename Value >
typedef Value mystl::map< Key, Value >::mapped_type

Public access to Value type.

template<typename Key , typename Value >
typedef std::pair<const key_type, mapped_type> mystl::map< Key, Value >::value_type

Entry type.

template<typename Key , typename Value >
typedef map_iterator<value_type> mystl::map< Key, Value >::iterator

Bidirectional iterator.

template<typename Key , typename Value >
typedef map_iterator<const value_type> mystl::map< Key, Value >::const_iterator

Const bidirectional iterator.

template<typename Key , typename Value >
typedef std::reverse_iterator<iterator> mystl::map< Key, Value >::reverse_iterator

Reverse bidirectional iterator.

template<typename Key , typename Value >
typedef std::reverse_iterator<const_iterator> mystl::map< Key, Value >::const_reverse_iterator

Const reverse bidirectional iterator.

Constructor & Destructor Documentation

template<typename Key , typename Value >
mystl::map< Key, Value >::map ( )

Constructor.

template<typename Key , typename Value >
mystl::map< Key, Value >::map ( const map< Key, Value > &  m)

Copy constructor.

Parameters
mOther map
template<typename Key , typename Value >
mystl::map< Key, Value >::~map ( )

Destructor.

Member Function Documentation

template<typename Key , typename Value >
map& mystl::map< Key, Value >::operator= ( const map< Key, Value > &  m)

Copy assignment.

Parameters
mOther map
Returns
Reference to self
template<typename Key , typename Value >
iterator mystl::map< Key, Value >::begin ( )
Returns
Iterator to beginning
template<typename Key , typename Value >
iterator mystl::map< Key, Value >::end ( )
Returns
Iterator to end
template<typename Key , typename Value >
reverse_iterator mystl::map< Key, Value >::rbegin ( )
Returns
Iterator to reverse beginning
template<typename Key , typename Value >
reverse_iterator mystl::map< Key, Value >::rend ( )
Returns
Iterator to reverse end
template<typename Key , typename Value >
const_iterator mystl::map< Key, Value >::cbegin ( ) const
Returns
Iterator to beginning
template<typename Key , typename Value >
const_iterator mystl::map< Key, Value >::cend ( ) const
Returns
Iterator to end
template<typename Key , typename Value >
const_reverse_iterator mystl::map< Key, Value >::crbegin ( ) const
Returns
Iterator to reverse beginning
template<typename Key , typename Value >
const_reverse_iterator mystl::map< Key, Value >::crend ( ) const
Returns
Iterator to reverse end
template<typename Key , typename Value >
size_t mystl::map< Key, Value >::size ( ) const
Returns
Size of map
template<typename Key , typename Value >
bool mystl::map< Key, Value >::empty ( ) const
Returns
Does the map contain anything?
template<typename Key , typename Value >
Value& mystl::map< Key, Value >::operator[] ( const Key &  k)
Parameters
kInput key
Returns
Value at given key

If k matches an element in the container, the function will return a reference to its mapped value.

If k is not found in the container, the function should insert a new element with that key and return a reference to its mapped value (constructed through default construction)

Todo:
implement at function. Utilize inserter function.
template<typename Key , typename Value >
Value& mystl::map< Key, Value >::at ( const Key &  k)
Parameters
kInput key
Returns
Value at given key

If k matches an element in the container, the function will return a reference to its mapped value.

If k is not found in the container, the function throws an out_of_range exception.

Todo:
implement at function. Utilize find function.
template<typename Key , typename Value >
const Value& mystl::map< Key, Value >::at ( const Key &  k) const
Parameters
kInput key
Returns
Value at given key

If k matches an element in the container, the function will return a reference to its mapped value.

If k is not found in the container, the function throws an out_of_range exception.

Todo:
implement at function. Utilize find function.
template<typename Key , typename Value >
std::pair<iterator, bool> mystl::map< Key, Value >::insert ( const value_type v)

Insert element into map, i.e., put(k, v) from the Map ADT.

Parameters
vKey, Value pair
Returns
pair of iterator and bool. Iterator pointing to found element or already existing element. bool is true if a new element was inserted and false if it existed.

Insert is the "put(k, v)" function of the Map ADT. Remember that Maps store unique elements, so if the element existed already it is returned.

Todo:
Implement erase. Utilize inserter helper.
template<typename Key , typename Value >
iterator mystl::map< Key, Value >::erase ( const_iterator  position)

Remove element at specified position.

Parameters
positionPosition
Returns
Position of new location of element which was after eliminated one
Todo:
Implement erase. Utilize eraser helper.
template<typename Key , typename Value >
size_t mystl::map< Key, Value >::erase ( const Key &  k)

Remove element at specified position.

Parameters
kKey
Returns
Number of elements removed (in this case it is at most 1)
Todo:
Implement erase. Utilize finder and eraser helpers.
template<typename Key , typename Value >
void mystl::map< Key, Value >::clear ( )
noexcept

Removes all elements.

template<typename Key , typename Value >
iterator mystl::map< Key, Value >::find ( const Key &  k)

Search the container for an element with key k.

Parameters
kKey
Returns
Iterator to position if found, end() otherwise
Todo:
Implement find. Utilize the finder helper.
template<typename Key , typename Value >
const_iterator mystl::map< Key, Value >::find ( const Key &  k) const

Search the container for an element with key k.

Parameters
kKey
Returns
Iterator to position if found, cend() otherwise
Todo:
Implement find. Utilize the finder helper
template<typename Key , typename Value >
size_t mystl::map< Key, Value >::count ( const Key &  k) const

Count elements with specific keys.

Parameters
kKey
Returns
Count of elements with key k

Because all elements in a map container are unique, the function will only return 1 or 0.

Todo:
Implement count. Utilize the find operation.
template<typename Key , typename Value >
node* mystl::map< Key, Value >::finder ( const Key &  k) const
private

Utility for finding a node with Key k.

Parameters
kKey
Returns
Node pointer to either where node exists or where it should be inserted

Base your algorithm off of Code Fragment 10.9 on page 436

Todo:
Implement finder helper function
template<typename Key , typename Value >
std::pair<node*, bool> mystl::map< Key, Value >::inserter ( const value_type v)
private

Utility for inserting a new node into the data structure.

Parameters
vKey, Value pair
Returns
pair of node and bool. node pointing to found element or already existing element. bool is true if a new element was inserted and false if it existed.

Inserter is the "put(k, v)" function of the Map ADT. Remember that Maps store unique elements, so if the element existed already it is returned.

Base you algorithm off of Code Fragment 10.10 on page 436

Hint: Will need to use functions node::replace and node::expand

Todo:
Implement inserter helper function
template<typename Key , typename Value >
node* mystl::map< Key, Value >::eraser ( node n)
private

Erase a node from the tree.

Parameters
nNode to erase
Returns
Next inorder successor of n in tree

Base your algorithm off of Code Fragment 10.11 on page 437

Hint: will need to use functions node::inorder_next, node::replace, and node::remove_above_external

Todo:
Implement eraser helper function

Member Data Documentation

template<typename Key , typename Value >
node* mystl::map< Key, Value >::root
private

Root of binary tree, the root will be a sentinel node for end iterator. root.left is the "true" root for the data

template<typename Key , typename Value >
size_t mystl::map< Key, Value >::sz
private

Number of nodes.


The documentation for this class was generated from the following file: