/*
 * 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.
 */

#include "path.h"

#include <algorithm>
#include <iterator>

void
Path::Write(ostream& ostr) const
{
    ostr << "Path:" << endl;
    const_iterator f = _value->edges.begin();
    const_iterator end = _value->edges.end();
    while (f != end) {
	ostr << "\t" << f->haplotype << ", " << f->weight << endl;
	++f;
    }
    ostr << "End Path.";
}

void
Path::Merge(Path& rhs)
{
    assert(To() == rhs.From());
    
    container_type& lhs_edges(_value->edges);
    container_type& rhs_edges(rhs._value->edges);

    if (rhs.size() == 0)
	return;

    if (_value->is_shared())
	_value = new PathValue(*_value);
    
    if (rhs._value->is_shared())
	rhs._value = new PathValue(*rhs._value);

    container_type::iterator first = rhs_edges.begin();
    ++first;

    lhs_edges.splice(lhs_edges.end(), rhs_edges,
		     rhs_edges.begin(), rhs_edges.end());
}

Path&
Path::operator+=(const Path& rhs)
{
    assert(To() == rhs.From());

    if (rhs.size() == 0)
	return *this;

    if (_value->is_shared())
	_value = new PathValue(*_value);

    typename container_type::const_iterator f = rhs._value->edges.begin();
    typename container_type::const_iterator end = rhs._value->edges.end();
    ++f;
    copy(f, end, back_inserter(_value->edges));

    return *this;
}
