using System;
using System.IO;
using Microsoft.SqlServer.Server;
using System.Data.SqlTypes;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;


[SqlUserDefinedAggregate(Format.Native, IsInvariantToDuplicates = false, IsInvariantToNulls = true, IsInvariantToOrder = true)]
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public class ComplexSum
{
	/// <summary>
	/// accumlates the result of the aggregation
	/// </summary>
	ComplexNumber result = new ComplexNumber();
	/// <summary>
	/// initialize used to set initial value
	/// of complex type
	/// </summary>
	public void Init()
	{
		result.realpart = 0;
		result.imagpart = 0;
	}
	/// <summary>
	/// accumulate a value, this method ignores null values
	/// </summary>
	/// <param name="next"></param>
	public void Accumulate(ComplexNumber next)
	{
		if (next.IsNull)
			return;

		result.realpart += next.realpart;
		result.imagpart += next.imagpart;
	}
	/// <summary>
	/// merge the partial aggregation with this instance.
	/// </summary>
	/// <param name="other"></param>
	public void Merge(ComplexSum other)
	{
		result.realpart += other.result.realpart;
		result.imagpart += other.result.imagpart;
	}
	/// <summary>
	/// terminate the aggregation and return the results to the caller.
	/// </summary>
	/// <returns></returns>
	public ComplexNumber Terminate()
	{
		return result;
	}
}    
