-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hiram
committed
Apr 3, 2019
1 parent
76e6a68
commit d503625
Showing
8 changed files
with
758 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/*************************************************************** | ||
* Description: 字节位逻辑 | ||
* | ||
* Documents: | ||
* Author: [email protected] | ||
***************************************************************/ | ||
|
||
using System; | ||
using System.Collections; | ||
|
||
namespace HiFramework | ||
{ | ||
public class Bits | ||
{ | ||
private byte[] _bytes; | ||
private BitArray _bitArray; | ||
private const int BitsOfOneByte = 8; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="bitCount">how many bit</param> | ||
public Bits(int bitCount = BitsOfOneByte) | ||
{ | ||
if (bitCount <= 0) | ||
{ | ||
throw new Exception("bit count can not less or equal 0"); | ||
} | ||
var @base = bitCount / BitsOfOneByte; | ||
var left = bitCount % BitsOfOneByte; | ||
var length = left == 0 ? @base : @base + 1; | ||
if (length <= 0) | ||
{ | ||
throw new Exception("byte array can not less or equal 0"); | ||
} | ||
_bytes = new byte[length]; | ||
_bitArray = new BitArray(_bytes); | ||
} | ||
|
||
/// <summary> | ||
/// 设置某个游标对应的值 | ||
/// </summary> | ||
/// <param name="index"></param> | ||
/// <param name="value"></param> | ||
public void SetBit(int index, bool value) | ||
{ | ||
if (index >= _bitArray.Count) | ||
{ | ||
throw new IndexOutOfRangeException("index out of range"); | ||
} | ||
_bitArray[index] = value; | ||
} | ||
|
||
/// <summary> | ||
/// 获取某个游标对应值 | ||
/// </summary> | ||
/// <param name="index"></param> | ||
/// <returns></returns> | ||
public bool GetBit(int index) | ||
{ | ||
if (index >= _bitArray.Count) | ||
{ | ||
throw new IndexOutOfRangeException("index out of range"); | ||
} | ||
return _bitArray[index]; | ||
} | ||
|
||
/// <summary> | ||
/// 向左移位 | ||
/// </summary> | ||
/// <param name="count"></param> | ||
public void MoveLeft(int count) | ||
{ | ||
BitArray b = new BitArray(_bitArray.Length); | ||
for (int i = count, j = 0; i < b.Length; i++, j++) | ||
{ | ||
b[i] = _bitArray[j]; | ||
} | ||
_bitArray = b; | ||
} | ||
|
||
/// <summary> | ||
/// 与运算 | ||
/// </summary> | ||
/// <param name="bites"></param> | ||
/// <returns></returns> | ||
public Bits And(Bits bits) | ||
{ | ||
_bitArray.And(bits._bitArray); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 或运算 | ||
/// </summary> | ||
/// <param name="bits"></param> | ||
/// <returns></returns> | ||
public Bits Or(Bits bits) | ||
{ | ||
_bitArray.Or(bits._bitArray); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// 向右移位 | ||
/// </summary> | ||
public void MoveRight(int count) | ||
{ | ||
BitArray b = new BitArray(_bitArray.Length); | ||
for (int i = b.Length - count, j = _bitArray.Length; i > 0; i--, j--) | ||
{ | ||
b[i - 1] = _bitArray[j - 1]; | ||
} | ||
_bitArray = b; | ||
} | ||
|
||
/// <summary> | ||
/// 获取比特值 | ||
/// </summary> | ||
/// <returns>比特值</returns> | ||
public string GetString() | ||
{ | ||
string s = ""; | ||
for (int i = _bitArray.Length; i > 0; i--) | ||
{ | ||
var value = _bitArray[i - 1] ? 1 : 0; | ||
s += value; | ||
} | ||
return s; | ||
} | ||
} | ||
} | ||
|
||
//void Start() | ||
//{ | ||
// Bits bits = new Bits(10); | ||
// bits.SetBit(3, true); | ||
// Debug.Log("初始化: " + bits.GetString()); | ||
// bits.MoveLeft(5); | ||
// Debug.Log("向左移5位: " + bits.GetString()); | ||
// bits.MoveRight(5); | ||
// Debug.Log("向右移5位: " + bits.GetString()); | ||
|
||
// Debug.LogError("开始与或运算"); | ||
// var test = new Bits(10); | ||
// test.SetBit(5, true); | ||
// Debug.Log("初始化: " + test.GetString()); | ||
// test.And(bits); | ||
// Debug.Log("与运算:" + test.GetString()); | ||
// test.SetBit(5, true); | ||
// test.Or(bits); | ||
// Debug.Log("或运算:" + test.GetString()); | ||
//} |
209 changes: 209 additions & 0 deletions
209
HiFramework/HiFramework/Extensions/CircleBuffer/CircleBuffer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
/**************************************************************************** | ||
* Description: | ||
* | ||
* Author: [email protected] | ||
****************************************************************************/ | ||
|
||
using System; | ||
|
||
namespace HiFramework | ||
{ | ||
public class CircullarBuffer<T> : ICircullarBuffer<T> | ||
{ | ||
/// <summary> | ||
/// Read and write state | ||
/// </summary> | ||
public enum State | ||
{ | ||
WriteAhead, | ||
ReadAhead, | ||
} | ||
|
||
/// <summary> | ||
/// Array to contain element | ||
/// </summary> | ||
public T[] Array { get; private set; } | ||
|
||
/// <summary> | ||
/// Capacity | ||
/// </summary> | ||
public int Size { get; private set; } | ||
|
||
/// <summary> | ||
/// Current read and write state | ||
/// </summary> | ||
public State EState | ||
{ | ||
get { return WritePosition >= ReadPosition ? State.WriteAhead : State.ReadAhead; } | ||
} | ||
|
||
/// <summary> | ||
/// Index of read position | ||
/// </summary> | ||
public int ReadPosition { get; private set; } | ||
|
||
/// <summary> | ||
/// Index of write postion | ||
/// </summary> | ||
public int WritePosition { get; private set; } | ||
|
||
/// <summary> | ||
/// how many data can write into array | ||
/// </summary> | ||
public int HowManyCanWrite | ||
{ | ||
get | ||
{ | ||
int remain = 0; | ||
if (EState == State.WriteAhead) | ||
{ | ||
remain = (Size - WritePosition) + ReadPosition; | ||
} | ||
else if (EState == State.ReadAhead) //writer back to head | ||
{ | ||
remain = ReadPosition - WritePosition; | ||
} | ||
return remain; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// How many data wait read | ||
/// </summary> | ||
public int HowManyCanRead | ||
{ | ||
get | ||
{ | ||
int remain = 0; | ||
if (EState == State.WriteAhead) | ||
{ | ||
remain = WritePosition - ReadPosition; | ||
} | ||
else if (EState == State.ReadAhead) //writer back to head | ||
{ | ||
remain = (Size - ReadPosition) + WritePosition; | ||
} | ||
return remain; | ||
} | ||
} | ||
|
||
public CircullarBuffer(int size = 2 << 13) | ||
{ | ||
Size = size; | ||
Array = new T[Size]; | ||
} | ||
|
||
/// <summary> | ||
/// Move read index | ||
/// </summary> | ||
/// <param name="length"></param> | ||
public void MoveReadPosition(int length) | ||
{ | ||
if (length > Size) | ||
{ | ||
throw new Exception("Length is large than array's capacity"); | ||
} | ||
if (length > HowManyCanRead) | ||
{ | ||
throw new Exception("Read length large than data's length"); | ||
} | ||
var index = ReadPosition + length; | ||
if (index > Size) | ||
{ | ||
index -= Size; | ||
} | ||
ReadPosition = index; | ||
} | ||
|
||
/// <summary> | ||
/// Move write index | ||
/// </summary> | ||
/// <param name="length"></param> | ||
public void MoveWritePosition(int length) | ||
{ | ||
if (length > Size) | ||
{ | ||
throw new Exception("Length is large than array's capacity"); | ||
} | ||
if (length > HowManyCanWrite) | ||
{ | ||
throw new Exception("Write length large than space"); | ||
} | ||
var index = WritePosition + length; | ||
if (index > Size) | ||
{ | ||
index -= Size; | ||
} | ||
WritePosition = index; | ||
} | ||
|
||
/// <summary> | ||
/// Write data to array | ||
/// </summary> | ||
/// <param name="array"></param> | ||
public void Write(T[] array) | ||
{ | ||
if (array.Length > HowManyCanWrite) | ||
{ | ||
throw new Exception("Can not write so many data to array"); | ||
} | ||
var length = Size - WritePosition; | ||
if (length >= array.Length)//write into end | ||
{ | ||
for (int i = 0; i < array.Length; i++) | ||
{ | ||
Array[WritePosition + i] = array[i]; | ||
} | ||
} | ||
else | ||
{ | ||
for (int i = WritePosition; i < Size; i++)//write into end | ||
{ | ||
Array[WritePosition + i] = array[i]; | ||
} | ||
var howManyAlreadWrite = Size - WritePosition; | ||
for (int i = 0; i < array.Length - howManyAlreadWrite; i++)//write into head | ||
{ | ||
Array[i] = array[howManyAlreadWrite + i]; | ||
} | ||
} | ||
MoveWritePosition(array.Length); | ||
} | ||
|
||
/// <summary> | ||
/// Read all from array | ||
/// </summary> | ||
/// <returns></returns> | ||
public T[] Read() | ||
{ | ||
T[] ts = new T[HowManyCanRead]; | ||
if (EState == State.WriteAhead) | ||
{ | ||
for (int i = 0; i < HowManyCanRead; i++)//read end | ||
{ | ||
ts[i] = Array[ReadPosition + i]; | ||
} | ||
} | ||
else if (EState == State.ReadAhead) | ||
{ | ||
var length = Size - ReadPosition; | ||
for (int i = 0; i < length; i++)//read end | ||
{ | ||
ts[i] = Array[ReadPosition + i]; | ||
} | ||
for (int i = 0; i < WritePosition; i++)//read head | ||
{ | ||
ts[length + i] = Array[i]; | ||
} | ||
} | ||
MoveReadPosition(ts.Length); | ||
return ts; | ||
} | ||
|
||
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary> | ||
public void Dispose() | ||
{ | ||
Array = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.