/*
 * Copyright (c) 1999 Johnny C. Lam.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 */

// -*- C++ -*-

template <class T, class W>
template <class InputIterator>
void
stl_graph<T,W>::add_node_range(const stl_graph<T,W>& g,
			       InputIterator f, InputIterator l,
			       node_map<node>* node_translate)
{
    InputIterator i = f;
    while (i != l) {
	(*node_translate)[*i] = add_node(g.value(*i));
	++i;
    }
    
    while (f != l) {
	node::adj_edges_iterator e = f->adj_edges_begin();
	node::adj_edges_iterator end = f->adj_edges_end();
	while (e != end) {
	    node n = (*node_translate)[e->target()];
	    if (n != node())
		add_edge((*node_translate)[*f], n, g.weight(*e));
	    ++e;
	}
	++f;
    }
}

template <class T, class W>
template <class InputIterator>
void
stl_graph<T,W>::add_node_range(const stl_graph<T,W>& g,
			       InputIterator f, InputIterator l)
{
    node_map<node> node_translate(g.topology());
    add_node_range(g, f, l, &node_translate);
}

template <class T, class W>
template <class InputIterator>
void
stl_graph<T,W>::add_edge_range(const stl_graph<T,W>& g,
			       InputIterator f, InputIterator l,
			       node_map<node>* node_translate)
{
    while (f != l) {
	const node &from = f->source();
	const node &to = f->target();
	if ((*node_translate)[from] == node())
	    (*node_translate)[from] = add_node(g.value(from));
	if ((*node_translate)[to] == node())
	    (*node_translate)[to] = add_node(g.value(to));
	add_edge((*node_translate)[from], (*node_translate)[to], g.weight(*f));
	++f;
    }
}

template <class T, class W>
template <class InputIterator>
void
stl_graph<T,W>::add_edge_range(const stl_graph<T,W>& g,
			       InputIterator f, InputIterator l)
{
    node_map<node> node_translate(g.topology());
    add_edge_range(g, f, l, &node_translate);
}

template <typename T, typename W>
node
stl_graph<T,W>::find_node(const value_type& data) const
{
    const node_map<value_type>& nd = _node_data;
    graph::node_iterator n = _topology.nodes_begin();
    graph::node_iterator end = _topology.nodes_end();
    while( n != end ) {
	if (nd[*n] == data) {
	    return *n;
	}
	++n;
    }
    return node();
}
