示例#1
0
        public void Set(K key, V value)
        {
            if (_KeyLinkMap.Count == _CacheSizeLimit)
            {
                _KeyLinkMap.Remove(Tail.Self);
                Tail      = Tail.Previous;
                Tail.Next = null;
            }
            var new_head = new LinkedElement()
            {
                Self = key,
                Next = Head,
            };

            _KeyLinkMap[key] = new LinkMappedEntry()
            {
                Data = value,
                Link = new_head,
            };
            if (Head != null)
            {
                Head.Previous = new_head;
            }
            Head = new_head;
        }
示例#2
0
        /// <summary>
        /// Inserts given element at given index
        /// </summary>
        /// <param name="element">Element</param>
        /// <param name="index">insertion index</param>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the index is either lesser
        /// than 0 or greater than the length of the collection</exception>
        public override void InsertAt(T element, int index)
        {
            if (index < 0 || index > Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (index == 0)
            {
                if (_head == null)
                {
                    Add(element);
                    return;
                }
                var prevHead = _head;
                _head = new LinkedElement <T>(element, prevHead);
                ++Length;
                return;
            }

            var current = _head;

            for (var i = 0; i < index - 1; i++)
            {
                current = current.Next;
            }
            var previousNext = current.Next;

            current.Next = new LinkedElement <T>(element, previousNext);
            ++Length;
        }
示例#3
0
        public T PopBack()
        {
            if (Empty)
            {
                throw new Exception("Queue empty.");
            }

            LinkedElement <T> temp = first;


            while (temp.getParent() != last)
            {
                if (temp.getParent() == null)
                {
                    last  = null;
                    first = null;
                    return(temp.valor);
                }
                temp = temp.getParent();
            }

            temp.setParent(null);

            LinkedElement <T> temp2 = last;

            last = temp;
            return(temp2.valor);
        }
示例#4
0
        /// <summary>
        /// Removes element of the collection
        /// </summary>
        /// <param name="index">Deletion index</param>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the index is either lesser
        /// than 0 or greater or equal to the length of the collection</exception>
        public void RemoveAt(int removeIndex)
        {
            if (_head == null)
            {
                return;
            }

            if (removeIndex < 0 || removeIndex >= Length)
            {
                throw new IndexOutOfRangeException();
            }

            if (removeIndex == 0)
            {
                _head = _head.Next;
                --Length;
                return;
            }

            var current = _head;
            LinkedElement <T> previous = null;

            for (var i = 0; i < removeIndex; i++)
            {
                previous = current;
                current  = current.Next;
            }
            previous.Next = current.Next;
            current       = null;
            --Length;
        }
示例#5
0
        public T leave()
        {
            if (Empty)
            {
                throw new Exception("Queue empty.");
            }
            LinkedElement <T> temp = first;

            first = first.getParent();
            return(temp.valor);
        }
示例#6
0
        public void Push(T valor)
        {
            if (valor == null)
            {
                throw new Exception("Invalid value: null");
            }
            LinkedElement <T> element = new LinkedElement <T>(valor);

            element.setParent(top);
            top = element;
        }
示例#7
0
        public T Pop()
        {
            if (Empty)
            {
                throw new Exception("Stack empty.");
            }
            LinkedElement <T> temp = top;

            top = top.getParent();
            return(temp.valor);
        }
示例#8
0
文件: 2.cs 项目: V4lonforth/ADS
        public T Dequeue()
        {
            T value = last.Value;

            last = last.NextElement;
            if (last == null)
            {
                first = null;
            }
            return(value);
        }
示例#9
0
文件: 2.cs 项目: V4lonforth/ADS
        public void Enqueue(T element)
        {
            LinkedElement newElement = new LinkedElement(element);

            if (first != null)
            {
                first.NextElement = newElement;
            }
            else
            {
                last = newElement;
            }
            first = newElement;
        }
示例#10
0
        /// <summary>
        /// Adds element to the end of the collection
        /// </summary>
        /// <param name="element">Element that will be added</param>
        public void Add(T element)
        {
            ++Length;
            if (_head == null)
            {
                _head = new LinkedElement <T>(element);
                return;
            }

            var current = _head;

            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = new LinkedElement <T>(element);
        }
示例#11
0
        public T PopFront()
        {
            if (Empty)
            {
                throw new Exception("Queue empty.");
            }

            LinkedElement <T> temp = first;

            first = first.getParent();

            if (first == null)
            {
                last = null;
            }

            return(temp.valor);
        }
示例#12
0
        public void Enter(T valor)
        {
            if (valor == null)
            {
                throw new Exception("Invalid value: null");
            }
            LinkedElement <T> element = new LinkedElement <T>(valor);

            if (first == null)
            {
                first = element;
            }
            if (last != null)
            {
                last.setParent(element);
            }
            last = element;
        }
示例#13
0
        /// <summary>
        /// Adds element to collection
        /// </summary>
        /// <param name="element">Element</param>
        public void Enqueue(T element)
        {
            ++Length;
            if (_first == null)
            {
                _first = new LinkedElement <T>(element);
                _last  = _first;
                return;
            }

            if (_last == null)
            {
                _last       = new LinkedElement <T>(element);
                _first.Next = _last;
                return;
            }

            _last.Next = new LinkedElement <T>(element);
        }
示例#14
0
        /// <summary>
        /// Returns the first element of the collection
        /// </summary>
        /// <param name="index">Index of the element</param>
        /// <returns>
        /// The element on that particular index
        /// </returns>
        /// <exception cref="System.IndexOutOfRangeException">Thrown when the collection is empty</exception>
        public T Dequeue()
        {
            if (_first == null)
            {
                throw new IndexOutOfRangeException();
            }

            --Length;
            var currentFirst = _first;

            if (currentFirst.Next == null)
            {
                _first = _last = null;
                return(currentFirst.Value);
            }

            _first = currentFirst.Next;
            return(currentFirst.Value);
        }
示例#15
0
        public void PushFront(T valor)
        {
            if (valor == null)
            {
                throw new Exception("Invalid value: null");
            }
            LinkedElement <T> element = new LinkedElement <T>(valor);

            if (first != null)
            {
                element.setParent(first);
            }
            else
            {
                last = element;
            }

            first = element;
        }
示例#16
0
        public V Get(K key)
        {
            var entry = _KeyLinkMap[key];
            var link  = entry.Link;

            // Extract
            if (link.Next != null)
            {
                link.Next.Previous = link.Previous;
            }
            if (link.Previous != null)
            {
                link.Previous.Next = link.Next;
            }
            // Put at head
            link.Next     = Head;
            Head.Previous = link;
            Head          = link;
            return(entry.Data);
        }
示例#17
0
        public void DoWork(IRequest request)
        {
            EntryRequestType            entryRequestType;
            string                      resourceKindName;
            ICorrelatedResSyncInfoStore correlatedResSyncStore;
            bool includeDataPayloads;   //?includePayloads value

            CorrelatedResSyncInfo[] correlatedResSyncInfos;
            int totalResult;

            #region initialization

            // multi or single entry request?
            entryRequestType = (String.IsNullOrEmpty(_requestContext.ResourceKey)) ? EntryRequestType.Multi : EntryRequestType.Single;

            resourceKindName = _requestContext.ResourceKind.ToString();

            string tmpValue;
            // ?includePayloads
            includeDataPayloads = false;    // default value, but check for settings now
            if (_requestContext.SdataUri.QueryArgs.TryGetValue("includePayload", out tmpValue))
            {
                includeDataPayloads = System.Xml.XmlConvert.ToBoolean(tmpValue);
            }

            // get store to request the correlations
            correlatedResSyncStore = RequestReceiver.NorthwindAdapter.StoreLocator.GetCorrelatedResSyncStore(_requestContext.SdataContext);

            #endregion

            // receive correlated resource synchronization entries
            if (entryRequestType == EntryRequestType.Multi)
            {
                int pageNumber = FeedMetadataHelpers.GetPageNumber(_requestContext);

                correlatedResSyncInfos = correlatedResSyncStore.GetPaged(resourceKindName, pageNumber, FeedMetadataHelpers.DEFAULT_ITEMS_PER_PAGE, out totalResult);
            }
            else
            {
                Guid uuid = (Guid)TypeDescriptor.GetConverter(typeof(Guid)).ConvertFrom(_requestContext.ResourceKey);
                correlatedResSyncInfos = correlatedResSyncStore.GetByUuid(resourceKindName, new Guid[] { uuid });
                totalResult            = correlatedResSyncInfos.Length;
            }

            // Create the feed
            SyncFeed feed = new SyncFeed();

            // initialize the feed
            feed.Id    = FeedMetadataHelpers.BuildBaseUrl(_requestContext, FeedMetadataHelpers.RequestKeywordType.linked);
            feed.Title = string.Format("{0} Linking Feed", resourceKindName);

            #region PAGING & OPENSEARCH

            /* PAGING */
            feed.Links = FeedMetadataHelpers.CreatePageFeedLinks(_requestContext, totalResult, FeedMetadataHelpers.RequestKeywordType.linked);

            /* OPENSEARCH */
            PageController pageLinkBuilder = FeedMetadataHelpers.GetPageLinkBuilder(_requestContext, totalResult, FeedMetadataHelpers.RequestKeywordType.linked);

            feed.Opensearch_ItemsPerPageElement = pageLinkBuilder.GetOpensearch_ItemsPerPageElement();
            feed.Opensearch_StartIndexElement   = pageLinkBuilder.GetOpensearch_StartIndexElement();
            feed.Opensearch_TotalResultsElement = pageLinkBuilder.GetOpensearch_TotalResultsElement();

            #endregion

            feed.FeedType = (entryRequestType == EntryRequestType.Multi) ? FeedType.Linked : FeedType.LinkedSingle;

            IEntityWrapper wrapper = null;
            if (includeDataPayloads)
            {
                wrapper = EntityWrapperFactory.Create(_requestContext.ResourceKind, _requestContext);
            }

            for (int i = 0; i < correlatedResSyncInfos.Length; i++)
            {
                SyncFeedEntry entry = null;

                if (includeDataPayloads)
                {
                    entry = wrapper.GetFeedEntry(correlatedResSyncInfos[i].LocalId);
                }
                else
                {
                    entry = new SyncFeedEntry();
                }

                entry.Id    = FeedMetadataHelpers.BuildLinkedEntryUrl(_requestContext, correlatedResSyncInfos[i].ResSyncInfo.Uuid);
                entry.Title = FeedMetadataHelpers.BuildLinkedEntryTitle(_requestContext, correlatedResSyncInfos[i].ResSyncInfo.Uuid);

                #region LINKED ELEMENT

                LinkedElement linkedElement = new LinkedElement();
                linkedElement.Resource = FeedMetadataHelpers.BuildEntryResourceUrl(_requestContext, correlatedResSyncInfos[i].LocalId);
                linkedElement.Uuid     = correlatedResSyncInfos[i].ResSyncInfo.Uuid;
                entry.Linked           = linkedElement;

                #endregion

                feed.Entries.Add(entry);
            }

            request.Response.Serializer  = new SyncFeedSerializer();
            request.Response.Feed        = (IFeed)feed;
            request.Response.ContentType = (entryRequestType == EntryRequestType.Multi) ? MediaType.Atom : MediaType.AtomEntry;
        }
示例#18
0
 public LinkedElement(T value, LinkedElement <T> next = null)
 {
     Value = value;
     Next  = next;
 }
示例#19
0
 public LinkedList()
 {
     _head = null;
 }
示例#20
0
        public void DoWork(IRequest request)
        {
            // only atom entry supported!!!
            if (request.ContentType != Sage.Common.Syndication.MediaType.AtomEntry)
            {
                throw new RequestException("Atom entry content type expected");
            }

            // deserialize the request stream to a SyncFeedEntry
            SyncFeedEntry entry  = new SyncFeedEntry();
            XmlReader     reader = XmlReader.Create(request.Stream);

            reader.MoveToContent();
            entry.ReadXml(reader, typeof(SyncDigestPayload));

            if (null == entry.Linked)
            {
                throw new RequestException("Invalid content: element 'linked' missing");
            }

            // We check for equality of the urls of the request url and
            // in the linked element up to the resourceKind.

            string         requestEndpointUrl            = _requestContext.OriginEndPoint;
            RequestContext entryResourceAsRequestContext = new RequestContext(new Sage.Common.Syndication.SDataUri(entry.Linked.Resource));    // TODO: not really nice here.
            string         linkedResourceUrl             = entryResourceAsRequestContext.OriginEndPoint;

            if (!string.Equals(requestEndpointUrl, linkedResourceUrl, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new RequestException("Request url and linked entry resource not matching.");
            }

            string resourceKindName = _requestContext.ResourceKind.ToString();

            Guid   currentUuid = (Guid)TypeDescriptor.GetConverter(typeof(Guid)).ConvertFrom(_requestContext.ResourceKey);
            Guid   newUuid     = entry.Linked.Uuid;
            string newLocalId  = entryResourceAsRequestContext.SdataUri.CollectionPredicate;

            // update the correlation entry in the sync store.
            // if the uuid should be updated we have to remove the current correlation and then add a new one.
            // otherwise we update the current correlation.
            ICorrelatedResSyncInfoStore correlatedResSyncInfoStore = RequestReceiver.NorthwindAdapter.StoreLocator.GetCorrelatedResSyncStore(_requestContext.SdataContext);
            // create the target correlation
            ResSyncInfo           targetResSyncInfo = new ResSyncInfo(newUuid, requestEndpointUrl, 0, string.Empty, DateTime.Now);
            CorrelatedResSyncInfo targetInfo        = new CorrelatedResSyncInfo(newLocalId, targetResSyncInfo);

            if (currentUuid == newUuid)
            {
                correlatedResSyncInfoStore.Update(resourceKindName, targetInfo);
            }
            else
            {
                correlatedResSyncInfoStore.Delete(resourceKindName, currentUuid);
                correlatedResSyncInfoStore.Add(resourceKindName, targetInfo);
            }

            // create response
            SyncFeed feed = new SyncFeed();

            feed.FeedType = FeedType.LinkedSingle;

            // create entry
            SyncFeedEntry responseEntry = new SyncFeedEntry();

            responseEntry.Id    = FeedMetadataHelpers.BuildLinkedEntryUrl(_requestContext, targetInfo.ResSyncInfo.Uuid);
            responseEntry.Title = FeedMetadataHelpers.BuildLinkedEntryTitle(_requestContext, targetInfo.ResSyncInfo.Uuid);

            LinkedElement linkedElement = new LinkedElement();

            linkedElement.Resource = FeedMetadataHelpers.BuildEntryResourceUrl(_requestContext, targetInfo.LocalId);
            linkedElement.Uuid     = targetInfo.ResSyncInfo.Uuid;
            responseEntry.Linked   = linkedElement;

            feed.Entries.Add(responseEntry);

            request.Response.Serializer  = new SyncFeedSerializer();
            request.Response.Feed        = (IFeed)feed;
            request.Response.ContentType = MediaType.AtomEntry;
        }