Exemplo n.º 1
0
 public VersionedList <T> Add(T item)
 {
     return(new VersionedList <T>(
                Items.Add(item),
                Version + 1,
                Changes.Add(ListChange.Added(Version, Items.Count, item))));
 }
Exemplo n.º 2
0
 public VersionedList <T> Insert(int index, T item)
 {
     return(new VersionedList <T>(
                Items.Insert(index, item),
                Version + 1,
                Changes.Add(ListChange.Added(Version, index, item))));
 }
Exemplo n.º 3
0
        public VersionedList <T> SetItem(int index, T newItem)
        {
            var oldItem = Items[index];

            return(new VersionedList <T>(
                       Items.SetItem(index, newItem),
                       Version + 1,
                       Changes.Add(ListChange.Replaced(Version, index, oldItem, newItem))
                       ));
        }
Exemplo n.º 4
0
        public VersionedList <T> Reverse(int index, int count)
        {
            var start  = index;
            var finish = index + count - 1;

            var moves = Enumerable.Range(0, count)
                        .Select(i => ListChange.Moved(Version + i, start, finish - i, Items[start + i]));

            return(new VersionedList <T>(Items.Reverse(index, count), Version + count, Changes.AddRange(moves)));
        }
Exemplo n.º 5
0
        public VersionedList <T> Replace(T oldValue, T newValue)
        {
            var index = Items.IndexOf(oldValue);

            return(new VersionedList <T>(
                       Items.Replace(oldValue, newValue),
                       Version + 1,
                       Changes.Add(ListChange.Replaced(Version, index, oldValue, newValue))
                       ));
        }
Exemplo n.º 6
0
        public VersionedList <T> Replace(T oldValue, T newValue, IEqualityComparer <T> equalityComparer)
        {
            var index = Items.IndexOf(oldValue, equalityComparer);

            return(new VersionedList <T>(
                       Items.Replace(oldValue, newValue, equalityComparer),
                       Version + 1,
                       Changes.Add(ListChange.Replaced(Version, index, oldValue, newValue))
                       ));
        }
Exemplo n.º 7
0
        public VersionedList <T> AddRange(IEnumerable <T> items)
        {
            var itemsToAdd = items.ToArray();
            var changes    = Enumerable.Range(0, itemsToAdd.Length)
                             .Select(i => ListChange.Added(Version + i, Items.Count + i, itemsToAdd[i]));

            return(new VersionedList <T>(
                       Items.AddRange(items),
                       Version + itemsToAdd.Length,
                       Changes.AddRange(changes)
                       ));
        }
Exemplo n.º 8
0
        private VersionedList <T> _removeWhere(Func <T, int, bool> predicate)
        {
            var pairs = Items
                        .Select((T item, int i) => new { Index = i, Item = item })
                        .ToArray();

            var pairsToRemove = pairs
                                .Where(pair => predicate(pair.Item, pair.Index))
                                .OrderByDescending(pair => pair.Index)
                                .ToArray();

            var remaining = pairs
                            .Where(pair => !predicate(pair.Item, pair.Index))
                            .Select(pair => pair.Item);

            var changes = Enumerable.Range(0, pairsToRemove.Length)
                          .Select(i => ListChange.Removed(Version + i, pairsToRemove[i].Index, pairsToRemove[i].Item));

            return(new VersionedList <T>(
                       ImmutableList.CreateRange(remaining),
                       Version + pairsToRemove.Length,
                       Changes.AddRange(changes)
                       ));
        }
Exemplo n.º 9
0
 private VersionedList(ImmutableList <T> items, int version)
     : this(items, version, ImmutableList <IListChange <T> > .Empty)
 {
     Changes = Changes.Add(ListChange.Reset(Version - 1, Items));
 }