Advertisement
Unchpokable

ReadonlyBytes

Mar 5th, 2021
827
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1. namespace hashes
  2. {
  3.     using System;
  4.     using System.Collections;
  5.     using System.Collections.Generic;
  6.     using System.Runtime.Serialization.Formatters.Binary;
  7.     using System.IO;
  8.     using System.Linq;
  9.  
  10.     [Serializable]
  11.     public class ReadonlyBytes : IEnumerable, IEnumerable<byte>, ICollection, IReadOnlyCollection<byte>
  12.     {
  13.         public int Length => innerBuffer.Length;
  14.  
  15.         private byte[] innerBuffer;
  16.         private int uID;
  17.  
  18.         public byte this[int index]
  19.         {
  20.             get
  21.             {
  22.                 if (index < 0 || index > int.MaxValue)
  23.                     throw new IndexOutOfRangeException($"Invalid index: {index}");
  24.                 return innerBuffer[index];
  25.             }
  26.         }
  27.  
  28.         public ReadonlyBytes(IEnumerable<byte> initializer)
  29.         {
  30.             innerBuffer = initializer.ToArray();
  31.             uID = ComputeHash();
  32.         }
  33.  
  34.         public ReadonlyBytes(params byte[] initializer) : this(initializer.AsEnumerable())
  35.         {
  36.         }
  37.  
  38.         public int ComputeHash()
  39.         {
  40.             unchecked
  41.             {
  42.                 var fnvPrime = 0x811C9DC5;
  43.                 var hash = 0;
  44.  
  45.                 for (var i = 0; i < innerBuffer.Length; i++)
  46.                 {
  47.                     hash *= (int)fnvPrime;
  48.                     hash ^= innerBuffer[i];
  49.                 }
  50.  
  51.                 return hash;
  52.             }
  53.         }
  54.  
  55.         #region Overrided Object methods
  56.  
  57.         public override bool Equals(object obj)
  58.         {
  59.             if (obj == null)
  60.                 return false;
  61.  
  62.             var knownObj = obj as ReadonlyBytes;
  63.  
  64.             if (knownObj == null || knownObj.GetType() != typeof(ReadonlyBytes))
  65.                 return false;
  66.  
  67.             if (knownObj.Length != this.Length)
  68.                 return false;
  69.             return this.SequenceEqual(knownObj);
  70.         }
  71.  
  72.         public override int GetHashCode()
  73.         {
  74.             return uID;
  75.         }
  76.  
  77.         public override string ToString()
  78.         {
  79.             return "[" + string.Join(", ", innerBuffer) + "]";
  80.         }
  81.         #endregion
  82.  
  83.         #region Cloning
  84.  
  85.         public new ReadonlyBytes MemberwiseClone() => new ReadonlyBytes(innerBuffer);
  86.  
  87.         public ReadonlyBytes DeepClone()
  88.         {
  89.             using (var memory = new MemoryStream())
  90.             {
  91.                 var formatter = new BinaryFormatter();
  92.                 formatter.Serialize(memory, this);
  93.                 memory.Position = 0;
  94.  
  95.                 return (ReadonlyBytes)formatter.Deserialize(memory);
  96.             }
  97.         }
  98.        
  99.         #endregion
  100.  
  101.         #region Autogenerated Interfaces Implementation
  102.         public int Count => ((ICollection)innerBuffer).Count;
  103.  
  104.         public object SyncRoot => innerBuffer.SyncRoot;
  105.  
  106.         public bool IsSynchronized => innerBuffer.IsSynchronized;
  107.  
  108.         public void CopyTo(Array array, int index)
  109.         {
  110.             innerBuffer.CopyTo(array, index);
  111.         }
  112.  
  113.         public IEnumerator<byte> GetEnumerator()
  114.         {
  115.             return innerBuffer.AsEnumerable().GetEnumerator();
  116.         }
  117.  
  118.         IEnumerator IEnumerable.GetEnumerator()
  119.         {
  120.             return GetEnumerator();
  121.         }
  122.         #endregion
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement