Exemple #1
0
 private static void GetMatches(string name, HashSet<PhonebookEntry> entries, MultiDictionary<string, PhonebookEntry> dict)
 {
     if (dict.ContainsKey(name))
     {
         foreach (var entry in dict[name])
         {
             if (!entries.Contains(entry))
             {
                 entries.Add(entry);
             }
         }
     }
 }
Exemple #2
0
 private static void GetDistinctRecords(string name, HashSet<Record> foundRecords, MultiDictionary<string, Record> data)
 {
     //add to the foundRecords only distinct records to avoid 
     //adding one person with two same names twice
     if (data.ContainsKey(name))
     {
         foreach (var record in data[name])
         {
             if (!foundRecords.Contains(record))
             {
                 foundRecords.Add(record);
             }
         }
     }
 }
        private static MultiDictionary<string, MultiDictionary<string, string>> FillPhoneBook()
        {
            const string PhonesPath = "phones.txt";

            StreamReader phonesReader = new StreamReader(PhonesPath);

            var phoneBook = new MultiDictionary<string, MultiDictionary<string, string>>(true);

            using (phonesReader)
            {
                string line = phonesReader.ReadLine();

                while (line != null)
                {
                    var splittedEntry = line.Split('|');

                    string name = splittedEntry[0].Trim();
                    string town = splittedEntry[1].Trim();
                    string phoneNumber = splittedEntry[2].Trim();

                    if (phoneBook.ContainsKey(name))
                    {
                        var nameInPhoneBook = phoneBook[name].First();

                        if (nameInPhoneBook.ContainsKey(town))
                        {
                            nameInPhoneBook[town].Add(phoneNumber);
                        }
                        else
                        {
                            nameInPhoneBook.Add(town, phoneNumber);
                        }
                    }
                    else
                    {
                        phoneBook.Add(name, new MultiDictionary<string, string>(true));
                        var nameInPhoneBook = phoneBook[name].First();
                        nameInPhoneBook.Add(town, phoneNumber);
                    }

                    line = phonesReader.ReadLine();
                }

                return phoneBook;
            }
        }
Exemple #4
0
        private void ReconnectTerminals(Graph workingSolution, Graph problemInstance, Graph problemInstanceDistance)
        {
            Stopwatch reconnectStopwatch = new Stopwatch();
            reconnectStopwatch.Start();

            var components = workingSolution.CreateComponentTable();
            var componentsToConnect =
                components.Where(x => problemInstance.Terminals.Contains(x.Key))
                    .Select(x => x.Value)
                    .Distinct()
                    .ToList();

            if (componentsToConnect.Count <= 1) return;

            MultiDictionary<int, Tuple<Vertex, Vertex>> componentConnectingPathDictionary = new MultiDictionary<int, Tuple<Vertex, Vertex>>();
            List<Vertex> componentGraphVertices = new List<Vertex>();
            foreach (var i in componentsToConnect)
                componentGraphVertices.Add(new Vertex(i));
            Graph componentGraph = new Graph(componentGraphVertices);

            for (int i = 0; i < componentsToConnect.Count; i++)
            {
                int fromComponent = componentsToConnect[i];
                for (int j = i + 1; j < componentsToConnect.Count; j++)
                {
                    int toComponent = componentsToConnect[j];
                    int minDistance = int.MaxValue;

                    foreach (var fromVertex in components.Where(x => x.Value == fromComponent)
                                                         .Select(x => x.Key))
                    {
                        var distanceEdges = problemInstanceDistance.GetEdgesForVertex(fromVertex);
                        foreach (var toVertexEdge in distanceEdges)
                        {
                            var toVertex = toVertexEdge.Other(fromVertex);
                            if (components[toVertex] != toComponent)
                                continue;

                            int distance = toVertexEdge.Cost;
                            if (!componentConnectingPathDictionary.ContainsKey(fromComponent, toComponent))
                            {
                                componentConnectingPathDictionary.Add(fromComponent, toComponent, new Tuple<Vertex, Vertex>(fromVertex, toVertex));
                                componentGraph.AddEdge(new Edge(componentGraphVertices[i], componentGraphVertices[j], minDistance));
                            }

                            if (distance < minDistance)
                            {
                                minDistance = distance;
                                componentConnectingPathDictionary[fromComponent, toComponent] = new Tuple<Vertex, Vertex>(fromVertex, toVertex);
                                componentGraph.GetEdgesForVertex(componentGraphVertices[i])
                                    .Single(x => x.Other(componentGraphVertices[i]) == componentGraphVertices[j])
                                    .Cost = minDistance;
                            }
                        }
                    }
                }
            }
            componentGraph = Algorithms.Kruskal(componentGraph);
            foreach (var edge in componentGraph.Edges)
            {
                var v1 = edge.Either();
                var v2 = edge.Other(v1);
                var vertices = componentConnectingPathDictionary[v1.VertexName, v2.VertexName];
                var path = Algorithms.DijkstraPath(vertices.Item1, vertices.Item2, problemInstance);
                foreach (var pathEdge in path.Edges)
                    workingSolution.AddEdge(pathEdge);
            }

            reconnectStopwatch.Stop();
        }
        private static void AddTypeParameters(GenericNameSyntax genericNameSyntax, MultiDictionary<string, TypeParameterSymbol> map)
        {
            // NOTE: Dev11 does not warn about duplication, it just matches parameter types to the
            // *last* type parameter with the same name.  That's why we're iterating backwards and
            // skipping subsequent symbols with the same name.  This can result in some surprising
            // behavior.  For example, both 'T's in "A<T>.B<T>" bind to the second implicitly
            // declared type parameter.
            SeparatedSyntaxList<TypeSyntax> typeArguments = genericNameSyntax.TypeArgumentList.Arguments;
            for (int i = typeArguments.Count - 1; i >= 0; i--)
            {
                // Other types (non-identifiers) are allowed in error scenarios, but they do not introduce new 
                // cref type parameters.
                if (typeArguments[i].Kind() == SyntaxKind.IdentifierName)
                {
                    IdentifierNameSyntax typeParameterSyntax = (IdentifierNameSyntax)typeArguments[i];
                    Debug.Assert(typeParameterSyntax != null, "Syntactic requirement of crefs");

                    string name = typeParameterSyntax.Identifier.ValueText;
                    if (SyntaxFacts.IsValidIdentifier(name) && !map.ContainsKey(name))
                    {
                        TypeParameterSymbol typeParameterSymbol = new CrefTypeParameterSymbol(name, i, typeParameterSyntax);
                        map.Add(name, typeParameterSymbol);
                    }
                }
            }
        }
        public static void Main(string[] args)
        {
            StreamReader phones = new StreamReader("..\\..\\phones.txt");

            var phonebook = new MultiDictionary<string, MultiDictionary<string, string>>(false);
            using (phones)
            {
                while (!phones.EndOfStream)
                {
                    string entry = phones.ReadLine();
                    string[] entryInfo = entry.Split('|');
                    string name = entryInfo[0].Trim();
                    string location = entryInfo[1].Trim();
                    string phoneNumber = entryInfo[2].Trim();

                    if (phonebook.ContainsKey(name))
                    {
                        var nameInPhoneBook = phonebook[name].First();
                        if (nameInPhoneBook.ContainsKey(location))
                        {
                            nameInPhoneBook[location].Add(phoneNumber);
                        }
                        else
                        {
                            nameInPhoneBook.Add(location, phoneNumber);
                        }
                    }
                    else
                    {
                        phonebook.Add(name, new MultiDictionary<string, string>(true));
                        var nameInPhoneBook = phonebook[name].First();
                        nameInPhoneBook.Add(location, phoneNumber);
                    }
                }
            }

            var commands = new StreamReader("..\\..\\commands.txt");
            using (commands)
            {
                while (!commands.EndOfStream)
                {
                    string commandString = commands.ReadLine();
                    string[] parameters = commandString
                        .Substring(commandString.IndexOf('(') + 1, commandString.IndexOf(')') - commandString.IndexOf('(') - 1)
                        .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string name = parameters[0].Trim();
                    string location = string.Empty;

                    if (parameters.Length == 2)
                    {
                        location = parameters[1].Trim();
                    }

                    Console.WriteLine(commandString);

                    if (location == string.Empty)
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);
                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phonesCollection in locationPhoneCollection)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}",
                                            nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);

                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                var nameAndLocation = locationPhoneCollection.Where(x => x.Key == location);

                                foreach (var phonesCollection in nameAndLocation)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}", nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #7
0
 /// <summary>
 /// Determines whether a given Triple is in the Triple Collection.
 /// </summary>
 /// <param name="t">The Triple to test.</param>
 /// <returns>True if the Triple already exists in the Triple Collection.</returns>
 public override bool Contains(Triple t)
 {
     return(_triples.ContainsKey(t));
 }
 public bool ContainsKey(T key)
 {
     return(Enabled && _dictionary.ContainsKey(key));
 }
Exemple #9
0
        public GameObject GetSpecies(int speciesIndex, bool useExisting)
        {
            Vector3    newPos      = Vector3.zero;
            GameObject newCreature = null;

            SpawnParameters sp = prefabs[speciesIndex].GetComponent <SpawnParameters>();

            if (useExisting && aliveMap.ContainsKey(prefabs[speciesIndex]))
            {
                GameObject creature = aliveMap[prefabs[speciesIndex]];
                if (!creature.GetComponent <SpawnParameters>().isSuspending)
                {
                    return(prefabs[speciesIndex]);
                }
            }
            if (suspended.ContainsKey(prefabs[speciesIndex]))
            {
                newCreature = suspended.Get(prefabs[speciesIndex]);
                if (FindPlace(newCreature, out newPos))
                {
                    suspended.Remove(prefabs[speciesIndex], newCreature);
                    Teleport(newCreature, newPos);
                    newCreature.SetActive(true);
                    if (newCreature.GetComponent <LifeColours>())
                    {
                        newCreature.GetComponent <LifeColours>().FadeIn();
                    }
                    if (newCreature.GetComponentInChildren <CreatureController>())
                    {
                        newCreature.GetComponentInChildren <CreatureController>().Restart();
                    }
                    // Change the school size every time we teleport a school
                    SchoolGenerator sg = newCreature.GetComponentInChildren <SchoolGenerator>();
                    if (sg != null)
                    {
                        sg.transform.position  = newPos;
                        sg.targetCreatureCount = Random.Range(sg.minBoidCount, sg.maxBoidCount);
                    }
                    alive.Add(newCreature);
                    aliveMap[prefabs[speciesIndex]] = newCreature;

                    /*GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                     * cube.transform.position = newPos;
                     * cube.transform.localScale = Vector3.one * 5;
                     */
                }
            }
            else
            {
                //Debug.Log("Instiantiating a new: " + prefabs[nextCreature]);
                if (FindPlace(prefabs[speciesIndex], out newPos))
                {
                    newCreature = GameObject.Instantiate <GameObject>(prefabs[speciesIndex], newPos
                                                                      , prefabs[speciesIndex].transform.rotation * Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up)
                                                                      );

                    newCreature.GetComponent <SpawnParameters>().Species = prefabs[speciesIndex];
                    if (school != null)
                    {
                        Boid b = newCreature.GetComponent <Boid>();
                        b.school = school;
                        school.boids.Add(b);
                    }

                    if (newCreature.GetComponentInChildren <CreatureController>())
                    {
                        newCreature.GetComponentInChildren <CreatureController>().mother = this;
                    }

                    newCreature.transform.parent   = this.transform;
                    newCreature.transform.position = newPos;
                    newCreature.SetActive(true);
                    alive.Add(newCreature);
                    aliveMap[prefabs[speciesIndex]] = newCreature;
                }
                else
                {
                    Debug.Log("Couldnt find a place for the new creature");
                }
            }
            return(prefabs[speciesIndex]);
        }
Exemple #10
0
        public static void Main(string[] args)
        {
            StreamReader phones = new StreamReader("..\\..\\phones.txt");

            var phonebook = new MultiDictionary <string, MultiDictionary <string, string> >(false);

            using (phones)
            {
                while (!phones.EndOfStream)
                {
                    string   entry       = phones.ReadLine();
                    string[] entryInfo   = entry.Split('|');
                    string   name        = entryInfo[0].Trim();
                    string   location    = entryInfo[1].Trim();
                    string   phoneNumber = entryInfo[2].Trim();

                    if (phonebook.ContainsKey(name))
                    {
                        var nameInPhoneBook = phonebook[name].First();
                        if (nameInPhoneBook.ContainsKey(location))
                        {
                            nameInPhoneBook[location].Add(phoneNumber);
                        }
                        else
                        {
                            nameInPhoneBook.Add(location, phoneNumber);
                        }
                    }
                    else
                    {
                        phonebook.Add(name, new MultiDictionary <string, string>(true));
                        var nameInPhoneBook = phonebook[name].First();
                        nameInPhoneBook.Add(location, phoneNumber);
                    }
                }
            }

            var commands = new StreamReader("..\\..\\commands.txt");

            using (commands)
            {
                while (!commands.EndOfStream)
                {
                    string   commandString = commands.ReadLine();
                    string[] parameters    = commandString
                                             .Substring(commandString.IndexOf('(') + 1, commandString.IndexOf(')') - commandString.IndexOf('(') - 1)
                                             .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string name     = parameters[0].Trim();
                    string location = string.Empty;

                    if (parameters.Length == 2)
                    {
                        location = parameters[1].Trim();
                    }

                    Console.WriteLine(commandString);

                    if (location == string.Empty)
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);
                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                foreach (var phonesCollection in locationPhoneCollection)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}",
                                                          nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        var resultsByName = phonebook.Where(x => x.Key == name);

                        foreach (var nameEntries in resultsByName)
                        {
                            foreach (var locationPhoneCollection in nameEntries.Value)
                            {
                                var nameAndLocation = locationPhoneCollection.Where(x => x.Key == location);

                                foreach (var phonesCollection in nameAndLocation)
                                {
                                    foreach (var phoneNumber in phonesCollection.Value)
                                    {
                                        Console.WriteLine("{0} -> {1} -> {2}", nameEntries.Key, phonesCollection.Key, phoneNumber);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }