是否有一个通用的(类型安全)BitArray在.NET? [英] Is there a generic (type-safe) BitArray in .NET?

查看:107
本文介绍了是否有一个通用的(类型安全)BitArray在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在.NET泛型BitArray?我只发现了非通用的。

是否会有一个通用的BitArray? (即会是合理的?)


编辑:

也许我应该说类型安全不是通用的。

基本上当你枚举类型对象,岂不是 INT 布尔?或者在其他成员调查员提供的其中之一?


例:

 的foreach(在myBitArray布尔位)
{

}
 


编辑:

我刚刚查了 BitArray 类的枚举,但一切都返回对象除了 .Current 属性:

 公共虚拟对象当前
 

解决方案

<打击>不,没有。

我甚至不知道什么是BitArray的部分将是通用的,如果有之一。

这不会是很难创建一个扩展方法取 BitArray 并返回<击> 布尔[] 名单,其中,布尔&GT; 用在循环 BitArray 。该循环将不涉及拳击,因为你会使用 BitArray 的索引,而<打击> 布尔[] 名单,其中,布尔&GT; 没有拳击以及可以列举

例如扩展方法:

 静态列表&LT;布尔&GT;了ToList(此BitArray BA){
名单&LT;布尔&GT; L =新的名单,其中,布尔&GT;(ba.Count);
的for(int i = 0; I&LT; ba.Count;我++){
l.Add(BA [I]);
}
返回L;
}
 

我发现从一个快速的基准(好奇心战胜了我)是的foreach(布尔b在myBitArray.ToList())把75%到85%的时间该的foreach(布尔b在myBitArray)。创建该列表中的每个时间。创建列表一次遍历了很多次了20%,至该的foreach(布尔b在myBitArray)25%的时间了。你只能利用这一点,如果你需要遍历布尔值多次和知道的,他们不会从时间更改你叫 myBitArray.ToList()

的foreach(布尔b在Enumerable.Cast&LT;布尔(myBitArray))花的时间150%的的foreach(布尔b在myBitArray) 了。

另一个编辑:我会说,既然是游戏,它可能是有一定道理的,你要尽一切力量,有一个很瘦的迭代,没有装箱/拆箱,甚至如果这意味着编写自己的 BitArray 。你可以节省时间和使用反射以<打击>复制大多数学习 BitArray 的code,因为类是密封的(不能继承并添加功能),以防万一有位变换优化学习的榜样。

编辑:来袭复制code反射出的建议。有些东西,如迭代器和闭包,产量,你不想直接复制反正怪异产生code。

Is there a generic BitArray in .NET? I only found the non-generic one.

Can there be a generic BitArray? (i.e. would it be reasonable?)


Edit:

Maybe I should have said type-safe not generic.

Basically when you enumerate the type as object, should it not be int or bool? Or one of them provided in another member enumerator?


Example:

foreach (bool bit in myBitArray)
{

}


Edit:

I just checked the enumerator of the BitArray class, but everything returns an object except .Current property:

public virtual object Current

解决方案

No, there isn't.

I'm not even sure what part of a BitArray would be generic if there were one.

It wouldn't be hard to create an extension method to take the BitArray and return a bool[] or List<bool> using a for loop on the BitArray. The for loop would not involve boxing since you would be using the BitArray's indexer, and the bool[] List<bool> could be enumerated without boxing as well.

Example extension method:

static List<bool> ToList( this BitArray ba ) {
	List<bool> l = new List<bool>(ba.Count);
	for ( int i = 0 ; i < ba.Count ; i++ ) {
		l.Add( ba[ i ] );
	}
	return l;
}

What I found from a quick benchmark (curiosity overcame me) was that foreach (bool b in myBitArray.ToList()) took 75% to 85% of the time that foreach (bool b in myBitArray). That creates the list each time. Creating the list once and iterating over it many times took 20% to 25% of the time that foreach (bool b in myBitArray) took. You could only take advantage of that if you need to iterate over the bool values multiple times and know that they won't have changed from the time you called myBitArray.ToList().

foreach (bool b in Enumerable.Cast<bool(myBitArray)) took 150% of the time that foreach (bool b in myBitArray) took.

Yet another edit: I'd say that since it is a game, it probably does make sense for you to do whatever it takes to have a very lean iteration with no boxing/unboxing, even if that means writing your own BitArray. You could save time and use Reflector to copy most of study BitArray's code since the class is sealed (can't inherit and add functionality), just in case there are bit-twiddling optimizations to learn from.

Edit: Struck the suggestion to copy code out of Reflector. Some things, like iterators and closures, yield weird generated code that you don't want to copy directly anyway.

这篇关于是否有一个通用的(类型安全)BitArray在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆