Files
stgsharp/编码器/StgSharpData/Encode/CodeCounter.cs
T

71 lines
2.0 KiB
C#

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<T>(string filePath) where T : struct
{
byte[] allByte = File.ReadAllBytes(filePath);
List<T> rawList = new List<T>();
fixed (byte* allBytePtr = allByte)
{
for (int i = 0; i < allByte.Length; i += Marshal.SizeOf<T>())
{
T t = new T();
t= *(T*)(allBytePtr + i);
rawList.Add(t);
}
}
return rawList.ToArray();
}
public static unsafe T[] SeparateCode<T>(byte[] stream) where T : struct
{
List<T> rawList = new List<T>();
fixed (byte* allBytePtr = stream)
{
for (int i = 0; i < stream.Length; i += Marshal.SizeOf<T>())
{
T t = new T();
t = *(T*)(allBytePtr + i);
rawList.Add(t);
}
}
return rawList.ToArray();
}
public static List<(T, ulong)> CountCodeFrequency<T>(T[] rawArray) where T : struct
{
Dictionary<T, ulong> frequencyCollection = new Dictionary<T, ulong>();
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();
}
}
}