FilterIterator 位掩码(或位域)
我正在努力处理位掩码(或者是位字段?)。我不知道该怎么做了。
我想创建一个 DirectoryFilterIterator 来接受要过滤的内容的标志。我想我应该使用这些位:
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
因为 DOT
也被认为是 DIR
我希望能够区分这两者。如果我是正确的,我认为类似的事情应该是可能的:
DirectoryFilterIterator::DIR & ~DirectoryFilterIterator::DOT
换句话说,这应该过滤掉DIR
,除非它是DOT
。但我完全陷入了如何让过滤发挥作用(在 accept
方法中):
class DirectoryFilterIterator
extends FilterIterator
{
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
protected $_filter;
public function __construct( DirectoryIterator $iterator, $filter = self::DIR )
{
parent::__construct( $iterator );
$this->_filter = $filter;
}
public function accept()
{
$item = $this->getInnerIterator()->current();
return (
!( ( $this->_filter & self::DOT ) == self::DOT && $item->isDot() ) &&
!( ( $this->_filter & self::DIR ) == self::DIR && $item->isDir() ) &&
!( ( $this->_filter & self::FILE ) == self::FILE && $item->isFile() )
);
}
}
...特别是因为我正在进行的所有否定,我有点迷失了。我怎样才能让它正常工作?
I'm struggling with bitmasks (or is it bitfields?). I'm not sure how to do it anymore.
I want to create a DirectoryFilterIterator that accepts flags of what to filter. I thought I'd use these bits for that:
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
Because a DOT
is also considered a DIR
I'd like to be able to distinguish between those two also. If I'm correct I thought something like that should be possible like this:
DirectoryFilterIterator::DIR & ~DirectoryFilterIterator::DOT
In other words, this should filter out DIR
unless it's a DOT
. But I'm totally stuck on how to get the filtering to work (in the accept
method):
class DirectoryFilterIterator
extends FilterIterator
{
const DIR_NO_DOT = 1;
const DOT = 2;
const DIR = 3;
const FILE = 4;
protected $_filter;
public function __construct( DirectoryIterator $iterator, $filter = self::DIR )
{
parent::__construct( $iterator );
$this->_filter = $filter;
}
public function accept()
{
$item = $this->getInnerIterator()->current();
return (
!( ( $this->_filter & self::DOT ) == self::DOT && $item->isDot() ) &&
!( ( $this->_filter & self::DIR ) == self::DIR && $item->isDir() ) &&
!( ( $this->_filter & self::FILE ) == self::FILE && $item->isFile() )
);
}
}
...especially because of all the negating I have going on, I'm kind of lost. How can I get this to work properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用位掩码时,您需要将其值设置为 2 的幂。如果将 DIR 设置为 3,则意味着它与 DIR_NO_DOT 和 DOT 组合同义。
尝试将它们的值设置为如下所示:
现在您可以使用表达式
DIR & 检查某个项目是否是 DIR 而不是 DOT。 !点
。When using bitmasks you need to set their values to powers of two. If you set DIR to 3, it means it's synonymous to both DIR_NO_DOT and DOT combined.
Try setting their values to something like this:
Now you can check if an item is a DIR but not a DOT with the expression
DIR & !DOT
.