Exemplo n.º 1
0
        void FixErrors(Person type)
        {
            /*
             * Fix errors by:
             *  1. detect if a person appear in two triplets
             *  2. in the second triplet, change its index to a one not used
             */

            SortedSet <int>       indexes = new SortedSet <int>();
            Dictionary <int, int> errors  = new Dictionary <int, int>();

            for (int i = 0; i < m_capacity; i++)
            {
                triple_t triple = m_triples[i];
                if (indexes.Contains(triple.Get(type)))
                {
                    Debug.Assert(!errors.ContainsKey(triple.Get(type)));
                    errors[triple.Get(type)] = i;
                }
                else
                {
                    indexes.Add(triple.Get(type));
                }
            }

            foreach (KeyValuePair <int, int> pair in errors)
            {
                // find first unused index
                int unused = 0;
                while (indexes.Contains(unused))
                {
                    unused++;
                }
                indexes.Add(unused);
                Debug.Assert(unused < m_capacity);

                triple_t triple = m_triples[pair.Value];
                Debug.Assert(triple.Get(type) == pair.Key);
                triple.Set(type, unused);
            }
        }