Esempio n. 1
0
        private static bool FindMismatchedElement <T>(IEnumerable <T> actual, T[] expected, Func <T, T, bool> equality, out int expectedCount, out int actualCount,
                                                      out T mismatchedElement)
        {
            List <ElementCount <T> > expectedElementCounts = GetElementCounts(expected, equality);
            List <ElementCount <T> > actualElementCounts   = GetElementCounts(actual, equality);

            foreach (ElementCount <T> expectedElement in expectedElementCounts)
            {
                ElementCount <T> actualElement = actualElementCounts.Find(e => equality(e.Element, expectedElement.Element));

                if (actualElement == null)
                {
                    expectedCount     = expectedElement.Count;
                    actualCount       = 0;
                    mismatchedElement = expectedElement.Element;
                    return(true);
                }

                if (expectedElement.Count != actualElement.Count)
                {
                    expectedCount     = expectedElement.Count;
                    actualCount       = actualElement.Count;
                    mismatchedElement = expectedElement.Element;
                    return(true);
                }
            }

            expectedCount     = 0;
            actualCount       = 0;
            mismatchedElement = default(T);
            return(false);
        }
Esempio n. 2
0
        public int CompareTo(Type other)
        {
            // Make sure that Pointer parameters are sorted last to avoid bug [#1098].
            // The rest of the comparisons help maintain a stable order (useful for source control).
            // Note that CompareTo is stricter than Equals and that there is code in
            // DelegateCollection.Add that depends on this fact.
            int result = this.CurrentType.CompareTo(other.CurrentType);

            if (result == 0)
            {
                result = Pointer.CompareTo(other.Pointer); // Must come after array/ref, see issue [#1098]
            }
            if (result == 0)
            {
                result = Reference.CompareTo(other.Reference);
            }
            if (result == 0)
            {
                result = Array.CompareTo(other.Array);
            }
            // Note: CLS-compliance and element counts
            // are used for comparison calculations, in order
            // to maintain a stable sorting order, even though
            // they are not used in equality calculations.
            if (result == 0)
            {
                result = CLSCompliant.CompareTo(other.CLSCompliant);
            }
            if (result == 0)
            {
                result = ElementCount.CompareTo(other.ElementCount);
            }
            return(result);
        }
Esempio n. 3
0
        public bool SetEquals(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            HashSet <T> otherAsSet = other as HashSet <T>;

            if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
            {
                if (_count != otherAsSet.Count)
                {
                    return(false);
                }
                return(ContainsAllElements(otherAsSet));
            }
            else
            {
                ICollection <T> otherAsCollection = other as ICollection <T>;
                if (otherAsCollection != null)
                {
                    if (_count == 0 && otherAsCollection.Count > 0)
                    {
                        return(false);
                    }
                }
                ElementCount result = CheckUniqueAndUnfoundElements(other, true);
                return(result.uniqueCount == _count && result.unfoundCount == 0);
            }
        }
Esempio n. 4
0
        public int CompareTo(Type other)
        {
            // Make sure that Pointer parameters are sorted last to avoid bug [#1098].
            // The rest of the comparisons are not important, but they are there to
            // guarantee a stable order between program executions.
            int result = this.CurrentType.CompareTo(other.CurrentType);

            if (result == 0)
            {
                result = Pointer.CompareTo(other.Pointer);
            }
            if (result == 0)
            {
                result = Reference.CompareTo(other.Reference);
            }
            if (result == 0)
            {
                result = Array.CompareTo(other.Array);
            }
            if (result == 0)
            {
                result = CLSCompliant.CompareTo(other.CLSCompliant);
            }
            if (result == 0)
            {
                result = ElementCount.CompareTo(other.ElementCount);
            }
            return(result);
        }
Esempio n. 5
0
        public bool IsProperSubsetOf(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            ICollection <T> otherAsCollection = other as ICollection <T>;

            if (otherAsCollection != null)
            {
                if (_count == 0)
                {
                    return(otherAsCollection.Count > 0);
                }
                HashSet <T> otherAsSet = other as HashSet <T>;
                if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
                {
                    if (_count >= otherAsSet.Count)
                    {
                        return(false);
                    }
                    return(IsSubsetOfHashSetWithSameEC(otherAsSet));
                }
            }
            ElementCount result = CheckUniqueAndUnfoundElements(other, false);

            return(result.uniqueCount == _count && result.unfoundCount > 0);
        }
Esempio n. 6
0
        public bool IsProperSubsetOf(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            ICollection <T> is2 = other as ICollection <T>;

            if (is2 != null)
            {
                if (this.m_count == 0)
                {
                    return(is2.Count > 0);
                }
                HashSet <T> set = other as HashSet <T>;
                if ((set != null) && HashSet <T> .AreEqualityComparersEqual((HashSet <T>) this, set))
                {
                    if (this.m_count >= set.Count)
                    {
                        return(false);
                    }
                    return(this.IsSubsetOfHashSetWithSameEC(set));
                }
            }
            ElementCount <T> count = this.CheckUniqueAndUnfoundElements(other, false);

            return((count.uniqueCount == this.m_count) && (count.unfoundCount > 0));
        }
Esempio n. 7
0
        public bool SetEquals(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            HashSet <T> set = other as HashSet <T>;

            if ((set != null) && HashSet <T> .AreEqualityComparersEqual((HashSet <T>) this, set))
            {
                if (this.m_count != set.Count)
                {
                    return(false);
                }
                return(this.ContainsAllElements(set));
            }
            ICollection <T> is2 = other as ICollection <T>;

            if (((is2 != null) && (this.m_count == 0)) && (is2.Count > 0))
            {
                return(false);
            }
            ElementCount <T> count = this.CheckUniqueAndUnfoundElements(other, true);

            return((count.uniqueCount == this.m_count) && (count.unfoundCount == 0));
        }
Esempio n. 8
0
        public bool IsProperSupersetOf(IEnumerable <T> other)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other");
            }
            if (_count == 0)
            {
                return(false);
            }
            if (other is ICollection <T> otherAsCollection)
            {
                if (otherAsCollection.Count == 0)
                {
                    return(true);
                }
                if (other is HashSet <T> otherAsSet && AreEqualityComparersEqual(this, otherAsSet))
                {
                    if (otherAsSet.Count >= _count)
                    {
                        return(false);
                    }
                    return(ContainsAllElements(otherAsSet));
                }
            }
            ElementCount result = CheckUniqueAndUnfoundElements(other, true);

            return(result.uniqueCount < _count && result.unfoundCount == 0);
        }
Esempio n. 9
0
        public byte[] ToArray()
        {
            var woffset = 0;
            var ret     = new byte[Size];

            ret.CopyAndIncr(ElementCount.ToArray(), ref woffset);
            ret.CopyAndIncr(Elements, woffset);

            return(ret);
        }
Esempio n. 10
0
        public byte[] ToArray()
        {
            var ret = new byte[ElementCount.Size + ElementCount];

            var ec = ElementCount.ToArray();

            Buffer.BlockCopy(ec, 0, ret, 0, ec.Length);

            Buffer.BlockCopy(Elements, 0, ret, ec.Length, Elements.Length);

            return(ret);
        }
Esempio n. 11
0
        private static List <ElementCount <T> > GetElementCounts <T>(IEnumerable <T> collection, Func <T, T, bool> equality)
        {
            List <ElementCount <T> > elementCounts = new List <ElementCount <T> >();

            foreach (T element in collection)
            {
                ElementCount <T> count = elementCounts.Find(p => equality(p.Element, element));
                if (count == null)
                {
                    elementCounts.Add(new ElementCount <T>(element));
                }
                else
                {
                    count.Increment();
                }
            }
            return(elementCounts);
        }
Esempio n. 12
0
        private string GetAttributeString()
        {
            var attributes = new Dictionary <string, string>();

            attributes.Add("protocol", this.Protocol.ToString());
            attributes.Add("gateway", this.Gateway);
            attributes.Add("path", Path);
            attributes.Add("plc", PlcType.ToString().ToLower());
            attributes.Add("elem_size", ElementSize?.ToString());
            attributes.Add("elem_count", ElementCount?.ToString());
            attributes.Add("name", Name);
            attributes.Add("read_cache_ms", ReadCacheMillisecondDuration?.ToString());
            if (UseConnectedMessaging.HasValue)
            {
                attributes.Add("use_connected_msg", UseConnectedMessaging.Value ? "1" : "0");
            }

            string separator = "&";

            return(string.Join(separator, attributes.Where(attr => attr.Value != null).Select(attr => $"{attr.Key}={attr.Value}")));
        }
Esempio n. 13
0
 public bool IsSubsetOf(IEnumerable <T> other)
 {
     if (other == null)
     {
         throw new ArgumentNullException("other");
     }
     if (_count == 0)
     {
         return(true);
     }
     if (other is HashSet <T> otherAsSet && AreEqualityComparersEqual(this, otherAsSet))
     {
         if (_count > otherAsSet.Count)
         {
             return(false);
         }
         return(IsSubsetOfHashSetWithSameEC(otherAsSet));
     }
     else
     {
         ElementCount result = CheckUniqueAndUnfoundElements(other, false);
         return(result.uniqueCount == _count && result.unfoundCount >= 0);
     }
 }
Esempio n. 14
0
        public OverworldSpriteListRun(IDataModel model, IReadOnlyList <ArrayRunElementSegment> parent, string paletteHint, int runIndex, int start, SortedSpan <int> sources = null) : base(start, sources)
        {
            this.model  = model;
            this.parent = parent;
            PaletteHint = paletteHint;
            RunIndex    = runIndex;

            var nextStartBuilder = new List <int>();
            int parentLength     = parent.Sum(seg => seg.Length);

            if (parent != null && sources != null && sources.Count > 0)
            {
                for (int nextSource = sources[0] + parentLength; true; nextSource += parentLength)
                {
                    var nextDest = model.ReadPointer(nextSource);
                    if (nextDest < 0 || nextDest >= model.Count)
                    {
                        break;
                    }
                    nextStartBuilder.Add(nextDest);
                }
            }
            var nextStart = nextStartBuilder.ToArray();

            var segments = new List <ArrayRunElementSegment> {
                new ArrayRunPointerSegment("sprite", "`ucs4x1x2`"),
                new ArrayRunElementSegment("length", ElementContentType.Integer, 4),
            };

            ElementContent = segments;
            ElementCount   = 1;
            Length         = ElementLength;
            SpriteFormat   = new SpriteFormat(4, 1, 1, string.Empty);

            if (sources == null || sources.Count == 0)
            {
                return;
            }

            // initialize format from parent info
            var listOffset   = GetOffset <ArrayRunPointerSegment>(parent, pSeg => pSeg.InnerFormat.StartsWith("`osl"));
            var widthOffset  = GetOffset(parent, seg => seg.Name == "width");
            var heightOffset = GetOffset(parent, seg => seg.Name == "height");
            var keyOffset    = GetOffset(parent, seg => seg.Name == "paletteid");

            if (widthOffset == parentLength)
            {
                widthOffset = -1;
            }
            if (heightOffset == parentLength)
            {
                heightOffset = -1;
            }
            if (keyOffset == parentLength)
            {
                keyOffset = -1;
            }

            var elementStart = sources[0] - listOffset;
            var width        = widthOffset >= 0 ? Math.Max(1, model.ReadMultiByteValue(elementStart + widthOffset, 2)) : 0;
            var height       = heightOffset >= 0 ? Math.Max(1, model.ReadMultiByteValue(elementStart + heightOffset, 2)) : 0;
            // if there was no height/width found, assume that it's square and based on the first element length
            var  pixelCount       = model.ReadMultiByteValue(start + 4, 4) * 2; // number of pixels is twice the number of bytes for all OW sprites
            bool adjustDimensions = true;

            if (width == 0)
            {
                width = (int)Math.Sqrt(pixelCount); adjustDimensions = true;
            }
            if (height == 0)
            {
                height = width; adjustDimensions = true;
            }
            var tileWidth  = (int)Math.Max(1, Math.Ceiling(width / 8.0));
            var tileHeight = (int)Math.Max(1, Math.Ceiling(height / 8.0));

            while (adjustDimensions)
            {
                adjustDimensions = false;
                while (tileWidth * tileHeight * 64 > pixelCount)
                {
                    adjustDimensions = true;
                    tileHeight      -= 1;
                }
                if (tileHeight == 0)
                {
                    break;
                }
                while (tileWidth * tileHeight * 64 < pixelCount)
                {
                    if (tileWidth > 500)
                    {
                        break;
                    }
                    adjustDimensions = true;
                    tileWidth       += 1;
                }
            }

            var key  = model.ReadMultiByteValue(elementStart + keyOffset, 2);
            var hint = $"{HardcodeTablesModel.OverworldPalettes}:id={key:X4}";

            if (!string.IsNullOrEmpty(paletteHint))
            {
                hint = PaletteHint + $"={runIndex:X4}";
            }
            if (paletteHint != null && paletteHint.Contains("="))
            {
                hint = PaletteHint + $"{key:X4}";
            }

            var format = $"`ucs4x{tileWidth}x{tileHeight}|{hint}`";

            if (keyOffset == -1 && string.IsNullOrEmpty(paletteHint))
            {
                format = $"`ucs4x{tileWidth}x{tileHeight}`";
                hint   = string.Empty;
            }
            segments[0] = new ArrayRunPointerSegment("sprite", format);

            // calculate the element count
            var byteLength      = tileWidth * tileHeight * TileSize;
            var nextAnchorStart = model.GetNextAnchor(Start + 1).Start;

            ElementCount = 0;
            Length       = 0;
            while (Start + Length < nextAnchorStart)
            {
                if (model[start + Length + 3] != 0x08)
                {
                    break;
                }
                if (model.ReadMultiByteValue(start + Length + 4, 4) != byteLength)
                {
                    break;
                }
                var nextRun = model.GetNextRun(start + Length);
                if (Length > 0 && (start + Length).IsAny(nextStart))
                {
                    break;                                              // metric: if there's a pointer in the parent table that points here, then it's the next list, not this list.
                }
                ElementCount += 1;
                Length       += ElementLength;
                if (ElementCount == MaxOverworldSprites)
                {
                    break;                                  // overworld sprite lists can only have so many elements
                }
            }

            SpriteFormat = new SpriteFormat(4, tileWidth, tileHeight, hint);
            ElementNames = ElementCount.Range().Select(i => string.Empty).ToList();
        }
 public bool Equals(OpenGLVertexInputElement other)
 {
     return(SizeInBytes.Equals(other.SizeInBytes) && ElementCount.Equals(other.ElementCount) &&
            Type == other.Type && Offset.Equals(other.Offset) && Normalized.Equals(other.Normalized) &&
            InstanceStepRate.Equals(other.InstanceStepRate));
 }