FilterIterator 位掩码(或位域)

发布于 2024-10-23 22:19:50 字数 1383 浏览 1 评论 0原文

我正在努力处理位掩码(或者是位字段?)。我不知道该怎么做了。

我想创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-10-30 22:19:50

使用位掩码时,您需要将其值设置为 2 的幂。如果将 DIR 设置为 3,则意味着它与 DIR_NO_DOT 和 DOT 组合同义。

尝试将它们的值设置为如下所示:

const DOT  = 1;
const DIR  = 2;
const FILE = 4;

现在您可以使用表达式 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:

const DOT  = 1;
const DIR  = 2;
const FILE = 4;

Now you can check if an item is a DIR but not a DOT with the expression DIR & !DOT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文