public bool AddPhone(string name, IEnumerable <string> phoneNumbers) { // Create / find the phonebook entry string nameLowercase = name.ToLowerInvariant(); PhonebookEntry entry; bool isNewEntry = !this.entriesByName.TryGetValue(nameLowercase, out entry); if (isNewEntry) { entry = new PhonebookEntry(name); this.entriesByName.Add(nameLowercase, entry); this.entriesSorted.Add(entry); } // Add the entry by phone foreach (var phoneNumber in phoneNumbers) { this.entriesByPhone.Add(phoneNumber, entry); } // Add / merge the phone numbers entry.Phones.UnionWith(phoneNumbers); return(isNewEntry); }
private static void AddPhoneToEntry(IEnumerable <string> phoneNumbers, PhonebookEntry entry) { foreach (var phoneNumber in phoneNumbers) { entry.PhoneNumbers.Add(phoneNumber); } }
public IList <PhonebookEntry> ListEntries(int startIndex, int count) { if (startIndex < 0 || startIndex >= this.entriesByName.Count) { throw new ArgumentOutOfRangeException("startIndex"); } if (count > this.entriesByName.Count) { throw new ArgumentOutOfRangeException("count"); } if (startIndex + count > this.entriesByName.Count) { throw new ArgumentOutOfRangeException("Invalid parameters"); } var matchedEntries = new PhonebookEntry[count]; for (int i = startIndex; i <= startIndex + count - 1; i++) { PhonebookEntry entry = this.sortedEntries[i]; matchedEntries[i - startIndex] = entry; } return(matchedEntries); }
public PhonebookEntry[] ListEntries(int startIndex, int count) { if (startIndex < 0 || startIndex + count > this.entriesByName.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } PhonebookEntry[] matchedEntries = new PhonebookEntry[count]; for (int i = startIndex; i <= startIndex + count - 1; i++) { PhonebookEntry entry = this.entriesSorted[i]; matchedEntries[i - startIndex] = entry; } return(matchedEntries); }
public PhonebookEntry[] ListEntries(int first, int num) { if (first < 0 || first + num > this.nameEntries.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } PhonebookEntry[] list = new PhonebookEntry[num]; for (int i = first; i <= first + num - 1; i++) { PhonebookEntry entry = this.sortedEntries[i]; list[i - first] = entry; } return(list); }
public IList <PhonebookEntry> ListEntries(int startIndex, int count) { if (startIndex < 0 || startIndex + count > this.entries.Count) { throw new ArgumentOutOfRangeException("Invalid start index or count."); } //objListOrder.Sort((x, y) => x.OrderDate.CompareTo(y.OrderDate)); this.entries.Sort((x, y) => x.Name.CompareTo(y.Name)); var listedEntries = new PhonebookEntry[count]; for (int i = startIndex; i <= startIndex + count - 1; i++) { var entry = this.entries[i]; listedEntries[i - startIndex] = entry; } return(listedEntries); }
public bool AddPhone(string name, IEnumerable <string> phoneNumbers) { var nameLowercase = name.ToLowerInvariant(); PhonebookEntry entry; bool isNewEntry = !this.entriesByName.TryGetValue(nameLowercase, out entry); if (isNewEntry) { entry = new PhonebookEntry(name); this.entriesByName.Add(nameLowercase, entry); this.sortedEntries.Add(entry); } foreach (var num in phoneNumbers) { this.entriesByPhoneNumber.Add(num, entry); } entry.PhoneNumbers.UnionWith(phoneNumbers); return(isNewEntry); }
public bool AddPhone(string name, IEnumerable <string> phoneNumbers) { var matchedEntry = from e in this.entries where e.Name.ToLowerInvariant() == name.ToLowerInvariant() select e; //matchedEntry = this.entries // .Where(entry => string.Equals(entry.Name, name, StringComparison.InvariantCultureIgnoreCase)) // .ToList(); bool isNewEntry; if (matchedEntry.Count() == 0) { PhonebookEntry entry = new PhonebookEntry(name); AddPhoneToEntry(phoneNumbers, entry); this.entries.Add(entry); isNewEntry = true; } else if (matchedEntry.Count() == 1) { PhonebookEntry existingEntry = matchedEntry.First(); var newPhoneNumbers = phoneNumbers .Where(phoneNumber => !existingEntry.PhoneNumbers.Contains(phoneNumber)) .ToList(); AddPhoneToEntry(newPhoneNumbers, existingEntry); isNewEntry = false; } else { throw new InvalidOperationException("Duplicate name."); } return(isNewEntry); }
public bool AddPhone(string name, IEnumerable <string> nums) { string nameToLower = name.ToLowerInvariant(); PhonebookEntry entry; bool isNew = !this.nameEntries.TryGetValue(nameToLower, out entry); if (isNew) { entry = new PhonebookEntry { Name = name, EntryPhoneNumbers = new SortedSet <string>() }; this.nameEntries.Add(nameToLower, entry); this.sortedEntries.Add(entry); } foreach (var num in nums) { this.phoneEntries.Add(num, entry); } entry.EntryPhoneNumbers.UnionWith(nums); return(isNew); }