This repository has been archived on 2026-05-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
CompuLogic/Assets/Scripts/Workspace/Gates/State.cs
T
2020-12-17 13:07:45 +01:00

80 lines
2.0 KiB
C#
Executable File

using System.Collections.Generic;
using System.Linq;
namespace UntitledLogicGame.Workspace.Gates
{
internal class InputState : State
{
public InputState(IEnumerable<bool> args) : base(args) { }
public InputState(params bool[] args) : base(args) { }
public InputState(int len) : base(len) { }
}
internal class OutputState : State
{
public OutputState(IEnumerable<bool> args) : base(args) { }
public OutputState(params bool[] args) : base(args) { }
public OutputState(int len) : base(len) { }
}
internal abstract class State
{
internal int Length => values.Length;
internal bool[] values;
public State(IEnumerable<bool> args)
{
values = args.ToArray();
}
public State(params bool[] args)
{
values = args;
}
public State(int len)
{
values = new bool[len];
}
public bool this[int index]
{
get
{
if (index < 0 || index >= values.Length)
return false;
return values[index];
}
set
{
if (index >= 0 && index < values.Length)
values[index] = value;
}
}
public override bool Equals(object obj)
{
return obj is State state && Enumerable.SequenceEqual(values, state.values);
}
public override int GetHashCode()
{
//https://stackoverflow.com/questions/6832139/gethashcode-from-booleans-only
int hash = 17;
for (int index = 0; index < values.Length; index++)
hash = hash * 23 + values[index].GetHashCode();
return hash;
}
public override string ToString()
{
return string.Join(",", values);
}
}
}