You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

150 lines
4.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Globalization;
using static System.Net.Mime.MediaTypeNames;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Net;
using System.Management;
using System.IO.Ports;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms.Design;
namespace UTIL
{
#region Циклический буфер
public class CircularBuffer<T>
{
T[] _buffer;
Int32 _head;
Int32 _tail;
Int32 _length;
Int32 _bufferSize;
Int32 _pos;
Object _lock = new object();
public CircularBuffer(Int32 bufferSize)
{
_buffer = new T[bufferSize];
_bufferSize = bufferSize;
_head = bufferSize - 1;
_tail = 0;
_length = 0;
}
public Int32 Count
{
get { return _length; }
}
public Int32 Head
{
get { return _head; }
}
public Int32 Tail
{
get { return _tail; }
}
public bool IsEmpty
{
get { return _length == 0; }
}
public bool IsFull
{
get { return _length == _bufferSize; }
}
public bool IsNoData
{
get { return _tail + _pos >= _head; }
}
public T Dequeue()
{
if (IsEmpty) throw new InvalidOperationException("Queue exhausted");
T dequeued = _buffer[_tail];
_tail = NextPosition(_tail);
_length--;
return dequeued;
}
public T Peek(int pos)
{
_pos = pos;
if (IsEmpty) throw new InvalidOperationException("Queue exhausted");
if (_tail + pos > _head) throw new InvalidOperationException("End data");
T dequeued = _buffer[_tail + pos];
return dequeued;
}
public T Peek()
{
if (IsEmpty) throw new InvalidOperationException("Queue exhausted");
if (_tail > _head) throw new InvalidOperationException("End data");
T dequeued = _buffer[_tail];
return dequeued;
}
private int NextPosition(int position)
{
return (position + 1) % _bufferSize;
}
public void Enqueue(T toAdd)
{
_head = NextPosition(_head);
_buffer[_head] = toAdd;
if (IsFull)
_tail = NextPosition(_tail);
else
_length++;
}
}
public class CircularBuffer2<T>
{
T[] _buffer;
Int32 _head;
Int32 _tail;
Int32 _length;
Int32 _bufferSize;
Int32 _pos;
Object _lock = new object();
public CircularBuffer2(Int32 bufferSize)
{
_buffer = new T[bufferSize];
_bufferSize = bufferSize;
_head = bufferSize - 1;
_tail = 0;
_length = 0;
}
public T Dequeue()
{
T dequeued = _buffer[_tail];
_tail = (_tail + 1) % _bufferSize;
_length--;
return dequeued;
}
public T Peek()
{
if (_length == 0) throw new InvalidOperationException("No data");
return _buffer[_tail];
}
private int NextPosition(int position)
{
return (position + 1) % _bufferSize;
}
public void Enqueue(T toAdd)
{
_head = (_head + 1) % _bufferSize;
_buffer[_head] = toAdd;
if (_length == _bufferSize)
_tail = (_tail + 1) % _bufferSize;
else
_length++;
}
}
#endregion
}