public static int GetUnsequencedHashCode <T>(this SCG.ICollection <T> collection, SCG.IEqualityComparer <T> equalityComparer = null)
        {
            if (collection == null)
            {
                return(0); // TODO: Better default value? H1/H2/H3?
            }

            if (equalityComparer == null)
            {
                equalityComparer = SCG.EqualityComparer <T> .Default;
            }

            var hashCode = 0; // TODO: Better intial value?

            // Does not use Linq.Sum() as it throws an exception if it overflows
            foreach (var item in collection)
            {
                var h = (uint)equalityComparer.GetHashCode(item);

                // We need at least three products, as two is too few
                hashCode += (int)((h * H1 + 1) ^ (h * H2) ^ (h * H3 + 2));
            }

            return(hashCode);
        }
 public override void ICollection_Generic_Contains_DefaultValueOnCollectionContainingDefaultValue(int count)
 {
     if (DefaultValueAllowed && !IsReadOnly && !AddRemoveClear_ThrowsNotSupported)
     {
         SCG.ICollection <string> collection = GenericICollectionFactory(count);
         AssertExtensions.Throws <ArgumentOutOfRangeException>("item", /*null,*/ () => collection.Add(default(string)));
     }
 }
 public override void ICollection_Generic_Remove_DefaultValueContainedInCollection(int count)
 {
     if (!IsReadOnly && !AddRemoveClear_ThrowsNotSupported && DefaultValueAllowed && !Enumerable.Contains(InvalidValues, default(string)))
     {
         int seed = count * 21;
         SCG.ICollection <string> collection = GenericICollectionFactory(count);
         Assert.False(collection.Remove(default(string)));
     }
 }
        protected static void WriteCollection <T>(Stream stream, SCG.ICollection <T> objs) where T : BitcoinSerializable, new()
        {
            ContractsCommon.NotNull(objs, "objs");
            ContractsCommon.CanWriteToStream(stream, VarIntByteSize(objs.Count) + objs.Sum(o => o.SerializedByteSize));

            WriteVarInt(stream, objs.Count);
            foreach (var obj in objs)
            {
                obj.Serialize(stream);
            }
        }
 public override void ICollection_Generic_Add_DefaultValue(int count)
 {
     // Adding an item to a TreeSubset does nothing - it updates the parent.
     if (DefaultValueAllowed && !IsReadOnly && !AddRemoveClear_ThrowsNotSupported && CanAddDefaultValue)
     {
         SCG.ICollection <T> collection = GenericICollectionFactory(count);
         collection.Add(default(T));
         Assert.Equal(count + 1, collection.Count); // collection is also updated.
         Assert.Equal(count + 1, OriginalSet.Count);
     }
 }
Example #6
0
        public void Paths()
        {
            WeightedGraph <int> graph = new WeightedGraph <int>
            {
                (1, 2), (2, 3), (3, 4), (4, 5)
            };

            DepthFirstPaths <int> dfPaths = new DepthFirstPaths <int>(graph, 1);

            SCG.ICollection <WeightedEdge <int> > path = dfPaths.GetPathTo(5);

            Assert.AreEqual(4, path.Count, "Path count");

            Assert.AreEqual(new WeightedEdge <int>(4, 5), path.ElementAt(3));
            Assert.AreEqual(new WeightedEdge <int>(3, 4), path.ElementAt(2));
            Assert.AreEqual(new WeightedEdge <int>(2, 3), path.ElementAt(1));
            Assert.AreEqual(new WeightedEdge <int>(1, 2), path.ElementAt(0));
        }
Example #7
0
 public void TrySCGColl(SCG.ICollection <double> coll)
 {
     // All members of SCG.ICollection<T>
     Assert.AreEqual(0, coll.Count);
     double[] arr = { };
     coll.CopyTo(arr, 0);
     Assert.IsFalse(coll.IsReadOnly);
     coll.Add(2.3);
     coll.Add(3.2);
     Assert.AreEqual(2, coll.Count);
     Assert.IsTrue(coll.Contains(2.3));
     Assert.IsFalse(coll.Contains(3.1));
     Assert.IsFalse(coll.Remove(3.1));
     Assert.IsTrue(coll.Remove(3.2));
     Assert.IsFalse(coll.Contains(3.1));
     Assert.AreEqual(1, coll.Count);
     coll.Clear();
     Assert.AreEqual(0, coll.Count);
     Assert.IsFalse(coll.Remove(3.1));
 }