private void Instance_AddComic(Comic comic) { if (this.Contains(comic)) { throw new ArgumentException("comic already exists in this collection"); } int index; if (this.sortSelector == ComicSortSelector.Random) { index = General.Randomizer.Next(this.Count + 1); } else { index = this.sortedComics.BinarySearch(comic, ComicComparers.Make(this.sortSelector)); if (index <= 0) { index = ~index; } } this.sortedComics.Insert(index, comic); this.comicAccessor[comic.UniqueIdentifier] = comic; }
/// <summary> /// Behavior is undefined if <c>comic</c> is not part of this view. /// </summary> public int?IndexOf(Comic comic) { if (this.sortSelector is ComicSortSelector.Random) { foreach (var(existingComic, index) in this.sortedComics.Select((comic, index) => (comic, index))) { if (existingComic.UniqueIdentifier == comic.UniqueIdentifier) { return(index); } } return(null); } else { // Note: BinarySearch returns >= 0 if found, <0 if not found var index = this.sortedComics.BinarySearch(comic, ComicComparers.Make(this.sortSelector)); if (index >= 0) { return(index); } else { return(null); } } }
private static void SortComics(List <Comic> comics, ComicSortSelector sortSelector) { if (sortSelector == ComicSortSelector.Random) { General.Shuffle(comics); } else { comics.Sort(ComicComparers.Make(sortSelector)); } }
protected override void RemoveComic(Comic comic) { if (this.sortSelector == ComicSortSelector.Random) { if (!this.sortedComics.Remove(comic)) { throw new ArgumentException("comic doesn't exist in this collection"); } } else { var index = this.sortedComics.BinarySearch(comic, ComicComparers.Make(this.sortSelector)); if (index < 0) { throw new ArgumentException("comic doesn't exist in this collection"); } this.sortedComics.RemoveAt(index); } _ = this.comicAccessor.Remove(comic.UniqueIdentifier); }