public bool ApplyBehaviour(List <ASTNode> nodes, int opIndex, OrderMethod caller)
 {
     if (nodes[opIndex] is IndexerNode ix)
     {
         if (ix.Nodes.Count == 0 && nodes[opIndex - 1].Is <IdentifierNode>().Or <ITypeIdentifier>(out bool isType))
         {
             ITypeIdentifier         arrayedType = isType ? nodes[opIndex - 1] as ITypeIdentifier : new TypeIdentifierNode(nodes[opIndex - 1] as IdentifierNode);
             TypeArrayIdentifierNode lookup      = new TypeArrayIdentifierNode(arrayedType, nodes[opIndex - 1].Pos);
             nodes[opIndex - 1] = lookup;
             nodes.RemoveAt(opIndex);
         }
         else
         {
             LookupNode lookup = new LookupNode(nodes[opIndex - 1] as IExpr, nodes[opIndex] as IndexerNode, nodes[opIndex - 1].Pos);
             caller.Invoke(lookup.Index.Nodes); // Invoke on sub-elements
             nodes[opIndex - 1] = lookup;
             nodes.RemoveAt(opIndex);
         }
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#2
0
 public PaletteLookup(Pixel[] palette)
 {
     Palette = new LookupNode[palette.Length];
     for(int paletteIndex = 0; paletteIndex < palette.Length; paletteIndex++)
     {
         Palette[paletteIndex] = new LookupNode{Pixel = palette[paletteIndex], PaletteIndex = (byte)paletteIndex};
     }
     BuildLookup(palette);
 }
示例#3
0
 public PaletteLookup(Pixel[] palette)
 {
     Palette = new LookupNode[palette.Length];
     for (int paletteIndex = 0; paletteIndex < palette.Length; paletteIndex++)
     {
         Palette[paletteIndex] = new LookupNode {
             Pixel = palette[paletteIndex], PaletteIndex = (byte)paletteIndex
         };
     }
     BuildLookup(palette);
 }
示例#4
0
        private object Lookup(LookupNode n)
        {
            var    name = n.Variable.Name;
            object result;

            if (variables.TryGetValue(name, out result))
            {
                return(result);
            }
            throw (new Exception($"Undefined variable {name}"));
        }
示例#5
0
        public ReverseLookup(IEnumerable <KeyValuePair <short, ContextString> > charLookup)
        {
            var unique = charLookup.Where(cs => cs.Value.String.Length > 0)
                         .GroupBy(cs => cs.Value)
                         .Select(g => g.First());

            foreach (var kv in unique)
            {
                short         value = kv.Key;
                ContextString cs    = kv.Value;
                char          first = cs.String[0];

                LookupNode root;

                if (tree.ContainsKey(first))
                {
                    root = tree[first];
                }
                else
                {
                    root = new LookupNode();
                    tree.Add(cs.String[0], root);
                }

                root.Depth = 1;

                int currentIndex = 1;

                while (currentIndex < cs.String.Length)
                {
                    char currentChar = cs.String[currentIndex];

                    LookupNode node;

                    if (root.Children.ContainsKey(currentChar))
                    {
                        node = root.Children[currentChar];
                    }
                    else
                    {
                        node = new LookupNode();
                        root.Children.Add(currentChar, node);
                    }

                    node.Parent = root;
                    root        = node;
                    currentIndex++;
                    root.Depth = currentIndex;
                }

                root.Values.Add(cs.Context, value);
            }
        }
        public static IEnumerable <(string, List <LookupNode>)> CreateLookupNodeProtobufs()
        {
            var folder   = RouteCleanerSettings.GetInstance().TemporaryNodeWithContainingWayOutLocation;
            var allFiles = Directory.GetFiles(folder);

            foreach (var file in allFiles)
            {
                Console.WriteLine($"Working on {file}");
                var    outputs = new Dictionary <string, List <LookupNode> >();
                string line;
                var    sr      = new StreamReader(file);
                long   lineNum = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    Node node;
                    try
                    {
                        node = JsonConvert.DeserializeObject <Node>(line);
                        lineNum++;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Exception {ex} on line {lineNum++}");
                        Console.WriteLine($"Line: {line}");
                        continue;
                    }

                    var location = new OpenLocationCode(node.Latitude, node.Longitude, codeLength: 6);
                    if (!outputs.ContainsKey(location.Code))
                    {
                        outputs.Add(location.Code, new List <LookupNode>());
                    }

                    var lNode = new LookupNode
                    {
                        Id        = node.Id,
                        Latitude  = node.Latitude,
                        Longitude = node.Longitude
                    };
                    lNode.Relations.AddRange(node.Relations);
                    lNode.TargetableWays.AddRange(node.ContainingWays);
                    outputs[location.Code].Add(lNode);
                }

                foreach (var kvp in outputs)
                {
                    kvp.Value.Sort(SortNodesByLatLong);
                    yield return(kvp.Key, kvp.Value);
                }
            }
        }
示例#7
0
        private object LookupNode(LookupNode lu)
        {
            var    name = lu.v.Name;
            object output;

            if (variables.TryGetValue(name, out output))
            {
                return(output);
            }
            else
            {
                throw(new Exception("Undefined Variable"));
            }
        }
示例#8
0
        public byte GetPaletteIndex(Pixel pixel)
        {
            int pixelKey = pixel.Argb & mMask;

            LookupNode[] bucket;
            if (!mLookup.TryGetValue(pixelKey, out bucket))
            {
                bucket = Palette;
            }

            if (bucket.Length == 1)
            {
                return(bucket[0].PaletteIndex);
            }

            int  bestDistance = int.MaxValue;
            byte bestMatch    = 0;

            foreach (var lookup in bucket)
            {
                var lookupPixel = lookup.Pixel;

                var deltaAlpha = pixel.Alpha - lookupPixel.Alpha;
                int distance   = deltaAlpha * deltaAlpha;

                var deltaRed = pixel.Red - lookupPixel.Red;
                distance += deltaRed * deltaRed;

                var deltaGreen = pixel.Green - lookupPixel.Green;
                distance += deltaGreen * deltaGreen;

                var deltaBlue = pixel.Blue - lookupPixel.Blue;
                distance += deltaBlue * deltaBlue;

                if (distance >= bestDistance)
                {
                    continue;
                }

                bestDistance = distance;
                bestMatch    = lookup.PaletteIndex;
            }

            if ((bucket == Palette) && (pixelKey != 0))
            {
                mLookup[pixelKey] = new LookupNode[] { bucket[bestMatch] };
            }

            return(bestMatch);
        }
示例#9
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PaletteLookup"/> class.
        /// </summary>
        /// <param name="palette">
        /// The palette.
        /// </param>
        public PaletteLookup(Color32[] palette)
        {
            Palette = new LookupNode[palette.Length];
            for (var paletteIndex = 0; paletteIndex < palette.Length; paletteIndex++)
            {
                Palette[paletteIndex] = new LookupNode
                {
                    Color32      = palette[paletteIndex],
                    PaletteIndex = (byte)paletteIndex
                };
            }

            BuildLookup(palette);
        }
示例#10
0
        public byte GetPaletteIndex(Pixel pixel)
        {
            int pixelKey = pixel.Argb & mMask;
            LookupNode[] bucket;
            if (!mLookup.TryGetValue(pixelKey, out bucket))
            {
                bucket = Palette;
            }

            if (bucket.Length == 1)
            {
                return bucket[0].PaletteIndex;
            }

            int bestDistance = int.MaxValue;
            byte bestMatch = 0;
            foreach(var lookup in bucket)
            {
                var lookupPixel = lookup.Pixel;

                var deltaAlpha = pixel.Alpha - lookupPixel.Alpha;
                int distance = deltaAlpha * deltaAlpha;

                var deltaRed = pixel.Red - lookupPixel.Red;
                distance += deltaRed * deltaRed;

                var deltaGreen = pixel.Green - lookupPixel.Green;
                distance += deltaGreen * deltaGreen;

                var deltaBlue = pixel.Blue - lookupPixel.Blue;
                distance += deltaBlue * deltaBlue;

                if (distance >= bestDistance)
                    continue;

                bestDistance = distance;
                bestMatch = lookup.PaletteIndex;
            }

            if ((bucket == Palette) && (pixelKey != 0))
            {
                mLookup[pixelKey] = new LookupNode[] { bucket[bestMatch] };
            }

            return bestMatch;
        }
示例#11
0
 private static int SortNodesByLatLong(LookupNode l, LookupNode r)
 {
     if (l.Latitude < r.Latitude)
     {
         return(-1);
     }
     if (l.Latitude > r.Latitude)
     {
         return(1);
     }
     if (l.Longitude < r.Longitude)
     {
         return(-1);
     }
     if (l.Longitude > r.Longitude)
     {
         return(1);
     }
     return(0);
 }
示例#12
0
        public short?Find(string str, int startingIndex, Context context, out int matchedLength)
        {
            matchedLength = 0;

            if (startingIndex >= str.Length)
            {
                return(null);
            }

            if (!tree.ContainsKey(str[startingIndex]))
            {
                return(null);
            }

            LookupNode node = tree[str[startingIndex++]];
            LookupNode bestMatch;

            if (node.Values.Count > 0 &&
                (node.Values.ContainsKey(context) || node.Values.ContainsKey(Context.None)))
            {
                bestMatch = node;
            }
            else
            {
                bestMatch = null;
            }

            matchedLength++;

            while (startingIndex < str.Length &&
                   node.Children.ContainsKey(str[startingIndex]))
            {
                node = node.Children[str[startingIndex++]];

                if (node.Values.Count > 0 &&
                    (node.Values.ContainsKey(context) || node.Values.ContainsKey(Context.None)))
                {
                    bestMatch = node;
                }
            }

            if (bestMatch != null)
            {
                if (bestMatch.Values.Count > 0)
                {
                    if (bestMatch.Values.ContainsKey(context))
                    {
                        matchedLength = bestMatch.Depth;
                        return(bestMatch.Values[context]);
                    }
                    else if (bestMatch.Values.ContainsKey(Context.None))
                    {
                        matchedLength = bestMatch.Depth;
                        return(bestMatch.Values[Context.None]);
                    }
                }
            }

            matchedLength = 0;
            return(null);
        }