您的位置:首页 > 编程语言 > C#

环形缓存(C#版)

2016-06-06 21:28 453 查看
想了一想,既然连厌恶的android平台都测试了环形缓存,不能冷落了windows。

C#源码:

/// <summary>
/// 环形缓冲区
/// 一. 写数据:
/// 1. push: 当数据已写满时返回false,否则可以正常写入返回true
/// 2. pushNoCaseFull: 不管缓冲区是否已写满或当前位置数据是否已读取过,都会写入,不关心读/写指针位置
/// 二. 读数据:
/// 1. pull: 当缓冲区已读空或还未写入过数据时,返回null
/// 另外一个重载方法可以对指定位置进行读取,此方法不会影响读指针位置
/// 2. pullNoCaseEmpty: 不管当前位置的数据有没有读过,即可以重复读,不关心读/写指针位置,不会影响读指针位置
/// 另外一个重载方法可以对指定位置进行读取,此方法不会影响读指针位置
/// 3. pullBack: 反相读取数据,返回数据情况与pull相同
/// 4. pullBackNoCaseEmpty: 反相读取数据,返回数据情况与pullNoCaseEmpty相同
/// 三. 是否已写满:
/// isFull: 如果写之前关心是否已满,调用此方法
/// 四. 是否已读空:
/// isEmpty: 读数据之前关心是否已读空,调用此方法
/// </summary>
/// <typeparam name="T">需要存储的类型</typeparam>
///

public class RingBuffer<T>
{
private static byte INVALID = 0;// 无效数据
private static byte WRITED = 1;// 数据写过
private static byte READED = 2;// 数据读过

private int mCapacity = 0;
private List<Node> mDataBuf = null;
private int mReader = 0;
private int mWriter = 0;

private class Node
{
public T @object = default(T);
public byte flag = INVALID;

public Node(T @object, byte flag)
{
this.@object = @object;
this.flag = flag;
}
}

public RingBuffer(int capacity)
{
mCapacity = capacity;
mDataBuf = new List<Node>(capacity);
}

/// <summary>
/// 当数据已写满时返回false,否则可以正常写入返回true
/// </summary>
/// <param name="object">被压入的数据</param>
/// <returns>写入成功, false:缓冲区已满</returns>
public Boolean Push(T @object)
{
lock(this)
{
int size = mDataBuf.Count;

if (mWriter < mCapacity)
{
}
else if (mWriter >= mCapacity)// 写到尾部
{
mWriter = 0;
}

if (mCapacity > size)// 无效数据(未写过数据)
{
Node node = new Node(@object, WRITED);
mDataBuf.Add(node);
mWriter++;
}
else
{
try
{
Node oldNode = mDataBuf[mWriter];
if (null != oldNode && WRITED == oldNode.flag)// 写已满
{
return false;
}

Node node = new Node(@object, WRITED);
mDataBuf[mWriter++] = node;
}
catch (Exception e)
{
}
}

return true;
}
}

/// <summary>
/// 不管缓冲区是否已写满或当前位置数据是否已读取过,都会写入,不关心读/写指针位置,也不影响读写指针
/// </summary>
/// <param name="object">被压入的数据</param>
public void PushNoCaseFull(T @object)
{
lock (this)
{
int size = mDataBuf.Count();

if (mWriter < mCapacity)
{
}
else if (mWriter >= mCapacity)// 写到尾部
{
mWriter = 0;
}

Node node = new Node(@object, WRITED);
if (mCapacity > size)// 无效数据(未写过数据)
{
mDataBuf.Add(node);
mWriter++;
}
else
mDataBuf[mWriter++] = node;
}
}

/// <summary>
/// 当缓冲区已读空或还未写入过数据时,返回null
/// </summary>
/// <returns>被拉取到的数据, 如果已读空或数据无效返回null</returns>
public T Pull()
{
lock(this)
{
if (mReader < mCapacity)
{
if (mReader < 0)
{
mReader = 0;
}
}
else if (mReader >= mCapacity)// 写到尾部
{
mReader = 0;
}

try
{
Node node = mDataBuf[mReader];
if (null != node && WRITED == node.flag)
{
node.flag = READED;
mDataBuf[mReader] = node;

mReader++;
return node.@object;
}
else// 已读空
{
}
}
catch (Exception e)// No data
{
}

return default(T);
}
}

/// <summary>
/// 对指定位置进行读取,此方法不会影响读指针位置
/// </summary>
/// <param name="location">读指针位置</param>
/// <returns>被拉取到的数据, 如果已读空或数据无效返回null</returns>
public T Pull(int location)
{
lock(this)
{
if (location < mCapacity)
{
if (location < 0)
{
location = 0;
}
}
else if (location >= mCapacity)// 读到尾部
{
location = 0;
}

try
{
Node node = mDataBuf[location];
if (null != node && WRITED == node.flag)
{
//node.flag = READED;
//mDataBuf[location] = node;

return node.@object;
}
else// 已读空
{
}
}
catch (Exception e)// No data
{
}

return default(T);
}
}

/// <summary>
/// 不管当前位置的数据有没有读过,即可以重复读,不关心读/写指针位置,也不影响读写指针
/// </summary>
/// <returns>被拉取到的数据, 如果数据无效返回null</returns>
public T PullNoCaseEmpty()
{
lock(this)
{
if (mReader < mCapacity)
{
if (mReader < 0)
{
mReader = 0;
}
}
else if (mReader >= mCapacity)// 读到尾部
{
mReader = 0;
}

try
{
Node node = mDataBuf[mReader];
if (null != node && INVALID != node.flag)// 只要数据有效
{
//node.flag = READED;
//mDataBuf[mReader] = node;

mReader++;
return node.@object;
}
else// 数据无效
{
}
}
catch (Exception e)
{
}

return default(T);
}
}

/// <summary>
/// 对指定位置进行读取,此方法不会影响读指针位置
/// </summary>
/// <param name="location">读取位置</param>
/// <returns>被拉取到的数据, 如果数据无效返回null</returns>
public T PullNoCaseEmpty(int location)
{
lock(this)
{
if (location < mCapacity)
{
if (location < 0)
{
location = 0;
}
}
else if (location >= mCapacity)// 读到尾部
{
location = 0;
}

try
{
Node node = mDataBuf[location];
if (null != node && INVALID != node.flag)// 只要数据有效
{
//node.flag = READED;
//mDataBuf[location] = node;
return node.@object;
}
else// 数据无效
{
}
}
catch (Exception e)
{
}

return default(T);
}
}

/// <summary>
/// 反相读取数据,当缓冲区已读空或还未写入过数据时,返回null
/// </summary>
/// <returns>被拉取到的数据, 如果已读空或数据无效返回null</returns>
public T PullBack()
{
lock(this)
{
if (mReader < mCapacity)
{
if (mReader < 0)
{
mReader = mCapacity - 1;
}
}
else if (mReader >= mCapacity)// 读到尾部
{
mReader = 0;
}

try
{
Node node = mDataBuf[mReader];
if (null != node && WRITED == node.flag)
{
node.flag = READED;
mDataBuf[mReader] = node;

mReader--;
return node.@object;
}
else// 已读空
{
}
}
catch (Exception e)// 可能还未写过数据
{
}

return default(T);
}
}

/// <summary>
/// 反相读取数据,不管当前位置的数据有没有读过,即可以重复读,不关心读/写指针位置,也不影响读写指针
/// </summary>
/// <returns>被拉取到的数据, 如果数据无效返回null</returns>
public T PullBackNoCaseEmpty()
{
lock(this)
{
if (mReader < mCapacity)
{
if (mReader < 0)
{
mReader = mCapacity - 1;
}
}
else if (mReader >= mCapacity)// 读到尾部
{
mReader = 0;
}

try
{
Node node = mDataBuf[mReader];
if (null != node && INVALID != node.flag)
{
//node.flag = READED;
//mDataBuf[mReader] = node;

mReader--;
return node.@object;
}
else// 无效数据(未写过)
{
}
}
catch (Exception e)// 可能还未写过数据
{
}

return default(T);
}
}

/// <summary>
/// 缓冲区是否已满(对写操作而言)
/// </summary>
/// <returns>true: 满, false:未满</returns>
public Boolean IsFull()
{
lock(this)
{
try
{
Node n = mDataBuf[mWriter];
if (null != n && WRITED == n.flag)
{
return true;
}
}
catch (Exception e)
{
}
return false;
}
}

/// <summary>
/// 缓冲区是否为空(对读操作而言)
/// </summary>
/// <returns></returns>
public Boolean IsEmpty()
{
lock(this)
{
if (mReader < mCapacity)
{
}
else if (mReader >= mCapacity)// 读到尾部
{
mReader = 0;
}

try
{
Node node = mDataBuf[mReader];
if (null != node && WRITED == node.flag)
{
}
else
{
return true;
}
}
catch (Exception e)// 可能未写入过数据
{
return true;
}

return false;
}
}

/// <summary>
/// 环形缓存容量
/// </summary>
/// <returns>容量</returns>
public int Capacity()
{
lock(this)
{
return mCapacity;
}
}

/// <summary>
/// 环形缓存size, size与capacity不一定相同,当还未填充满时size小于capacity,反之size=capacity
/// </summary>
/// <returns>size</returns>
public int Size()
{
lock (this)
{
return mDataBuf.Count;
}
}
}

测试代码略。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: