示例#1
0
        public void ShardsAsExpected()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            Assert.AreEqual(1, shardedList.RegionCount);

            var subRegion1 = shardedList.Shard(0, 2);

            Assert.AreEqual(1, subRegion1.RegionCount);
            var subRegion2 = shardedList.Shard(2, 2);

            Assert.AreEqual(1, subRegion2.RegionCount);
            var subRegion3 = shardedList.Shard(4, 2);

            Assert.AreEqual(1, subRegion3.RegionCount);

            Assert.AreEqual(3, shardedList.RegionCount);

            var subRegion1a = subRegion1.Shard(0, 1);
            var subRegion1b = subRegion1.Shard(1, 1);

            Assert.AreEqual(2, subRegion1.RegionCount);

            Assert.AreEqual(3, shardedList.RegionCount);
        }
示例#2
0
        public void ResizingSingleShardUp()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            shardedList.Resize(7);
            Assert.AreEqual(new[] { 1, 2, 3, 4, 5, 6, 0 }, shardedList.ToArray());
        }
示例#3
0
        public ShardedList <T> Shard(int localOffset, int length)
        {
            Asserts.Assert(length > 0, "Tried to shard a region of size 0!");
            Asserts.Assert(localOffset + length <= this.Length,
                           "Tried to shard a region that extends beyond the size of this shard!");

            if (localOffset == 0 && length == this.Length)
            {
                return(this);
            }

            this.GetRegionAtLocalOffset_(localOffset,
                                         out var region,
                                         out var regionOffset,
                                         out var regionIndex);

            var spanRegion = region as SpanShardedList;

            Asserts.Assert(spanRegion != null,
                           "Tried to shard a previously sharded region.");

            var regionLocalOffset = localOffset - regionOffset;

            Asserts.Assert(regionLocalOffset + length <= region.Length,
                           "New shard spans multiple regions.");

            var beforeLength    = regionLocalOffset;
            var newRegionLength = length;
            var afterLength     = region.Length - (beforeLength + newRegionLength);

            this.regions_.RemoveAt(regionIndex);

            var regionImpl = spanRegion !.Impl;

            if (afterLength > 0)
            {
                var afterSpan =
                    regionImpl.Sub(beforeLength + newRegionLength, afterLength);
                this.regions_.Insert(regionIndex,
                                     new SpanShardedList(this, afterSpan));
            }

            var newRegionSpan = regionImpl.Sub(beforeLength, newRegionLength);
            var newRegion     = new ShardedList <T>(this, newRegionSpan);

            this.regions_.Insert(regionIndex, newRegion);

            if (beforeLength > 0)
            {
                var beforeSpan = regionImpl.Sub(0, beforeLength);
                this.regions_.Insert(regionIndex,
                                     new SpanShardedList(this, beforeSpan));
            }

            this.UpdateLength_();

            //this.MergeNeighboringRawRegions_();

            return(newRegion);
        }
示例#4
0
        public void DoubleShardingThrows()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            shardedList.Shard(2, 4);

            Assert.Throws <AssertException>(() => shardedList.Shard(2, 4));
        }
示例#5
0
        public void ShardingFullListReturnsSelf()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            var subRegion = shardedList.Shard(0, 6);

            Assert.AreSame(shardedList, subRegion);
        }
示例#6
0
            public RawShardedList(ShardedList <T> parent, T[] impl)
            {
                this.Impl = impl;

                this.Parent       = parent;
                this.GlobalOffset =
                    new ShardedListAddress(() => parent.LocateRegion_(this));
            }
示例#7
0
        public void ShardingOverRegionThrows()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            shardedList.Shard(0, 2);
            shardedList.Shard(4, 2);

            Assert.Throws <AssertException>(() => shardedList.Shard(2, 3));
        }
示例#8
0
        public void IteratingOverSubRegion()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            var subRegion = shardedList.Shard(1, 4);

            Assert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, shardedList.ToArray());
            Assert.AreEqual(new[] { 2, 3, 4, 5 }, subRegion.ToArray());
        }
示例#9
0
        public void ResizingSubRegionUp()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            var subRegion = shardedList.Shard(1, 4);

            subRegion.Resize(5);

            Assert.AreEqual(new[] { 1, 2, 3, 4, 5, 0, 6 }, shardedList.ToArray());
            Assert.AreEqual(new[] { 2, 3, 4, 5, 0 }, subRegion.ToArray());
        }
示例#10
0
        public void IteratingOverNeighboringRegions()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            var subRegion1 = shardedList.Shard(0, 2);
            var subRegion2 = shardedList.Shard(2, 2);
            var subRegion3 = shardedList.Shard(4, 2);

            Assert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, shardedList.ToArray());
            Assert.AreEqual(new[] { 1, 2 }, subRegion1.ToArray());
            Assert.AreEqual(new[] { 3, 4 }, subRegion2.ToArray());
            Assert.AreEqual(new[] { 5, 6 }, subRegion3.ToArray());
        }
示例#11
0
        private ShardedList(
            ShardedList <T>?parent,
            ISpannable <T> ts
            )
        {
            this.parent_ = parent;
            this.regions_.Add(new SpanShardedList(this, ts));

            this.UpdateLength_();

            if (parent != null)
            {
                this.GlobalOffset = new ShardedListAddress(
                    () => parent.LocateRegion_(this));
            }
            else
            {
                this.GlobalOffset = new NullShardedListAddress();
            }
        }
示例#12
0
        public void ShardingNegativeThrows()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            Assert.Throws <AssertException>(() => shardedList.Shard(-1, 1));
        }
示例#13
0
        public void IteratingOverSingleShard()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            Assert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, shardedList.ToArray());
        }
示例#14
0
        public void ShardingLargeThrows()
        {
            var shardedList = ShardedList <int> .From(1, 2, 3, 4, 5, 6);

            Assert.Throws <AssertException>(() => shardedList.Shard(0, 7));
        }