Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OrderedItemList{T}"/> class
        /// </summary>
        /// <param name="orderedItemList">The <see cref="OrderedItemList{T}"/> which values are copied</param>
        /// <param name="container">The owner of this <see cref="OrderedItemList{T}"/></param>
        public OrderedItemList(OrderedItemList <T> orderedItemList, Thing container)
        {
            this.Container   = container;
            this.sortedItems = new SortedList <long, T>(orderedItemList.SortedItems);
            this.random      = new Random(Guid.NewGuid().GetHashCode());
            this.IsComposite = orderedItemList.IsComposite;

            if (this.sortedItems.Any())
            {
                this.highestKey = this.sortedItems.Keys.Max();
            }
        }
Example #2
0
        /// <summary>
        /// Creates and returns a copy of this <see cref="OrderedItemList{T}"/> that preserves order of each item.
        /// If the type T of the contained objects is <see cref="Thing"/> then a clone of each object is made with cloning all contained things,
        /// otherwise the object will be passed to a new item. Be cautious about mutability.
        /// </summary>
        /// <param name="container">The <see cref="Thing"/> that contains this <see cref="OrderedItemList{T}"/> clone.</param>
        /// <returns>
        /// A cloned instance of <see cref="OrderedItemList{T}"/>.
        /// </returns>
        public OrderedItemList <T> Clone(Thing container)
        {
            var clonedOrderedItemList = new OrderedItemList <T>(container, this.IsComposite);

            clonedOrderedItemList.AddOrderedItems(this.sortedItems.Select(x => {
                var item = new OrderedItem()
                {
                    K = x.Key
                };
                var value = x.Value as Thing;
                if (value != null)
                {
                    item.V = value.Clone(true);
                }
                else
                {
                    item.V = x.Value;
                }

                return(item);
            }));

            return(clonedOrderedItemList);
        }