Skip to content

Instantly share code, notes, and snippets.

@georgeRenard
Created March 8, 2018 11:51
Show Gist options
  • Save georgeRenard/f096f054b5fe5ff89b326d02901b935e to your computer and use it in GitHub Desktop.
Save georgeRenard/f096f054b5fe5ff89b326d02901b935e to your computer and use it in GitHub Desktop.
Organization - Last Judge Submission @joro
using System;
using System.Collections;
using System.Collections.Generic;
using Wintellect.PowerCollections;
public class Organization : IOrganization
{
MultiDictionary<string, Person> people =
new MultiDictionary<string, Person>(true);
List<Person> byInsertion = new List<Person>();
OrderedBag<LengthPair> peopleByNameLength = new OrderedBag<LengthPair>();
Dictionary<int, List<Person>> peopleByNameSize = new Dictionary<int, List<Person>>();
public IEnumerator<Person> GetEnumerator()
{
return this.byInsertion.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public int Count { get => this.byInsertion.Count; }
public bool Contains(Person person)
{
return this.people[person.Name].Contains(person);
}
public bool ContainsByName(string name)
{
return this.people.ContainsKey(name);
}
public void Add(Person person)
{
this.people.Add(person.Name, person);
this.byInsertion.Add(person);
this.peopleByNameLength.Add(new LengthPair(person.Name.Length, person));
if (!this.peopleByNameSize.ContainsKey(person.Name.Length))
{
this.peopleByNameSize[person.Name.Length] = new List<Person>();
}
this.peopleByNameSize[person.Name.Length].Add(person);
}
public Person GetAtIndex(int index)
{
if(index < 0 || index >= this.byInsertion.Count)
{
throw new IndexOutOfRangeException();
}
return this.byInsertion[index];
}
public IEnumerable<Person> GetByName(string name)
{
return this.people[name];
}
public IEnumerable<Person> FirstByInsertOrder(int count = 1)
{
for (int i = 0; i < count && i < this.byInsertion.Count; i++)
{
yield return this.byInsertion[i];
}
}
public IEnumerable<Person> SearchWithNameSize(int minLength, int maxLength)
{
foreach (var item in
this.peopleByNameLength
.Range(new LengthPair(minLength, null), true, new LengthPair(maxLength, null), true))
{
yield return item.Person;
}
}
public IEnumerable<Person> GetWithNameSize(int length)
{
if (!this.peopleByNameSize.ContainsKey(length))
{
throw new ArgumentException();
}
return this.peopleByNameSize[length];
}
public IEnumerable<Person> PeopleByInsertOrder()
{
return this.byInsertion;
}
public class LengthPair : IComparable<LengthPair>
{
public int NameLength { get; set; }
public Person Person { get; set; }
public LengthPair(int nameLen, Person person)
{
this.NameLength = nameLen;
this.Person = person;
}
public int CompareTo(LengthPair other)
{
return this.NameLength.CompareTo(other.NameLength);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment