PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/DICOM/IO/Buffer/ByteBufferEnumerator.cs

https://github.com/petnet/fo-dicom
C# | 191 lines | 152 code | 39 blank | 0 comment | 23 complexity | 4b744720e5bc10c49521af96f17f6bbc MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. namespace Dicom.IO.Buffer {
  7. public abstract class ByteBufferEnumerator<T> : IEnumerable<T>, IEnumerator<T> {
  8. protected ByteBufferEnumerator(IByteBuffer buffer) {
  9. Buffer = buffer;
  10. UnitSize = Marshal.SizeOf(typeof(T));
  11. Position = -UnitSize;
  12. }
  13. public IByteBuffer Buffer {
  14. get;
  15. protected set;
  16. }
  17. private byte[] _data;
  18. protected byte[] Data {
  19. get {
  20. if (_data == null)
  21. _data = Buffer.Data;
  22. return _data;
  23. }
  24. }
  25. protected int Position {
  26. get;
  27. set;
  28. }
  29. protected int UnitSize {
  30. get;
  31. set;
  32. }
  33. public void Dispose() {
  34. _data = null;
  35. }
  36. public IEnumerator<T> GetEnumerator() {
  37. return this;
  38. }
  39. IEnumerator IEnumerable.GetEnumerator() {
  40. return this;
  41. }
  42. public T Current {
  43. get { return CurrentItem(); }
  44. }
  45. object System.Collections.IEnumerator.Current {
  46. get { return CurrentItem(); }
  47. }
  48. public bool MoveNext() {
  49. Position += UnitSize;
  50. if (Position <= (Buffer.Size - UnitSize))
  51. return true;
  52. return false;
  53. }
  54. public void Reset() {
  55. Position = -UnitSize;
  56. }
  57. protected abstract T CurrentItem();
  58. public static IEnumerable<T> Create(IByteBuffer buffer) {
  59. if (typeof(T) == typeof(byte))
  60. return (IEnumerable<T>)new ByteBufferByteEnumerator(buffer);
  61. if (typeof(T) == typeof(sbyte))
  62. return (IEnumerable<T>)new ByteBufferSByteEnumerator(buffer);
  63. if (typeof(T) == typeof(short))
  64. return (IEnumerable<T>)new ByteBufferInt16Enumerator(buffer);
  65. if (typeof(T) == typeof(ushort))
  66. return (IEnumerable<T>)new ByteBufferUInt16Enumerator(buffer);
  67. if (typeof(T) == typeof(int))
  68. return (IEnumerable<T>)new ByteBufferInt32Enumerator(buffer);
  69. if (typeof(T) == typeof(uint))
  70. return (IEnumerable<T>)new ByteBufferUInt32Enumerator(buffer);
  71. if (typeof(T) == typeof(long))
  72. return (IEnumerable<T>)new ByteBufferInt64Enumerator(buffer);
  73. if (typeof(T) == typeof(ulong))
  74. return (IEnumerable<T>)new ByteBufferUInt64Enumerator(buffer);
  75. if (typeof(T) == typeof(float))
  76. return (IEnumerable<T>)new ByteBufferSingleEnumerator(buffer);
  77. if (typeof(T) == typeof(double))
  78. return (IEnumerable<T>)new ByteBufferDoubleEnumerator(buffer);
  79. throw new NotSupportedException("ByteBufferEnumerator<T> only provides support for the base .NET numeric types.");
  80. }
  81. protected class ByteBufferByteEnumerator : ByteBufferEnumerator<byte> {
  82. public ByteBufferByteEnumerator(IByteBuffer buffer) : base(buffer) {
  83. }
  84. protected override byte CurrentItem() {
  85. return Data[Position];
  86. }
  87. }
  88. protected class ByteBufferSByteEnumerator : ByteBufferEnumerator<sbyte> {
  89. public ByteBufferSByteEnumerator(IByteBuffer buffer) : base(buffer) {
  90. }
  91. protected override sbyte CurrentItem() {
  92. return (sbyte)Data[Position];
  93. }
  94. }
  95. protected class ByteBufferInt16Enumerator : ByteBufferEnumerator<short> {
  96. public ByteBufferInt16Enumerator(IByteBuffer buffer) : base(buffer) {
  97. }
  98. protected override short CurrentItem() {
  99. return BitConverter.ToInt16(Data, Position);
  100. }
  101. }
  102. protected class ByteBufferUInt16Enumerator : ByteBufferEnumerator<ushort> {
  103. public ByteBufferUInt16Enumerator(IByteBuffer buffer) : base(buffer) {
  104. }
  105. protected override ushort CurrentItem() {
  106. return BitConverter.ToUInt16(Data, Position);
  107. }
  108. }
  109. protected class ByteBufferInt32Enumerator : ByteBufferEnumerator<int> {
  110. public ByteBufferInt32Enumerator(IByteBuffer buffer) : base(buffer) {
  111. }
  112. protected override int CurrentItem() {
  113. return BitConverter.ToInt32(Data, Position);
  114. }
  115. }
  116. protected class ByteBufferUInt32Enumerator : ByteBufferEnumerator<uint> {
  117. public ByteBufferUInt32Enumerator(IByteBuffer buffer) : base(buffer) {
  118. }
  119. protected override uint CurrentItem() {
  120. return BitConverter.ToUInt32(Data, Position);
  121. }
  122. }
  123. protected class ByteBufferInt64Enumerator : ByteBufferEnumerator<long> {
  124. public ByteBufferInt64Enumerator(IByteBuffer buffer) : base(buffer) {
  125. }
  126. protected override long CurrentItem() {
  127. return BitConverter.ToInt64(Data, Position);
  128. }
  129. }
  130. protected class ByteBufferUInt64Enumerator : ByteBufferEnumerator<ulong> {
  131. public ByteBufferUInt64Enumerator(IByteBuffer buffer) : base(buffer) {
  132. }
  133. protected override ulong CurrentItem() {
  134. return BitConverter.ToUInt64(Data, Position);
  135. }
  136. }
  137. protected class ByteBufferSingleEnumerator : ByteBufferEnumerator<float> {
  138. public ByteBufferSingleEnumerator(IByteBuffer buffer) : base(buffer) {
  139. }
  140. protected override float CurrentItem() {
  141. return BitConverter.ToSingle(Data, Position);
  142. }
  143. }
  144. protected class ByteBufferDoubleEnumerator : ByteBufferEnumerator<double> {
  145. public ByteBufferDoubleEnumerator(IByteBuffer buffer) : base(buffer) {
  146. }
  147. protected override double CurrentItem() {
  148. return BitConverter.ToDouble(Data, Position);
  149. }
  150. }
  151. }
  152. }