
/*
 * Copyright (C) 2002 - 2007 Lehrstuhl Grafische Systeme, BTU Cottbus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package de.grogra.c4d.model;

import java.io.Serializable;

import de.grogra.c4d.BaseDocument;
import de.grogra.c4d.BaseObject;
import de.grogra.c4d.Constants;
import de.grogra.reflect.Type;
import de.grogra.xl.impl.base.GraphImpl;
import de.grogra.xl.impl.base.GraphQueue;
import de.grogra.xl.impl.base.GraphQueueImpl;
import de.grogra.xl.impl.queues.QueueCollection;
import de.grogra.xl.impl.queues.QueueDescriptor;
import de.grogra.xl.query.EdgeDirection;
import de.grogra.xl.query.MatchConsumer;
import de.grogra.xl.query.Producer;
import de.grogra.xl.query.QueryState;
import de.grogra.xl.util.ObjectList;

public class Graph extends GraphImpl
{
	private final ObjectList<BaseObject> siblings = new ObjectList<BaseObject> ();


	public Graph (RuntimeModel runtime)
	{
		super (runtime);
	}


	public boolean canEnumerateEdges (EdgeDirection dir, boolean constEdge, Serializable edge)
	{
		return true;
	}

	@Override
	public GraphQueue createQueue (QueueCollection qc, QueueDescriptor descr)
	{
		return new GraphQueueImpl (descr, qc)
		{
			@Override
			protected int maskConnectionEdges (int edges)
			{
				return edges & RuntimeModel.CHILD;
			}
		};
	}


	@Override
	protected void beginModifications ()
	{
		siblings.clear ();
	}


	@Override
	protected void commitModifications ()
	{
		boolean changed;
		do
		{
			changed = false;
			for (int i = siblings.size () - 2; i >= 0; i -= 2)
			{
				if (C4D.addEdgeBits(siblings.get (i), siblings.get (i + 1), RuntimeModel.SIBLING))
				{
					changed = true;
					siblings.remove (i + 1);
					siblings.remove (i);
				}
			}
		} while (changed);
	}

	public void enumerateNodes (Type type, QueryState qs, int index, MatchConsumer consumer, int arg)
	{
		BaseDocument s = C4D.getCurrentDocument();
		BaseObject o = s.getFirstObject();
		if (o == null)
		{
			return;
		}
		long[] nodes = C4D.enumerateNodes(s.getPointer(), o.getHandle(), true, Constants.Obase, 0);
		for (int i = 0; i < nodes.length; i++)
		{
			BaseObject n = s.getBaseObject(nodes[i]);
			if (type.isInstance (n))
			{
				qs.amatch (index, n, consumer, arg);
			}
		}
	}

	public Object getRoot ()
	{
		return C4D.getCurrentRoot();
	}

	@Override
	public void addNode (Object node)
	{
	}


	@Override
	public void addEdgeBits (Object source, Object target, int bits)
	{
		if (!C4D.addEdgeBits((BaseObject) source, (BaseObject) target, bits))
		{
			siblings.push ((BaseObject) source).push ((BaseObject) target);
		}
	}


	@Override
	public void removeEdgeBits (Object source, Object target, int bits)
	{
		C4D.removeEdgeBits((BaseObject) source, (BaseObject) target, bits);
	}

	public Producer createProducer (QueryState match)
	{
		return new C4DProducer (match);
	}
}
