/*
 * 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++ -*-
//==========================================================================
//
//   connectivity.cc
//
//==========================================================================

#include <GTL_connectivity.h>
#include <list>
#include <vector>

__GTL_BEGIN_NAMESPACE

connectivity::connectivity () : dfs ()
{}

void connectivity::reset () 
{ 
    vector<list<node> > v (0);

    dfs::reset ();
    component_count = 0;
    component_lists.swap(v);
}

int connectivity::check (graph& G) 
{
    return G.is_undirected() &&
	dfs::check(G) == GTL_OK ? GTL_OK : GTL_ERROR;
}
    

//--------------------------------------------------------------------------
//   Handler
//--------------------------------------------------------------------------


void connectivity::init_handler (graph& G) 
{
    vector<list<node> > v (0);

    component_count = 0;
    component_nums.init (G);
    component_lists.swap(v);
}

void connectivity::entry_handler (graph& G, node& curr, node& father)
{
    component_nums[curr] = component_count - 1;
    component_lists.back().push_back(curr);
}

void connectivity::new_start_handler (graph& G, node& n)
{
    component_count++;
    component_lists.push_back(list<node>());
}

__GTL_END_NAMESPACE

//--------------------------------------------------------------------------
//   end of file
//--------------------------------------------------------------------------

