public void IndexedLinkedList_AddFirst()
 {
     IndexedLinkedList<string> list = new IndexedLinkedList<string>();
     list.AddFirst("3");
     list.AddFirst("2");
     list.AddFirst("1");
     Assert.AreEqual(3, list.Count);
     Assert.AreEqual("1", list.First);
 }
 public void IndexedLinkedList_Clear()
 {
     IndexedLinkedList<string> list = new IndexedLinkedList<string>();
     list.AddFirst("3");
     list.AddFirst("2");
     list.AddFirst("1");
     Assert.AreEqual(3, list.Count);
     list.Clear();
     Assert.AreEqual(0, list.Count);
     Assert.IsNull(list.First);
     Assert.IsNull(list.Last);
 }
示例#3
0
        /// <summary>
        /// Creates or updates entity.
        /// </summary>
        /// <remarks>
        /// Update or Create operations are performed in the critical section, so left any processing out-of those functions.
        /// </remarks>
        /// <param name="entityKey">The entity key.</param>
        /// <param name="updateAction">The update action. It will be called when entity with the specified key exists.</param>
        /// <param name="createFunc">The create function. It will be called when entity with the specified key not exists.</param>
        public void CreateOrUpdateEntity(TKey entityKey, Action <TEntity> updateAction, Func <TEntity> createFunc)
        {
            VxArgs.NotNull(entityKey, nameof(entityKey));
            VxArgs.NotNull(updateAction, nameof(updateAction));
            VxArgs.NotNull(createFunc, nameof(createFunc));

            lock (_entities)
            {
                if (_entities.TryGetValue(entityKey, out var entity))
                {
                    updateAction(entity);
                }
                else
                {
                    TEntity newEntity = createFunc();

                    // Following max capacity rule.
                    if (MaxCapacity != null && _gcFifoQueue != null)
                    {
                        if (_gcFifoQueue.Count >= MaxCapacity)
                        {
                            if (_gcFifoQueue.TryDequeue(out var itemToRemove))
                            {
                                _entities.Remove(itemToRemove.Key);
                            }
                        }

                        _gcFifoQueue.AddFirst(entityKey, default);
                    }

                    _entities.Add(entityKey, newEntity);
                    Critical.Assert(
                        newEntity.Key.Equals(entityKey),
                        "Created entity should have the same key as was queried to the CreateOrUpdateEntity method.");
                }
            }
        }