Ejemplo n.º 1
0
        public MainWindow()
        {
            InitializeComponent();

            var book1 = new Book("J.R.R. Tolkien", "The Lord of the Rings");
            var book2 = new Book("Patrick Rothfuss", "Name of the Wind");
            // For the next item "targetBucket" ("Frank Herbert".GetHashCode() % 7) collides with the one for the book1.
            // We will store the book3 in the first available slot in "entries" array storing the old collided entry index in the new entry "next" property.
            // Then we replace buckets[targetBucket] value with the new entry index in the Entries array.
            var book3 = new Book("Frank Herbert", "Dune");
            //var book4 = new Book("Iain M. Banks", "Consider Phlebas");
            //var book5 = new Book("Isaac Asimov", "Foundation");
            //var book6 = new Book("Arthur Clarke", "2001: Space Odyssey");

            // For a five-item-sized dictionary .NET provides seven buckets.
            var dict = new Dictionary <string, Book>(5)
            {
                { book1.Author, book1 },
                { book2.Author, book2 },
                { book3.Author, book3 },
                //{ book4.Author, book4},
                //{ book5.Author, book5},
                //{ book6.Author, book6},
            };

            //dict.Remove(book2.Author);
            //dict.Remove(book5.Author);

            var extractor = new DictionaryMetadataExtractor <string, Book>();

            metadata = extractor.ExtractMetadata(dict);

            this.DrawVisualization(metadata);
        }
Ejemplo n.º 2
0
        public IDictionaryMetadata GetDictionaryMetadataById(int dictionaryId)
        {
            IDictionaryMetadata dictionaryMetadata = new DictionaryMetadata();

            Dictionary dictionary = Dictionaries.Find(x => x.Id == dictionaryId);

            dictionaryMetadata.Id         = dictionary.Id;
            dictionaryMetadata.Name       = dictionary.Name;
            dictionaryMetadata.Attributes = dictionary.Attributes;

            return(dictionaryMetadata);
        }
Ejemplo n.º 3
0
 public virtual void Inform(IResourceLoader loader)
 {
     if (resourceName == null)
     {
         // Get the dictionary lazily, does not hold up memory.
         this.dictionary = new PolishStemmer().Dictionary;
     }
     else
     {
         using Stream dict = loader.OpenResource(resourceName);
         using Stream meta = loader.OpenResource(DictionaryMetadata.GetExpectedMetadataFileName(resourceName));
         this.dictionary   = Dictionary.Read(dict, meta);
     }
 }
Ejemplo n.º 4
0
        private void DrawVisualization <TKey, TValue>(DictionaryMetadata <TKey, TValue> metadata)
        {
            this.InitGridRowDefinitions(metadata.Buckets.Length);

            for (int i = 0; i < metadata.Buckets.Length; i++)
            {
                AddIndexTextBlock <TKey, TValue>(i);

                AddBucketElement(metadata, i);

                AddEntryElement(metadata, i);
            }

            this.DrawArrows(metadata);
        }
Ejemplo n.º 5
0
        public MainWindow()
        {
            InitializeComponent();

            var key = new GoodButMutableTestRefKey(3);

            // For a dictionaties with the size from 1 to 3 items, the number of buckets equals 3.
            var dictWithMutableKey = new Dictionary <GoodButMutableTestRefKey, string>
            {
                { key, "Hello!" }
            };

            // Mutate the key object. This affects both the GetHashCode() return value and Equals(obj) equality test.
            key.Key = 1;

            try
            {
                var obj1 = dictWithMutableKey[key];
            }
            catch (KeyNotFoundException)
            {
                this.LookupByTheWrongBucket.Content = "(GetHashCode() % buckets.Length) returns \"targetBucket\" 1 instead of 0";
            }

            try
            {
                var obj2 = dictWithMutableKey[new GoodButMutableTestRefKey(3)];
            }
            catch (KeyNotFoundException)
            {
                this.EqualityComparisonForKeysFailed.Content = "Bucket is correct now, but the referenced Entry has a key that is different from \"3\"";
            }

            try
            {
                var obj3 = dictWithMutableKey[new GoodButMutableTestRefKey(1)];
            }
            catch (KeyNotFoundException)
            {
                this.LookupByTheWrongBucket2.Content = "(GetHashCode() % buckets.Length) returns \"targetBucket\" 1 instead of 0 again";
            }

            var extractor = new DictionaryMetadataExtractor <GoodButMutableTestRefKey, string>();

            metadata = extractor.ExtractMetadata(dictWithMutableKey);

            this.DrawVisualization(metadata);
        }
Ejemplo n.º 6
0
        private void AddBucketElement <TKey, TValue>(DictionaryMetadata <TKey, TValue> metadata, int i)
        {
            var bucket = new Border
            {
                Child = new TextBlock
                {
                    Text = metadata.Buckets[i].ToString()
                },
                BorderBrush     = Brushes.Black,
                BorderThickness = new Thickness(1),
                Width           = 50,
                Height          = 50
            };

            Grid.SetColumn(bucket, 2);
            Grid.SetRow(bucket, i + 1);
            this.EntriesGrid.Children.Add(bucket);
        }
Ejemplo n.º 7
0
 private static Dictionary GetDictionary()
 {
     try
     {
         Type type = typeof(UkrainianMorfologikAnalyzer);
         // LUCENENET NOTE: In Lucene, this was downloaded from Maven as a dependency
         // (see https://search.maven.org/search?q=a:morfologik-ukrainian-search). However, we are embedding the file in .NET.
         // Since it doesn't appear to be updated frequently, this should be okay.
         string dictFile = "ukrainian.dict";
         using var dictStream     = type.FindAndGetManifestResourceStream(dictFile);
         using var metadataStream = type.FindAndGetManifestResourceStream(DictionaryMetadata.GetExpectedMetadataFileName(dictFile));
         return(Dictionary.Read(dictStream, metadataStream));
     }
     catch (IOException e)
     {
         throw new Exception(e.ToString(), e);
     }
 }
Ejemplo n.º 8
0
        private void DrawArrows <TKey, TValue>(DictionaryMetadata <TKey, TValue> metadata)
        {
            ArrowsCanvas.Children.Clear();

            for (int i = 0; i < metadata.Buckets.Length; i++)
            {
                if (metadata.Buckets[i] != -1)
                {
                    var   bucket    = GetElement(i, 2);
                    Point bucketPos = bucket.TransformToAncestor(this.RootGrid).Transform(new Point(0, 0));

                    var   entry    = GetElement(metadata.Buckets[i], 4);
                    Point entryPos = entry.TransformToAncestor(this.RootGrid).Transform(new Point(0, 0));

                    var line = new Line
                    {
                        X1              = bucketPos.X + bucket.ActualWidth,
                        Y1              = bucketPos.Y + bucket.ActualHeight / 2,
                        X2              = entryPos.X,
                        Y2              = entryPos.Y + entry.ActualHeight / 2,
                        Stroke          = Brushes.Blue,
                        StrokeThickness = 4
                    };

                    ArrowsCanvas.Children.Add(line);

                    var head = new Ellipse
                    {
                        Fill   = Brushes.Blue,
                        Width  = 14,
                        Height = 14
                    };

                    Canvas.SetTop(head, entryPos.Y + entry.ActualHeight / 2 - 7);
                    Canvas.SetLeft(head, entryPos.X - 7);

                    ArrowsCanvas.Children.Add(head);
                }

                if (metadata.Entries[i].Next != -1 && (metadata.Entries[i].Key != null || metadata.Entries[i].Next > 0))
                {
                    var   entry1    = GetElement(i, 4);
                    Point entry1Pos = entry1.TransformToAncestor(this.RootGrid).Transform(new Point(0, 0));

                    var   entry2    = GetElement(metadata.Entries[i].Next, 4);
                    Point entry2Pos = entry2.TransformToAncestor(this.RootGrid).Transform(new Point(0, 0));

                    var X1 = entry1Pos.X + entry1.ActualWidth;
                    var Y1 = entry1Pos.Y + entry1.ActualHeight / 2;
                    var X2 = entry2Pos.X + entry2.ActualWidth;
                    var Y2 = entry2Pos.Y + entry2.ActualHeight / 2;

                    var path = new Path
                    {
                        Data = new PathGeometry
                        {
                            Figures = new PathFigureCollection
                            {
                                new PathFigure
                                {
                                    Segments = new PathSegmentCollection
                                    {
                                        new PolyBezierSegment
                                        {
                                            Points = new PointCollection
                                            {
                                                new Point(60, (Y2 - Y1) / 3),
                                                new Point(60, (Y2 - Y1) * 2 / 3),
                                                new Point(0, Y2 - Y1),
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        StrokeThickness = 4,
                        Stroke          = Brushes.Green
                    };

                    Canvas.SetLeft(path, X1);
                    Canvas.SetTop(path, Y1);

                    ArrowsCanvas.Children.Add(path);

                    var head = new Ellipse
                    {
                        Fill   = Brushes.Green,
                        Width  = 14,
                        Height = 14
                    };

                    Canvas.SetLeft(head, X2 - 7);
                    Canvas.SetTop(head, Y2 - 7);

                    ArrowsCanvas.Children.Add(head);
                }
            }
        }
Ejemplo n.º 9
0
        private void AddEntryElement <TKey, TValue>(DictionaryMetadata <TKey, TValue> metadata, int i)
        {
            var entry = new Border
            {
                BorderBrush     = Brushes.Black,
                BorderThickness = new Thickness(1),
                Height          = 50
            };

            if (metadata.Entries[i].Value != null)
            {
                var entryGrid = new Grid();
                entryGrid.ColumnDefinitions.Add(new ColumnDefinition());
                entryGrid.ColumnDefinitions.Add(new ColumnDefinition());
                entryGrid.RowDefinitions.Add(new RowDefinition());
                entryGrid.RowDefinitions.Add(new RowDefinition());
                entryGrid.RowDefinitions.Add(new RowDefinition());

                var entryHashCode = new TextBlock
                {
                    Text     = "HashCode: " + metadata.Entries[i].HashCode.ToString(),
                    FontSize = 12,
                    Margin   = new Thickness(12, 0, 12, 0)
                };

                var entryModulus = new TextBlock
                {
                    Text     = "HashCode mod: " + metadata.Entries[i].HashCode % metadata.Buckets.Length,
                    FontSize = 12,
                    Margin   = new Thickness(12, 0, 12, 0)
                };

                var entryKey = new TextBlock
                {
                    Text     = "Key: " + metadata.Entries[i].Key.ToString(),
                    FontSize = 12,
                    Margin   = new Thickness(12, 0, 12, 0)
                };

                var entryNext = new TextBlock
                {
                    Text     = "Next: " + metadata.Entries[i].Next.ToString(),
                    FontSize = 12,
                    Margin   = new Thickness(0, 0, 12, 0)
                };

                var entryValue = new TextBlock
                {
                    Text     = "Value: " + metadata.Entries[i].Value.ToString(),
                    FontSize = 12,
                    Margin   = new Thickness(0, 0, 12, 0)
                };

                Grid.SetColumn(entryKey, 0);
                Grid.SetRow(entryKey, 0);
                Grid.SetColumn(entryHashCode, 0);
                Grid.SetRow(entryHashCode, 1);
                Grid.SetColumn(entryModulus, 0);
                Grid.SetRow(entryModulus, 2);
                Grid.SetColumn(entryNext, 1);
                Grid.SetRow(entryNext, 0);
                Grid.SetColumn(entryValue, 1);
                Grid.SetRow(entryValue, 1);

                entryGrid.Children.Add(entryKey);
                entryGrid.Children.Add(entryHashCode);
                entryGrid.Children.Add(entryModulus);
                entryGrid.Children.Add(entryNext);
                entryGrid.Children.Add(entryValue);

                entry.Child = entryGrid;
            }

            Grid.SetColumn(entry, 4);
            Grid.SetRow(entry, i + 1);
            this.EntriesGrid.Children.Add(entry);
        }
Ejemplo n.º 10
0
        private static Dictionary LoadDictionary()
        {
            Type type = typeof(PolishStemmer);

            lock (type)
            {
                string dict = ResourcePath + DictionaryName;
                using (var dictStream = type.Assembly.GetManifestResourceStream(dict))
                    using (var metadataStream = type.Assembly.GetManifestResourceStream(DictionaryMetadata.GetExpectedMetadataFileName(dict)))
                    {
                        if (dictStream == null)
                        {
                            throw new IOException("Polish dictionary resource not found.");
                        }

                        return(Dictionary.Read(dictStream, metadataStream));
                    }
            }
        }