using StgSharp.Data; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; namespace StgSharp.Data { public static class CodeSeparator { public static unsafe T[] SeparateCode(string filePath) where T : struct { byte[] allByte = File.ReadAllBytes(filePath); List rawList = new List(); fixed (byte* allBytePtr = allByte) { for (int i = 0; i < allByte.Length; i += Marshal.SizeOf()) { T t = new T(); t= *(T*)(allBytePtr + i); rawList.Add(t); } } return rawList.ToArray(); } public static unsafe T[] SeparateCode(byte[] stream) where T : struct { List rawList = new List(); fixed (byte* allBytePtr = stream) { for (int i = 0; i < stream.Length; i += Marshal.SizeOf()) { T t = new T(); t = *(T*)(allBytePtr + i); rawList.Add(t); } } return rawList.ToArray(); } public static List<(T, ulong)> CountCodeFrequency(T[] rawArray) where T : struct { Dictionary frequencyCollection = new Dictionary(); foreach (var item in rawArray) { if (frequencyCollection.ContainsKey(item)) { frequencyCollection[item] += 1; } else { frequencyCollection.Add(item, 1); } } return frequencyCollection.OrderByDescending(pair=>pair.Value) .Select(pair=>(pair.Key, pair.Value)).ToList(); } } }