<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Duality.Editor;
using Duality.Resources;
using Duality.Serialization;

namespace Duality.Drawing
{
	/// <summary>
	/// Describes a set of shader parameters independently from any specific shader.
	/// It's a CPU side key-value store for values that can be applied to a shader
	/// program by the <see cref="Duality.Backend.IGraphicsBackend"/>.
	/// </summary>
	public class ShaderParameterCollection : IEquatable<ShaderParameterCollection>, ISerializeExplicit
	{
		private struct NameComparer : IComparer<ValueItem>
		{
			public int Compare(ValueItem x, ValueItem y)
			{
				return string.CompareOrdinal(x.Name, y.Name);
			}
		}
		private struct ValueItem
		{
			public string Name;
			public ContentRef<Texture> Texture;
			public float[] Uniform;

			public override string ToString()
			{
				if (this.Uniform != null)
					return string.Format("{0}: {1}", this.Name, this.Uniform.ToString(", "));
				else
					return string.Format("{0}: {1}", this.Name, this.Texture);
			}
		}

		private static readonly IComparer<ValueItem> nameComparer = new NameComparer();

		private RawList<ValueItem> values = null;
		private ulong              hash   = 0L;
		
		
		/// <summary>
		/// [GET] A 64 bit hash value that represents this particular collection of
		/// shader parameters. The same set of parameters will always have the same
		/// hash value.
		/// </summary>
		public ulong Hash
		{
			get { return this.hash; }
		}
		/// <summary>
		/// [GET] Enumerates all variables in this collection by name.
		/// </summary>
		public IEnumerable<string> Names
		{
			get
			{
				for (int i = 0; i < this.values.Count; i++)
				{
					yield return this.values.Data[i].Name;
				}
			}
		}


		public ShaderParameterCollection()
		{
			this.UpdateHash();
		}
		public ShaderParameterCollection(ShaderParameterCollection other)
		{
			if (other.values != null)
			{
				this.values = new RawList<ValueItem>(other.values);

				int count = this.values.Count;
				ValueItem[] data = this.values.Data;
				for (int i = 0; i < data.Length; i++)
				{
					if (i > count) break;
					if (data[i].Uniform != null)
						data[i].Uniform = (float[])data[i].Uniform.Clone();
				}<commit_msg>Added a null-protected Count method to ShaderParameterCollection<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Duality.Editor;
using Duality.Resources;
using Duality.Serialization;

namespace Duality.Drawing
{
	/// <summary>
	/// Describes a set of shader parameters independently from any specific shader.
	/// It's a CPU side key-value store for values that can be applied to a shader
	/// program by the <see cref="Duality.Backend.IGraphicsBackend"/>.
	/// </summary>
	public class ShaderParameterCollection : IEquatable<ShaderParameterCollection>, ISerializeExplicit
	{
		private struct NameComparer : IComparer<ValueItem>
		{
			public int Compare(ValueItem x, ValueItem y)
			{
				return string.CompareOrdinal(x.Name, y.Name);
			}
		}
		private struct ValueItem
		{
			public string Name;
			public ContentRef<Texture> Texture;
			public float[] Uniform;

			public override string ToString()
			{
				if (this.Uniform != null)
					return string.Format("{0}: {1}", this.Name, this.Uniform.ToString(", "));
				else
					return string.Format("{0}: {1}", this.Name, this.Texture);
			}
		}

		private static readonly IComparer<ValueItem> nameComparer = new NameComparer();

		private RawList<ValueItem> values = null;
		private ulong              hash   = 0L;
		
		
		/// <summary>
		/// [GET] A 64 bit hash value that represents this particular collection of
		/// shader parameters. The same set of parameters will always have the same
		/// hash value.
		/// </summary>
		public ulong Hash
		{
			get { return this.hash; }
		}
		/// <summary>
		/// [GET] Enumerates all variables in this collection by name.
		/// </summary>
		public IEnumerable<string> Names
		{
			get
			{
				for (int i = 0; i < this.Count; i++)
				{
					yield return this.values.Data[i].Name;
				}
			}
		}
		/// <summary>
		/// [GET] Returns the number of variables in this collection.
		/// </summary>
		public int Count
		{
			get
			{
				if (this.values != null)
					return this.values.Count;

				return 0;
			}
		}

		public ShaderParameterCollection()
		{
			this.UpdateHash();
		}
		public ShaderParameterCollection(ShaderParameterCollection other)
		{
			if (other.values != null)
			{
				this.values = new RawList<ValueItem>(other.values);

				int count = this.values.Count;
				ValueItem[] data = this.values.Data;
				for (int i = 0; i < data.Length; i++)
				{
					if (i > count) break;
					if (data[i].Uniform != null)
						data[i].Uniform = (float[])data[i].Uniform.Clone();
				}
{'fraction_non_alphanumeric': 0.12321686996071946, 'fraction_numerical': 0.00392805457928468, 'mean_word_length': 2.955846279640229, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2272006', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1476, 'n_words': 438}