Example #1
0
        void MakeMatch(int itemIndex, int dataIndex)
        {
            var item = AllTreeItems[itemIndex];
            var data = AllExternalDataItems[dataIndex];

            match[item] = data;
            for (int i = 0; i < matrix.GetLength(0); i++)
            {
                matrix[i, dataIndex] = 0;
            }
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                matrix[itemIndex, j] = 0;
            }
            UnmatchedExternalDataItems.Remove(data);
            MatchedExternalDataItems.Add(data);
            UnmatchedTreeItems.Remove(item);
            MatchedTreeItems.Add(item);
        }
Example #2
0
        public void Push(Item root)
        {
            AllExternalDataItems = initialDataItems.ToList();
            MatchedExternalDataItems.Clear();
            UnmatchedExternalDataItems = initialDataItems.ToList();
            AllTreeItems.AddRange(root.Subtree().OfType <TItem>());
            MatchedTreeItems.Clear();
            UnmatchedTreeItems = AllTreeItems.ToList();
            match  = new Dictionary <TItem, TData>();
            matrix = new double[AllTreeItems.Count, AllExternalDataItems.Count];

            for (int i = 0; i < AllTreeItems.Count; i++)
            {
                for (int j = 0; j < AllExternalDataItems.Count; j++)
                {
                    matrix[i, j] = distance(AllTreeItems[i], AllExternalDataItems[j]);
                }
            }

            for (int i = 0; i < AllTreeItems.Count; i++)
            {
                var storedData = AllTreeItems[i].Get <TData>();
                if (storedData != null)
                {
                    var foundData = UnmatchedExternalDataItems.Where(z => Equals(z, storedData)).FirstOrDefault();
                    if (foundData != null)
                    {
                        MakeMatch(i, AllExternalDataItems.IndexOf(foundData));
                    }
                }
            }



            while (true)
            {
                int    bestX = -1;
                int    bestY = -1;
                double best  = 0;
                for (int i = 0; i < AllTreeItems.Count; i++)
                {
                    for (int j = 0; j < AllExternalDataItems.Count; j++)
                    {
                        if (bestX < 0 || matrix[i, j] > best)
                        {
                            bestX = i;
                            bestY = j;
                            best  = matrix[i, j];
                        }
                    }
                }
                if (best > 0)
                {
                    MakeMatch(bestX, bestY);
                }
                else
                {
                    break;
                }
            }

            DataBinding <TItem> .Pull(root, z => match.ContainsKey(z)?match[z] : default(TData));
        }