/*
 * Copyright (C) 2015 by the University of Southern California
 * $Id: safe_map.cc 23974 2015-05-13 09:18:26Z liangzhu $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.                                      
 *
 * You should have received a copy of the GNU General Public License along           
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
 * 
 */

#include "safe_map.hh"
#include <iostream>
using namespace std;

template<typename T, typename S>
safe_map<T, S>::safe_map()
{
  
}

template<typename T, typename S>
safe_map<T, S>::~safe_map()
{
  
}

template<typename T, typename S>
bool safe_map<T, S>::find(T x)
{
  unique_lock<mutex> lk (mtx);
  return m.find(x) != m.end();
}

template<typename T, typename S>
void safe_map<T, S>::erase(T x)
{
  unique_lock<mutex> lk (mtx);
  m.erase(x);
}

template<typename T, typename S>
bool safe_map<T, S>::empty()
{
  unique_lock<mutex> lk (mtx);
  return m.empty();
}

template<typename T, typename S>
void safe_map<T, S>::push(T x, S y)
{
  unique_lock<mutex> lk (mtx);
  m[x] = y;
}

template<typename T, typename S>
S safe_map<T, S>::get(T x)
{
  unique_lock<mutex> lk (mtx);
  return m[x];
}

template<typename T, typename S>
S safe_map<T, S>::pop_second()
{
  unique_lock<mutex> lk (mtx);
  if (m.empty())
    return 0;
  auto it = m.begin();
  auto rv = it->second;
  m.erase(it->first);
  return rv;
}

template<typename T, typename S>
T safe_map<T, S>::pop_first()
{
  unique_lock<mutex> lk (mtx);
  if (m.empty())
    return 0;
  auto it = m.begin();
  auto rv = it->first;
  m.erase(it->first);
  return rv;
}

template class safe_map<uint16_t, client_t *>;
