annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/CircularBuffer.cs @ 168:ceec0dd9ec9c

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 07 Feb 2020 11:51:13 +0000
parents 5b4145a0d408
children
rev   line source
cannam@128 1 //
cannam@128 2 // © Copyright Henrik Ravn 2004
cannam@128 3 //
cannam@128 4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
cannam@128 5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
cannam@128 6 //
cannam@128 7
cannam@128 8 using System;
cannam@128 9 using System.Diagnostics;
cannam@128 10
cannam@128 11 namespace DotZLib
cannam@128 12 {
cannam@128 13
cannam@128 14 /// <summary>
cannam@128 15 /// This class implements a circular buffer
cannam@128 16 /// </summary>
cannam@128 17 internal class CircularBuffer
cannam@128 18 {
cannam@128 19 #region Private data
cannam@128 20 private int _capacity;
cannam@128 21 private int _head;
cannam@128 22 private int _tail;
cannam@128 23 private int _size;
cannam@128 24 private byte[] _buffer;
cannam@128 25 #endregion
cannam@128 26
cannam@128 27 public CircularBuffer(int capacity)
cannam@128 28 {
cannam@128 29 Debug.Assert( capacity > 0 );
cannam@128 30 _buffer = new byte[capacity];
cannam@128 31 _capacity = capacity;
cannam@128 32 _head = 0;
cannam@128 33 _tail = 0;
cannam@128 34 _size = 0;
cannam@128 35 }
cannam@128 36
cannam@128 37 public int Size { get { return _size; } }
cannam@128 38
cannam@128 39 public int Put(byte[] source, int offset, int count)
cannam@128 40 {
cannam@128 41 Debug.Assert( count > 0 );
cannam@128 42 int trueCount = Math.Min(count, _capacity - Size);
cannam@128 43 for (int i = 0; i < trueCount; ++i)
cannam@128 44 _buffer[(_tail+i) % _capacity] = source[offset+i];
cannam@128 45 _tail += trueCount;
cannam@128 46 _tail %= _capacity;
cannam@128 47 _size += trueCount;
cannam@128 48 return trueCount;
cannam@128 49 }
cannam@128 50
cannam@128 51 public bool Put(byte b)
cannam@128 52 {
cannam@128 53 if (Size == _capacity) // no room
cannam@128 54 return false;
cannam@128 55 _buffer[_tail++] = b;
cannam@128 56 _tail %= _capacity;
cannam@128 57 ++_size;
cannam@128 58 return true;
cannam@128 59 }
cannam@128 60
cannam@128 61 public int Get(byte[] destination, int offset, int count)
cannam@128 62 {
cannam@128 63 int trueCount = Math.Min(count,Size);
cannam@128 64 for (int i = 0; i < trueCount; ++i)
cannam@128 65 destination[offset + i] = _buffer[(_head+i) % _capacity];
cannam@128 66 _head += trueCount;
cannam@128 67 _head %= _capacity;
cannam@128 68 _size -= trueCount;
cannam@128 69 return trueCount;
cannam@128 70 }
cannam@128 71
cannam@128 72 public int Get()
cannam@128 73 {
cannam@128 74 if (Size == 0)
cannam@128 75 return -1;
cannam@128 76
cannam@128 77 int result = (int)_buffer[_head++ % _capacity];
cannam@128 78 --_size;
cannam@128 79 return result;
cannam@128 80 }
cannam@128 81
cannam@128 82 }
cannam@128 83 }