using System.Collections.Generic;
using System.Runtime.InteropServices;
using System;
namespace YKMoon.Math
{
///
/// Custom long mask like unity layoutmask.
///
[StructLayout(LayoutKind.Sequential)]
public struct LongMask
{
public const int MAX_LAYER = 64;
private long m_Mask;
public static implicit operator long(LongMask mask)
{
return mask.m_Mask;
}
public static implicit operator LongMask(long intVal)
{
LongMask mask;
mask.m_Mask = intVal;
return mask;
}
public long value {
get {
return this.m_Mask;
}
set {
this.m_Mask = value;
}
}
public void Set(int pos, bool value)
{
if(value) {
m_Mask |= ((long)1) << pos;
} else {
m_Mask &= ~(((long)1) << pos);
}
}
public static long GetMask(params int[] values)
{
if(values == null) {
throw new ArgumentNullException("values");
}
long num = 0;
for(int i = 0; i < values.Length; i++) {
int intval = values[i];
if(intval != -1) {
num |= ((long)1) << intval;
}
}
return num;
}
public static long GetMask(List values)
{
if(values == null) {
throw new ArgumentNullException("values");
}
long num = 0;
for(int i = 0; i < values.Count; i++) {
int intval = values[i];
if(intval != -1) {
num |= ((long)1) << intval;
}
}
return num;
}
public bool Contains(int layer)
{
return ((m_Mask & ((long)1 << layer)) > 0);
}
public List Split()
{
List result = new List();
for(int i=0; i< MAX_LAYER; i++) {
if (Contains(i)) {
result.Add(i);
}
}
return result;
}
}
}