コード例 #1
0
        /// <summary>
        /// Intersect with a Bloom filter.
        /// </summary>
        /// <param name="bloomFilterData"></param>
        /// <remarks>Results in only retaining the keys the filters have in common.</remarks>
        public virtual void Intersect(IBloomFilterData bloomFilterData)
        {
            if (bloomFilterData == null)
            {
                return;
            }
            var foldedSize = _configuration.FoldingStrategy?.GetFoldFactors(_blockSize, bloomFilterData.BlockSize);

            if (bloomFilterData.BlockSize != BlockSize &&
                foldedSize == null)
            {
                throw new ArgumentException("Bloom filters of different sizes cannot be intersected.", nameof(bloomFilterData));
            }
            var otherBitArray = new FastBitArray(bloomFilterData.Bits ?? EmptyByteArray)
            {
                Length = (int)bloomFilterData.BlockSize
            };

            if (foldedSize != null &&
                (foldedSize.Item2 != 1 ||
                 foldedSize.Item1 != 1))
            {
                if (foldedSize.Item1 != 1)
                {
                    Fold((uint)foldedSize.Item1, true);
                }
                if (foldedSize.Item2 != 1)
                {
                    for (var i = 0; i < _data.Length; i++)
                    {
                        _data.Set(i, _data.Get(i) & GetFolded(otherBitArray, i, (uint)foldedSize.Item2));
                    }
                    ItemCount = EstimateItemCount(_data, _hashFunctionCount);
                    return;
                }
            }
            _data     = _data.And(otherBitArray);
            ItemCount = EstimateItemCount(_data, _hashFunctionCount);
        }