Beispiel #1
0
        public bool Exchange(int index, T item, out T previous)
        {
            var found = BucketHelper.Null;

            previous = default(T);
            var result = _bucketCore.DoMayIncrement
                         (
                index,
                (ref object target) =>
            {
                found = Interlocked.Exchange(ref target, (object)item ?? BucketHelper.Null);
                return(found == null);
            }
                         );

            if (result)
            {
                Interlocked.Increment(ref _count);
                return(true);
            }
            if (found != BucketHelper.Null)
            {
                previous = (T)found;
            }
            return(false);
        }
Beispiel #2
0
        public Bucket(IEnumerable <T> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            _bucketCore = new BucketCore(7);
            var index = 0;

            foreach (var item in source)
            {
                var copy = item;
                _bucketCore.DoMayIncrement
                (
                    index,
                    (ref object target) => Interlocked.Exchange(ref target, (object)copy ?? BucketHelper.Null) == null
                );
                index++;
                _count++;
            }
        }