Sylloge
A C# helper library
code/System/Memory.cs
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 using System;
00017 using System.Collections;
00018 using System.Collections.Generic;
00019 using System.Text;
00020 using System.Windows.Forms;
00021 using System.Reflection;
00022 
00023 namespace Sylloge
00024 {
00028     public static class Memory
00029     {
00030         #region Constants
00031 
00032         #region 'Standard' (base 10) byte measurements
00033 
00037         public const uint KB = 1000;
00041         public const uint MB = (1000 * Sylloge.Memory.KB);
00045         public const uint GB = (1000 * Sylloge.Memory.MB);
00049         public const ulong TB = (1000L * Sylloge.Memory.GB);
00050 
00051         #endregion
00052 
00053         #region 'Scientific' (base 2) byte measurements
00054 
00058         public const uint KiB = 1024;
00062         public const uint MiB = (1024 * Sylloge.Memory.KiB);
00066         public const uint GiB = (1024 * Sylloge.Memory.MiB);
00070         public const ulong TiB = (1024L * Sylloge.Memory.GiB);
00071 
00072         #endregion
00073 
00074         #endregion
00075 
00083         public static bool Compare(Array arr1, Array arr2)
00084         {
00085             if (arr1 == null || arr2 == null) { throw new System.ArgumentNullException((arr1 == null) ? "arr1" : "arr2"); }
00086             if (arr1 == arr2) { return true; } // are the pointers the same?
00087             if (arr1.Length == 0 || arr2.Length == 0) { throw new System.ArgumentException("array cannot be 0 length"); }
00088             if (arr1.LongLength != arr2.LongLength) { return false; }
00089             long size = arr1.LongLength;
00090             bool m2 = (size % 2 == 0);
00091             bool m4 = (size % 4 == 0);
00092             bool m8 = (size % 8 == 0);
00093             for (long x = 0; x < size; ) {
00094                 if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00095                 if (m2) {
00096                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00097                 }
00098                 if (m4) {
00099                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00100                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00101                 }
00102                 if (m8) {
00103                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00104                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00105                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00106                     if (arr1.GetValue(x) != arr2.GetValue(x)) { return false; } x++;
00107                 }
00108             }
00109             return true;
00110         }
00111 
00124         public static void Copy(Array source, int sourceOffset, Array dest, int destOffest, int count)
00125         {
00126             if (source == null || source.Length == 0) { throw new System.ArgumentException("Source must not be null or empty"); }
00127             if (dest == null || dest.Length == 0) { throw new System.ArgumentException("Destination must not be null or 0-length"); }
00128             if (sourceOffset >= source.Length) { throw new System.ArgumentOutOfRangeException("sourceOffset", "Source offset is greater than source length"); }
00129             if (destOffest >= dest.Length) { throw new System.ArgumentOutOfRangeException("destOffest", "Destination offset is greater than destination length"); }
00130             if (count > (dest.Length - destOffest)) { throw new System.ArgumentOutOfRangeException("count", "Count is greater than the destination length - offset"); }
00131             Sylloge.Memory.Set(dest, destOffest, count, 0x00);
00132             // if count > the diff between src.len and offset, then BlockCopy throws an error
00133             // so we want to copy differnece of the 2 instead of count in that instance so we
00134             // get the bytes we want without the error
00135             int cnt = ((count > (source.Length - sourceOffset)) ? (source.Length - sourceOffset) : count);
00136             System.Buffer.BlockCopy(source, sourceOffset, dest, destOffest, cnt);
00137         }
00138 
00144         public static void Copy(Array source, Array dest)
00145         {
00146             Sylloge.Memory.Copy(source, 0, dest, 0, source.Length);
00147         }
00148 
00155         public static bool IsBitSet(byte b, int pos)
00156         {
00157             if (pos < 0) { throw new System.ArgumentOutOfRangeException("Position must be greater than 0"); }
00158             if (pos > 7) { throw new System.ArgumentOutOfRangeException("Position must be less than 8"); }
00159             return ((b & (1 << pos)) != 0);
00160         }
00161 
00168         public static byte SetBit(byte b, int pos)
00169         {
00170             if (pos < 0) { throw new System.ArgumentOutOfRangeException("Position must be greater than 0"); }
00171             if (pos > 7) { throw new System.ArgumentOutOfRangeException("Position must be less than 8"); }
00172             return (b |= (byte)(1 << pos));
00173         }
00174 
00183         public static Array Set(Array dest, long count, object value)
00184         {
00185             if (dest == null || dest.Length == 0) { throw new System.ArgumentException("Destination must not be null or empty"); }
00186             if (count > dest.LongLength) { throw new System.ArgumentOutOfRangeException("size", "must be less than destination length"); }
00187             bool m2 = (count % 2 == 0);
00188             bool m4 = (count % 4 == 0);
00189             bool m8 = (count % 8 == 0);
00190             for (long x = 0; x < count; ) {
00191                 dest.SetValue(value, x++);
00192                 if (m2) { dest.SetValue(value, x++); }
00193                 if (m4) {
00194                     dest.SetValue(value, x++);
00195                     dest.SetValue(value, x++);
00196                 }
00197                 if (m8) {
00198                     dest.SetValue(value, x++);
00199                     dest.SetValue(value, x++);
00200                     dest.SetValue(value, x++);
00201                     dest.SetValue(value, x++);
00202                 }
00203             }
00204             return dest;
00205         }
00206 
00216         public static Array Set(Array dest, long offset, long count, byte value)
00217         {
00218             if (dest == null || dest.Length == 0) { throw new System.ArgumentException("Destination must not be null or empty"); }
00219             if (count > dest.LongLength) { throw new System.ArgumentOutOfRangeException("count", "must be less than destination length"); }
00220             if (offset < 0 || offset > dest.LongLength) { throw new System.ArgumentOutOfRangeException("count", "must be less than array length and greater than 0"); }
00221             if (count > (dest.LongLength - offset)) { throw new System.ArgumentOutOfRangeException("count", "must be less than destination length - offset"); }
00222             bool m2 = (count % 2 == 0);
00223             bool m4 = (count % 4 == 0);
00224             bool m8 = (count % 8 == 0);
00225             for (long x = offset; x < count; ) {
00226                 dest.SetValue(value, x++);
00227                 if (m2) { dest.SetValue(value, x++); }
00228                 if (m4) {
00229                     dest.SetValue(value, x++);
00230                     dest.SetValue(value, x++);
00231                 }
00232                 if (m8) {
00233                     dest.SetValue(value, x++);
00234                     dest.SetValue(value, x++);
00235                     dest.SetValue(value, x++);
00236                     dest.SetValue(value, x++);
00237                 }
00238             }
00239             return dest;
00240         }
00241 
00247         public static Array SwapValueOrder(Array value)
00248         {
00249             if (value == null || value.Length <= 0) { throw new System.ArgumentException("Value cannot be empty"); }
00250             object h, l;
00251             for (int i = (value.Length - 1), ni = 0; i > ni; i--, ni++) {
00252                 h = value.GetValue(i);
00253                 l = value.GetValue(ni);
00254                 value.SetValue(l, i);
00255                 value.SetValue(h, ni);
00256             }
00257             return value;
00258         }
00259 
00271         public static Array SwapValueOrder(Array value, int index, int count, bool returnWholeValue)
00272         {
00273             if (value == null || value.Length <= 0) { throw new System.ArgumentException("Value cannot be empty"); }
00274             if (count <= 0 || count > value.Length) { throw new System.ArgumentOutOfRangeException("count", "Count must be greater than 0 and less than value length"); }
00275             if (index < 0 || index > value.Length) { throw new System.ArgumentOutOfRangeException("index", "Index must be greater than 0 and less than value length"); }
00276             if ((index + count) > value.Length) { throw new System.ArgumentOutOfRangeException("Index + count cannot exceed array limit"); }
00277             Array nb = Array.CreateInstance(value.GetValue(0).GetType(), count);
00278             System.Buffer.BlockCopy(value, index, nb, 0, count);
00279             nb = Sylloge.Memory.SwapValueOrder(nb);
00280             if (returnWholeValue) {
00281                 //Array ret = value;
00282                 System.Buffer.BlockCopy(nb, 0, value, index, count);
00283                 return value;
00284             }
00285             return nb;
00286         }
00287 
00295         public static Array SwapValueOrder(Array value, int index, int count)
00296         {
00297             return Sylloge.Memory.SwapValueOrder(value, index, count, false);
00298         }
00299 
00305         public static byte[] SwapByteOrder(byte[] value)
00306         {
00307             return (byte[])SwapValueOrder(value);
00308         }
00309 
00321         public static byte[] SwapByteOrder(byte[] value, int index, int count, bool returnWholeValue)
00322         {
00323             return (byte[])Sylloge.Memory.SwapValueOrder(value, index, count, returnWholeValue);
00324         }
00325 
00333         public static byte[] SwapByteOrder(byte[] value, int index, int count)
00334         {
00335             return Sylloge.Memory.SwapByteOrder(value, index, count, false);
00336         }
00337 
00338         /*
00343         public static void SwapByteOrder(ref byte[] value)
00344         {
00345             value = Sylloge.Memory.SwapByteOrder(value);
00346         }
00347 
00358         public static void SwapByteOrder(ref byte[] value, int index, int count, bool returnWholeValue)
00359         {
00360             value = Sylloge.Memory.SwapByteOrder(value, index, count, returnWholeValue);
00361         }
00362 
00370         public static void SwapByteOrder(ref byte[] value, int index, int count)
00371         {
00372             value = Sylloge.Memory.SwapByteOrder(value, index, count, true);
00373         }
00374         */
00375     }
00376 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events