C# 通过字节数组键快速获取值(类似于“Dictionary”lt;byte[16],int>;)?
在我的程序中,我有非常大(许多terrabytes)的数据流作为输入,从中我需要得到键字节数组(例如16字节-一些伪GUID),通过该键我需要得到一些其他键整数,通过第二个键我需要在内存中做一些计算。但我不知道如何快速获得“第二把钥匙” 在开始处理这个“非常大的数据流”之前,我可以获得所有“byte[16]->int”关联。 Dictionary看起来很快,但它不能正确地使用字节[16]作为键-它只是检查对象引用的相等性(在我的位置中,它总是相等的,因为它只是缓冲区对象),但我需要使用真实的字节数组值作为键,并且这样做很快(每秒计数)。。。如何做到这一点C# 通过字节数组键快速获取值(类似于“Dictionary”lt;byte[16],int>;)?,c#,dictionary,bytearray,hashtable,binary-search-tree,C#,Dictionary,Bytearray,Hashtable,Binary Search Tree,在我的程序中,我有非常大(许多terrabytes)的数据流作为输入,从中我需要得到键字节数组(例如16字节-一些伪GUID),通过该键我需要得到一些其他键整数,通过第二个键我需要在内存中做一些计算。但我不知道如何快速获得“第二把钥匙” 在开始处理这个“非常大的数据流”之前,我可以获得所有“byte[16]->int”关联。 Dictionary看起来很快,但它不能正确地使用字节[16]作为键-它只是检查对象引用的相等性(在我的位置中,它总是相等的,因为它只是缓冲区对象),但我需要使用真实的字节
嗯。。。我已经在尝试基于二进制搜索实现自己的关联数组类。。或者有任何标准的字典替代品,可以使用大于4字节的int作为键?与通用
字典一起使用。您可以使用此类来比较键
这将让您开始,性能可能会得到优化:
public class MyComparer : IEqualityComparer<byte[]> {
public bool Equals(byte[] a, byte[] b) {
return a.SequenceEqual(b);
}
public int GetHashCode(byte[] key) {
if (key == null)
throw new ArgumentNullException("key");
return key.Sum(b => b);
}
}
// usage:
myDict = new Dictionary(myByteArray, myInt, new MyComparer());
公共类MyComparer:IEqualityComparer{
公共布尔等于(字节[]a,字节[]b){
返回a.a(b);
}
public int GetHashCode(字节[]键){
if(key==null)
抛出新的ArgumentNullException(“键”);
返回key.Sum(b=>b);
}
}
//用法:
myDict=新字典(myByteArray,myInt,new MyComparer());
与通用词典一起使用。您可以使用此类来比较键
这将让您开始,性能可能会得到优化:
public class MyComparer : IEqualityComparer<byte[]> {
public bool Equals(byte[] a, byte[] b) {
return a.SequenceEqual(b);
}
public int GetHashCode(byte[] key) {
if (key == null)
throw new ArgumentNullException("key");
return key.Sum(b => b);
}
}
// usage:
myDict = new Dictionary(myByteArray, myInt, new MyComparer());
公共类MyComparer:IEqualityComparer{
公共布尔等于(字节[]a,字节[]b){
返回a.a(b);
}
public int GetHashCode(字节[]键){
if(key==null)
抛出新的ArgumentNullException(“键”);
返回key.Sum(b=>b);
}
}
//用法:
myDict=新字典(myByteArray,myInt,new MyComparer());
与通用词典一起使用。您可以使用此类来比较键
这将让您开始,性能可能会得到优化:
public class MyComparer : IEqualityComparer<byte[]> {
public bool Equals(byte[] a, byte[] b) {
return a.SequenceEqual(b);
}
public int GetHashCode(byte[] key) {
if (key == null)
throw new ArgumentNullException("key");
return key.Sum(b => b);
}
}
// usage:
myDict = new Dictionary(myByteArray, myInt, new MyComparer());
公共类MyComparer:IEqualityComparer{
公共布尔等于(字节[]a,字节[]b){
返回a.a(b);
}
public int GetHashCode(字节[]键){
if(key==null)
抛出新的ArgumentNullException(“键”);
返回key.Sum(b=>b);
}
}
//用法:
myDict=新字典(myByteArray,myInt,new MyComparer());
与通用词典一起使用。您可以使用此类来比较键
这将让您开始,性能可能会得到优化:
public class MyComparer : IEqualityComparer<byte[]> {
public bool Equals(byte[] a, byte[] b) {
return a.SequenceEqual(b);
}
public int GetHashCode(byte[] key) {
if (key == null)
throw new ArgumentNullException("key");
return key.Sum(b => b);
}
}
// usage:
myDict = new Dictionary(myByteArray, myInt, new MyComparer());
公共类MyComparer:IEqualityComparer{
公共布尔等于(字节[]a,字节[]b){
返回a.a(b);
}
public int GetHashCode(字节[]键){
if(key==null)
抛出新的ArgumentNullException(“键”);
返回key.Sum(b=>b);
}
}
//用法:
myDict=新字典(myByteArray,myInt,new MyComparer());
或者,您可以编写一些代码来表示您的类型,如下所示
public struct PseudoGuid : IReadOnlyList<byte>, IEquatable<PseudoGuid>
{
private readonly byte[] value;
public PseudoGuid(IList<byte> value)
{
if (value.Count != 16)
{
throw new ArgumentException(...
}
this.value = value.ToArray();
}
public byte this[int index]
{
get
{
if (this.value != null)
{
return this.value[index]
}
return default(byte);
}
}
public bool Equals(PseudoGuid other)
{
return this.SequenceEquals(other);
}
public override string ToString()
{
var result = new StringBuilder(32);
for (var i = 0; i < 16; i++)
{
result.Append(this[i].ToString("X2"));
}
return result.ToString();
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is PseudoGuid && this.Equals((PseudoGuid)obj);
}
public override int GetHashCode()
{
if (this.value == null)
{
return 0;
}
return BitConvertor.ToInt32(this.value, 0) ^
BitConvertor.ToInt32(this.value, 4) ^
BitConvertor.ToInt32(this.value, 8) ^
BitConvertor.ToInt32(this.value, 12);
}
public static bool operator ==(PseudoGuid left, PseudoGuid right)
{
return left.Equals(right);
}
public static bool operator !=(PseudoGuid left, PseudoGuid right)
{
return !left.Equals(right);
}
public int Count
{
get
{
return 16;
}
}
public IEnumerator<byte> GetEnumerator()
{
if (this.value != null)
{
return ((IList<byte>)this.value).GetEnumerator();
}
return Enumerable.Repeat(default(byte), 16).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
public-struct-PseudoGuid:IReadOnlyList,IEquatable
{
私有只读字节[]值;
公共伪GUID(IList值)
{
如果(value.Count!=16)
{
抛出新的ArgumentException(。。。
}
this.value=value.ToArray();
}
公共字节此[int索引]
{
得到
{
如果(this.value!=null)
{
返回此.value[索引]
}
返回默认值(字节);
}
}
公共布尔等于(伪GUID其他)
{
返回此。SequenceEquals(其他);
}
公共重写字符串ToString()
{
var结果=新的StringBuilder(32);
对于(变量i=0;i<16;i++)
{
result.Append(this[i].ToString(“X2”));
}
返回result.ToString();
}
公共覆盖布尔等于(对象对象对象)
{
if(ReferenceEquals(null,obj))
{
返回false;
}
返回obj为PseudoGuid&&this.Equals((PseudoGuid)obj);
}
公共覆盖int GetHashCode()
{
if(this.value==null)
{
返回0;
}
返回BitConverter.ToInt32(this.value,0)^
位转换器.ToInt32(this.value,4)^
位转换器.ToInt32(this.value,8)^
位转换器.ToInt32(该值为12);
}
公共静态布尔运算符==(伪GUID左,伪GUID右)
{
返回左。等于(右);
}
公共静态布尔运算符!=(伪GUID左,伪GUID右)
{
返回!左。等于(右);
}
公共整数计数
{
得到
{
返回16;
}
}
公共IEnumerator GetEnumerator()
{
如果(this.value!=null)
{
返回((IList)this.value).GetEnumerator();
}
返回Enumerable.Repeat(默认值(字节),16).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
返回此.GetEnumerator();
}
}
或者,您可以编写一些代码来表示您的类型,如下所示
public struct PseudoGuid : IReadOnlyList<byte>, IEquatable<PseudoGuid>
{
private readonly byte[] value;
public PseudoGuid(IList<byte> value)
{
if (value.Count != 16)
{
throw new ArgumentException(...
}
this.value = value.ToArray();
}
public byte this[int index]
{
get
{
if (this.value != null)
{
return this.value[index]
}
return default(byte);
}
}
public bool Equals(PseudoGuid other)
{
return this.SequenceEquals(other);
}
public override string ToString()
{
var result = new StringBuilder(32);
for (var i = 0; i < 16; i++)
{
result.Append(this[i].ToString("X2"));
}
return result.ToString();
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
return obj is PseudoGuid && this.Equals((PseudoGuid)obj);
}
public override int GetHashCode()
{
if (this.value == null)
{
return 0;
}
return BitConvertor.ToInt32(this.value, 0) ^
BitConvertor.ToInt32(this.value, 4) ^
BitConvertor.ToInt32(this.value, 8) ^
BitConvertor.ToInt32(this.value, 12);
}
public static bool operator ==(PseudoGuid left, PseudoGuid right)
{
return left.Equals(right);
}
public static bool operator !=(PseudoGuid left, PseudoGuid right)
{
return !left.Equals(right);
}
public int Count
{
get
{
return 16;
}
}
public IEnumerator<byte> GetEnumerator()
{
if (this.value != null)
{
return ((IList<byte>)this.value).GetEnumerator();
}
return Enumerable.Repeat(default(byte), 16).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
public-struct-PseudoGuid:IReadOnlyList,IEquatable
{
私有只读字节[]值;
公共伪GUID(IList值)
{
如果(value.Count!=16)
{
抛出新的ArgumentException(。。。
}
this.value=value.ToArray();
}
公共字节此[int索引]
{
得到
{
如果(this.value!=null)
{
返回此.value[索引]
}
返回默认值(字节);
}
}
公共布尔等于(伪GUID其他)
{