Esempio n. 1
0
        /// <summary>
        /// Checks if any of the provided elements is in the filter.
        /// </summary>
        /// <param name="data">Data elements to check in the filter.</param>
        /// <param name="key">Key used for hashing the data elements.</param>
        /// <returns>true if at least one of the elements is in the filter, otherwise false.</returns>
        internal bool MatchAny(IEnumerable <byte[]> data, int dataCount, byte[] key)
        {
            if (data == null || dataCount == 0)
            {
                throw new ArgumentException("data can not be null or empty array.", nameof(data));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (N == 0)
            {
                return(false);
            }

            var hs = ConstructHashedSet(P, N, M, key, data, dataCount);

            var lastValue1 = 0UL;
            var lastValue2 = hs[0];
            var i          = 1;

            var bitStream = new BitStream(Data);
            var sr        = new GRCodedStreamReader(bitStream, P, 0);

            while (lastValue1 != lastValue2)
            {
                if (lastValue1 > lastValue2)
                {
                    if (i < hs.Length)
                    {
                        lastValue2 = hs[i];
                        i++;
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (lastValue2 > lastValue1)
                {
                    if (sr.TryRead(out var val))
                    {
                        lastValue1 = val;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if any of the provided elements is in the filter.
        /// </summary>
        /// <param name="data">Data elements to check in the filter.</param>
        /// <param name="key">Key used for hashing the data elements.</param>
        /// <returns>true if at least one of the elements is in the filter, otherwise false.</returns>
        public bool MatchAny(IEnumerable <byte[]> data, byte[] key)
        {
            if (data == null || !data.Any())
            {
                throw new ArgumentException("data can not be null or empty array.", nameof(data));
            }

            var hs = ConstructHashedSet(P, N, M, key, data);

            var lastValue1 = 0UL;
            var lastValue2 = hs[0];
            var i          = 1;

            var bitStream = new BitStream(Data);
            var sr        = new GRCodedStreamReader(bitStream, P, 0);

            try
            {
                while (lastValue1 != lastValue2)
                {
                    if (lastValue1 > lastValue2)
                    {
                        if (i < hs.Count)
                        {
                            lastValue2 = hs[i];
                            i++;
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else if (lastValue2 > lastValue1)
                    {
                        var val = sr.Read();
                        lastValue1 = val;
                    }
                }
            }
            catch (InvalidOperationException)             // end-of-stream
            {
                return(false);
            }

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Checks if any of the provided elements is in the filter.
        /// </summary>
        /// <param name="data">Data elements to check in the filter.</param>
        /// <param name="key">Key used for hashing the data elements.</param>
        /// <returns>true if at least one of the elements is in the filter, otherwise false.</returns>
        internal bool MatchAny(IEnumerable <byte[]> data, int dataCount, byte[] key)
        {
            if (data == null || dataCount == 0)
            {
                throw new ArgumentException("data can not be null or empty array.", nameof(data));
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            var hs = ConstructHashedSet(P, N, M, key, data, dataCount);

            var bitStream = new BitStream(Data);
            var sr        = new GRCodedStreamReader(bitStream, P, 0);

            while (sr.TryRead(out var val))
            {
                var dataIndex = 0;
                while (true)
                {
                    if (dataIndex == dataCount)
                    {
                        return(false);
                    }

                    if (hs[dataIndex] == val)
                    {
                        return(true);
                    }

                    if (hs[dataIndex] > val)
                    {
                        break;
                    }

                    dataIndex++;
                }
            }

            return(false);
        }