Skip to content

Instantly share code, notes, and snippets.

@enue
Created October 9, 2018 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enue/cb60ea830d7a63f864683c91188cc2dd to your computer and use it in GitHub Desktop.
Save enue/cb60ea830d7a63f864683c91188cc2dd to your computer and use it in GitHub Desktop.
NativeBitArray for Unity2018.2.8f1
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
namespace TSKT
{
public struct NativeBitArray : IDisposable, IEnumerable<bool>
{
NativeArray<int> array;
public int Length { get; }
public NativeBitArray(int length, Allocator allocator)
{
Length = length;
if (Length == 0)
{
array = new NativeArray<int>(
0,
allocator, NativeArrayOptions.ClearMemory);
}
else
{
array = new NativeArray<int>(
(length - 1) / 32 + 1,
allocator, NativeArrayOptions.ClearMemory);
}
}
public bool this[int index]
{
get
{
var i = index / 32;
var j = index % 32;
return (array[i] & (1 << j)) != 0;
}
set
{
if (index < 0 || index >= Length)
{
throw new ArgumentOutOfRangeException();
}
var i = index / 32;
var j = index % 32;
var item = array[i];
if (value)
{
item = (item | (1 << j));
}
else
{
item = (item & ~(1 << j));
}
array[i] = item;
}
}
public void Dispose()
{
array.Dispose();
}
public void SetAll(bool value)
{
var fillValue = value ? unchecked(((int)0xffffffff)) : 0;
for (int i=0; i<array.Length; ++i)
{
array[i] = fillValue;
}
}
public void And(NativeBitArray source)
{
for(int i=0; i<array.Length; ++i)
{
var v = array[i] & source.array[i];
array[i] = v;
}
}
public void Or(NativeBitArray source)
{
for (int i = 0; i < array.Length; ++i)
{
var v = array[i] | source.array[i];
array[i] = v;
}
}
public void Xor(NativeBitArray source)
{
for (int i = 0; i < array.Length; ++i)
{
var v = array[i] ^ source.array[i];
array[i] = v;
}
}
public IEnumerator<bool> GetEnumerator()
{
for (int i = 0; i < Length; ++i)
{
yield return this[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
for (int i = 0; i < Length; ++i)
{
yield return this[i];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment