/
NLObP
/
AAEmu
Обзор
Документация
Войти
/
NLObP
/
AAEmu
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
AAEmu.Commons/Utils/BitSet.cs
77 строк
2 KB
Roger Barreto
Performance + Refactoring + Code Style (#709)
16 сен 2023, 10:50
Не верифицирован
16 сен 2023, 10:50
0d89a74
Код
Авторство
О чём код?
using System.Collections; namespace AAEmu.Commons.Utils; public sealed class BitSet { private BitArray _bits; public int Count { get; private set; } public BitSet(int count) { Count = count; _bits = new BitArray(count); } public bool this[int index] => Get(index); public void Clear() => _bits.SetAll(false); public void Clear(int index) => _bits.Set(index, false); public void Set(int index) => _bits.Set(index, true); public bool Get(int index) => _bits.Get(index); public int NextSet(int startFrom) { var offset = startFrom; if (offset >= Count) return -1; var res = _bits.Get(offset); // locate non-empty slot while (!res) { if ((++offset) >= Count) return -1; res = _bits.Get(offset); } return offset; } public int NextClear(int startFrom) { var offset = startFrom; if (offset >= Count) return -1; var res = _bits.Get(offset); // locate non-empty slot while (res) { if ((++offset) >= Count) return -1; res = _bits.Get(offset); } return offset; } public void Or(BitSet other) { for (var i = 0; i < other.Count; i++) _bits[i] = other[i]; } public int[] ToIntArray() { var result = new int[Count / 32]; _bits.CopyTo(result, 0); return result; } public byte[] ToByteArray() { var result = new byte[Count / 8]; _bits.CopyTo(result, 0); return result; } }