Exemple #1
0
        internal bool Read(FlagArray flags, ref int owner, out LockSlot <T> slot)
        {
            if (Read(ref owner, out slot))
            {
                return(true);
            }
            var resultLock = -1;

            foreach (var flag in flags.Flags)
            {
                if (!_slots.TryGet(flag, out var testSlot))
                {
                    continue;
                }

                if (slot?.CompareTo(testSlot) >= 0)
                {
                    continue;
                }

                slot       = testSlot;
                resultLock = flag;
            }
            if (Interlocked.CompareExchange(ref owner, resultLock, -1) != -1)
            {
                return(Read(ref owner, out slot));
            }
            return(slot != null);
        }
Exemple #2
0
 internal NeedleLock(LockContext <T> context)
 {
     _context  = context ?? throw new ArgumentNullException(nameof(context));
     _hashCode = base.GetHashCode();
     _capture  = new FlagArray(_context.Capacity);
     _owner    = -1;
 }
Exemple #3
0
        public static void Xor()
        {
            var a = new FlagArray(6)
            {
                [0] = false,
                [1] = false,
                [2] = true,
                [3] = true,
                [4] = false,
                [5] = true
            };
            var b = new FlagArray(4)
            {
                [0] = false,
                [1] = true,
                [2] = false,
                [3] = true
            };
            var c = a.Xor(b);

            Assert.AreEqual(false, c[0]);
            Assert.AreEqual(true, c[1]);
            Assert.AreEqual(true, c[2]);
            Assert.AreEqual(false, c[3]);
            Assert.AreEqual(false, c[4]);
            Assert.AreEqual(true, c[5]);
            Assert.AreEqual(6, c.Capacity);
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = c[c.Capacity]);
        }
Exemple #4
0
        public static void EnumerableConstructorWithCapacity()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = new FlagArray(8, new[] { -1 }));
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = new FlagArray(8, new[] { 9 }));

            var a = new FlagArray(8, new[] { 2, 3, 5 });

            Assert.AreEqual(false, a[0]);
            Assert.AreEqual(false, a[1]);
            Assert.AreEqual(true, a[2]);
            Assert.AreEqual(true, a[3]);
            Assert.AreEqual(false, a[4]);
            Assert.AreEqual(true, a[5]);
            Assert.AreEqual(false, a[6]);
            Assert.AreEqual(false, a[7]);
            Assert.AreEqual(8, a.Capacity);

            var b = new FlagArray(8, new[] { 1, 3 });

            Assert.AreEqual(false, b[0]);
            Assert.AreEqual(true, b[1]);
            Assert.AreEqual(false, b[2]);
            Assert.AreEqual(true, b[3]);
            Assert.AreEqual(false, b[4]);
            Assert.AreEqual(false, b[5]);
            Assert.AreEqual(false, b[6]);
            Assert.AreEqual(false, b[7]);
            Assert.AreEqual(8, b.Capacity);
        }
Exemple #5
0
        internal bool Read(FlagArray flags, ref int owner, out LockSlot <T> slot)
        {
            if (Read(ref owner, out slot))
            {
                return(true);
            }
            var resultLock = -1;

            foreach (var flag in flags.Flags)
            {
                LockSlot <T> testSlot;
                if (!_slots.TryGet(flag, out testSlot))
                {
                    continue;
                }
                if (ReferenceEquals(slot, null) || slot.CompareTo(testSlot) < 0)
                {
                    slot       = testSlot;
                    resultLock = flag;
                }
            }
            if (Interlocked.CompareExchange(ref owner, resultLock, -1) != -1)
            {
                return(Read(ref owner, out slot));
            }
            if (slot == null)
            {
                return(false);
            }
            return(true);
        }
Exemple #6
0
        public void CloneFlagArray()
        {
            var array = new FlagArray(16, true);
            var clone = CloneHelper <FlagArray> .GetCloner().Clone(array); // GenericCloner

            Assert.AreEqual(array, clone);
            Assert.IsFalse(ReferenceEquals(array, clone));
        }
Exemple #7
0
 internal NeedleLock(LockContext <T> context)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     _context  = context;
     _hashCode = base.GetHashCode();
     _capture  = new FlagArray(_context.Capacity);
     _owner    = -1;
 }
Exemple #8
0
        public static void Clone()
        {
            var x = new FlagArray(16)
            {
                [5] = true,
                [9] = true
            };
            var y = x.Clone();

            Assert.AreEqual(16, y.Capacity);
            Assert.AreEqual(true, y[5]);
            Assert.AreEqual(true, y[9]);
        }
Exemple #9
0
 internal NeedleLock(LockContext <T> context, T target)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
     if (target == null)
     {
         _hashCode = base.GetHashCode();
     }
     else
     {
         _target   = target;
         _hashCode = target.GetHashCode();
     }
     _capture = new FlagArray(_context.Capacity);
     _owner   = -1;
 }
Exemple #10
0
        public static void Not()
        {
            var b = new FlagArray(4)
            {
                [0] = false,
                [1] = true,
                [2] = false,
                [3] = true
            };
            var c = b.Not();

            Assert.AreEqual(true, c[0]);
            Assert.AreEqual(false, c[1]);
            Assert.AreEqual(true, c[2]);
            Assert.AreEqual(false, c[3]);
            Assert.AreEqual(4, c.Capacity);
            Assert.Throws <ArgumentOutOfRangeException>(() => _ = c[c.Capacity]);
        }
Exemple #11
0
 internal NeedleLock(LockContext <T> context, T target)
 {
     if (context == null)
     {
         throw new ArgumentNullException("context");
     }
     _context = context;
     if (ReferenceEquals(target, null))
     {
         _hashCode = base.GetHashCode();
     }
     else
     {
         _target   = target;
         _hashCode = target.GetHashCode();
     }
     _capture = new FlagArray(_context.Capacity);
     _owner   = -1;
 }
Exemple #12
0
        public static void SetAllAndContains()
        {
            var x = new FlagArray(new[] { 5, 9 });

            Assert.AreEqual(true, x[5]);
            Assert.AreEqual(true, x[9]);
            Assert.AreEqual(2, x.Count);
            Assert.AreEqual(true, x.Contains(true));
            Assert.AreEqual(true, x.Contains(false));
            x.SetAll(false);
            Assert.AreEqual(false, x.Contains(true));
            Assert.AreEqual(true, x.Contains(false));
            Assert.AreEqual(false, x[5]);
            Assert.AreEqual(false, x[9]);
            Assert.AreEqual(0, x.Count);
            x.SetAll(true);
            Assert.AreEqual(true, x.Contains(true));
            Assert.AreEqual(false, x.Contains(false));
            Assert.AreEqual(x.Capacity, x.Count);
        }
Exemple #13
0
        public static void CopyTo()
        {
            var x     = new FlagArray(new[] { 5, 9, 15 });
            var bits1 = new bool[x.Capacity];

            x.CopyTo(bits1);
            var test1 = new bool[x.Capacity];

            test1[5]  = true;
            test1[9]  = true;
            test1[15] = true;
            Assert.AreEqual(test1, bits1);
            var bits2 = new bool[15];

            x.CopyTo(bits2, 5, 10);
            var test2 = new bool[15];

            test2[10] = true;
            test2[14] = true;
            Assert.AreEqual(test2, bits2);
        }
Exemple #14
0
        public static void Flags()
        {
            var x = new FlagArray(new[] { 5, 9 });

            Assert.AreEqual(new[] { 5, 9 }, x.Flags);
        }