#include <stdio.h>
#include <string.h>
#include "algorithm.h"
#include "functional.h"
#include "map.h"

#include "map.c++"
#include "hoistbp.c++"

char key[10] =
{
  '0',
  '1',
  '2',
  '3',
  '4',
  '5',
  '6',
  '7',
  '8',
  '9',
};

void print_elem(map_value_type<char, char> const& elem)
{
  printf("'%c': %i\n", elem.first, elem.second);
}

int main()
{
  map<char, char, less<char> > test_map;

  int i;

  for(i = 0; i < 10; ++i)
    test_map[key[i]] = i;

  for(i = 0; i < 10; ++i)
    printf("'%c': %i\n", key[i], test_map[key[i]]);

  printf("\n");

  print_elem(*test_map.find('3'));

  map<char, char, less<char> > const& const_map = test_map;
  print_elem(*const_map.find('3'));

  try
  {
    map<char, char, less<char> > const& const_map = test_map;
    int not_there = const_map['?'];
  }
  BEGIN_HANDLERS
  catch(out_of_range, ex)
  {
    printf("Exception caught: %s\n\n", ex.what());
  }
  END_HANDLERS


  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");


  for_each(test_map.rbegin(), test_map.rend(), print_elem);
  printf("\n");

  map_iterator<char, char, less<char> > j = test_map.end();

  print_elem(*--j);
  printf("\n");

  print_elem(*--j);
  printf("\n");

  print_elem(*--j);
  printf("\n");

  map<char, char, less<char> > m2(test_map);

  for_each(m2.begin(), m2.end(), print_elem);
  printf("\n");

  printf("test_map == m2 %i\n", test_map == m2);
  printf("\n");

  m2['0'] = 1;
  for_each(m2.begin(), m2.end(), print_elem);
  printf("\n");

  printf("test_map == m2 %i\n", test_map == m2);
  printf("\n");

  test_map.erase(test_map.find('5'), test_map.find('7'));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");

  test_map.erase(test_map.find('8'));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");

  test_map.erase(test_map.find('0'));
  for_each(test_map.begin(), test_map.end(), print_elem);
  printf("\n");
};

