/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#include "pgl/pgl.h"

#include "base/StringArray.h"
#include "pgl/PglBoolSym.h"
#include "pgl/PglStringSym.h"
#include "pgl/PglContainerSym.h"



String ContainerSym::TheType = "Container";



bool ContainerSym::isA(const String &type) const {
	return ExpressionSym::isA(type) || type == TheType;
}

const SynSym *ContainerSym::item(int offset) const {
	double h;
	return itemProb(offset, h);
}

ExpressionSym *ContainerSym::bnOper(const Oper &op, const SynSym &exp) const {
	if (!op.equality())
		return ExpressionSym::bnOper(op, exp);

	if (!exp.isA(TheType))
		return ExpressionSym::bnOper(op, exp);

	const ContainerSym &them = (const ContainerSym&)exp.cast(TheType);

	const int thisCount = count();
	const int themCount = them.count();
	bool same = thisCount == themCount;

	for (int i = 0; same && i < thisCount; ++i) {
		const SynSym *s = item(i);
		if (s->isA(ExpressionSym::TheType)) {
			const ExpressionSym &thisE = 
				(const ExpressionSym &)s->cast(ExpressionSym::TheType);
			same = thisE.bnOper(Oper::Same(), *them.item(i));
		} else {
			// cannot compare non-expression items on equality,
			// but note that different counts will yeild a result (earlier)
			return ExpressionSym::bnOper(op, exp);
		}
	}

	if (op.same())
		return new BoolSym(same);
	if (op.diff())
		return new BoolSym(!same);

	return ExpressionSym::bnOper(op, exp);
}

void ContainerSym::toStringArray(StringArray &strs) const {
	int c = count();
	for (int i = 0; i < c; ++i) {
		const String &str = ((const StringSym&)item(i)->cast(StringSym::TheType)).val();
		strs.append(str);
	}
}


syntax highlighted by Code2HTML, v. 0.9.1