Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.Xml-5.5.0/ObjectIterator.cs @ 12966

Last change on this file since 12966 was 11804, checked in by jkarder, 10 years ago

#2077:

  • added fancy xml documentation
  • fixed configurations and plattforms
File size: 3.1 KB
Line 
1// Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21
22namespace ICSharpCode.NRefactory.Xml
23{
24  /// <summary>
25  /// Iterates through an internal object tree.
26  /// </summary>
27  sealed class ObjectIterator
28  {
29    Stack<InternalObject[]> listStack = new Stack<InternalObject[]>();
30    Stack<int> indexStack = new Stack<int>();
31   
32    InternalObject[] objects;
33    int currentIndex;
34    InternalObject currentObject;
35    int currentPosition;
36    internal bool StopAtElementEnd;
37    bool isAtElementEnd;
38   
39    public ObjectIterator(InternalObject[] objects, int startPosition = 0)
40    {
41      this.currentPosition = startPosition;
42      this.objects = objects;
43      if (objects.Length > 0)
44        this.currentObject = objects[0];
45    }
46   
47    public InternalObject CurrentObject {
48      get { return currentObject; }
49    }
50   
51    public int CurrentPosition {
52      get { return currentPosition; }
53    }
54   
55    public bool IsAtElementEnd {
56      get { return isAtElementEnd; }
57    }
58   
59    public int Depth {
60      get { return listStack.Count; }
61    }
62   
63    public void MoveNext()
64    {
65      if (currentObject == null)
66        return;
67      currentIndex++;
68      currentPosition += currentObject.Length;
69      isAtElementEnd = false;
70      while (currentIndex >= objects.Length && listStack.Count > 0) {
71        objects = listStack.Pop();
72        currentIndex = indexStack.Pop();
73        if (this.StopAtElementEnd) {
74          isAtElementEnd = true;
75          break;
76        } else {
77          currentIndex++;
78        }
79      }
80      currentObject = (currentIndex < objects.Length ? objects[currentIndex] : null);
81    }
82   
83    public void MoveInto()
84    {
85      if (isAtElementEnd || !(currentObject is InternalElement)) {
86        MoveNext();
87      } else {
88        listStack.Push(objects);
89        indexStack.Push(currentIndex);
90        objects = currentObject.NestedObjects;
91        currentIndex = 0;
92        currentObject = objects[0];
93      }
94    }
95   
96    /// <summary>
97    /// Skips all nodes in front of 'position'
98    /// </summary>
99    public void SkipTo(int position)
100    {
101      while (currentObject != null && currentPosition < position) {
102        if (currentPosition + currentObject.Length <= position)
103          MoveNext();
104        else
105          MoveInto();
106      }
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.