/*
 * CoreAttributesTreeIterator - TaskJuggler
 *
 * Copyright (c) 2001, 2002, 2003, 2004 by Chris Schlaeger <cs@suse.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * $Id: CoreAttributesTreeIterator.cpp 654 2004-03-13 14:57:34Z cs $
 */

#include "CoreAttributesTreeIterator.h"
#include "CoreAttributes.h"
#include "CoreAttributesList.h"

CoreAttributesTreeIterator::CoreAttributesTreeIterator(CoreAttributes* r,
                                                       IterationMode m)
{
    root = current = r;
    iMode = m;
    while (current->hasSubs())
        current = current->getSubList().getFirst();
}

CoreAttributes*
CoreAttributesTreeIterator::operator++()
{
    if (current == 0)
        return 0;

    while (current != root)
    {
        // Find the current CA in the parent's sub list.
        CoreAttributesListIterator
            cli(current->getParent()->getSubListIterator());
        for ( ; *cli != current; ++cli)
            ;
        // Check if there is another task in the sub list.
        ++cli;
        if (*cli != 0)
        {
            // Find the first leaf in this sub list.
            current = *cli;
            while (current->hasSubs())
                current = current->getSubList().getFirst();
            // This is the new current task.
            return current;
        }
        // End of sub list reached. Try parent node then.
        current = current->getParent();
        if (iMode == parentAfterLeaves)
            return current;
    }
    return (current = 0);
}

