#include "nameserverinterface.h"
#include "vectornameserver.h"
#include <algorithm>
#include <vector>

using namespace std;

namespace cpp_lab4 {
  void VectorNameServer::insert(const HostName &hostname, const IPAddress &ip) {
    store.push_back(pair<HostName, IPAddress>(hostname, ip));
  }

  bool VectorNameServer::remove(const HostName &hostname) {
    vector<pair<HostName,IPAddress> >::iterator it;
    it = find_if(store.begin(), store.end(), bind2nd(first_equal(), hostname));
    if (it != store.end()) {
      store.erase(it);
      return true;
    }
    return false;
  }

  IPAddress VectorNameServer::lookup(const HostName &hostname) const {
    IPAddress result = NON_EXISTING_ADDRESS;

    vector<pair<HostName,IPAddress> >::iterator it;
    it = find_if(store.begin(), store.end(), bind2nd(first_equal(), hostname));
    if (it != store.end()) {
      result = (*it)->second;
    }

    return result;
  }
}
