~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Analysis/Profiler/Controller/Queries/NodePath.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Linq;
 
6
using System.Collections.Generic;
 
7
 
 
8
namespace ICSharpCode.Profiler.Controller.Queries
 
9
{
 
10
        /// <summary>
 
11
        /// Describes an absolute path to an CallTreeNode.
 
12
        /// </summary>
 
13
        public sealed class NodePath : IEquatable<NodePath>, IEnumerable<int>
 
14
        {
 
15
                /// <summary>
 
16
                /// Describes an empty NodePath.
 
17
                /// </summary>
 
18
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes",
 
19
                                                                 Justification = "NodePath is immutable")]
 
20
                public static readonly NodePath Empty = new NodePath(0, null);
 
21
                
 
22
                int lastId;
 
23
                NodePath previous;
 
24
                
 
25
                /// <summary>
 
26
                /// Gets the top-most/last NameId in the path.
 
27
                /// </summary>
 
28
                public int LastId {
 
29
                        get { return lastId; }
 
30
                }
 
31
                
 
32
                /// <summary>
 
33
                /// Gets a reference to the previous segment of the path.
 
34
                /// </summary>
 
35
                public NodePath Previous {
 
36
                        get { return previous; }
 
37
                }
 
38
                
 
39
                NodePath(int id, NodePath previous)
 
40
                {
 
41
                        this.lastId = id;
 
42
                        this.previous = previous;
 
43
                }
 
44
                
 
45
                /// <summary>
 
46
                /// Creates a new NodePath from this with a new Name Id segment attached.
 
47
                /// </summary>
 
48
                public NodePath Append(int newValue)
 
49
                {
 
50
                        return new NodePath(newValue, this);
 
51
                }
 
52
                
 
53
                /// <summary>
 
54
                /// Returns whether the other NodePath is equal to this NodePath.
 
55
                /// </summary>
 
56
                public bool Equals(NodePath other)
 
57
                {
 
58
                        if (other == null)
 
59
                                return false;
 
60
                        
 
61
                        return other.lastId == this.lastId && object.Equals(other.previous, this.previous);
 
62
                }
 
63
                
 
64
                /// <inheritdoc/>
 
65
                public override bool Equals(object obj)
 
66
                {
 
67
                        return Equals(obj as NodePath);
 
68
                }
 
69
                
 
70
                /// <inheritdoc/>
 
71
                public override int GetHashCode()
 
72
                {
 
73
                        // (a[0] * p + a[1]) * p + a[2]
 
74
                        const int hashPrime = 1000000007;
 
75
                        
 
76
                        unchecked {
 
77
                                return ((previous != null) ? previous.GetHashCode() : 0) * hashPrime + lastId;
 
78
                        }
 
79
                }
 
80
                
 
81
                /// <inheritdoc/>
 
82
                public IEnumerator<int> GetEnumerator()
 
83
                {
 
84
                        var list = new List<int>();
 
85
                        var me = this;
 
86
                        while (me != null) {
 
87
                                list.Add(me.lastId);
 
88
                                me = me.previous;
 
89
                        }
 
90
                        list.Reverse();
 
91
                        return list.GetEnumerator();
 
92
                }
 
93
                
 
94
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 
95
                {
 
96
                        return GetEnumerator();
 
97
                }
 
98
                
 
99
                /// <inheritdoc/>
 
100
                public override string ToString()
 
101
                {
 
102
                        return ((previous != null) ? previous.ToString() + "->" : "") + lastId;
 
103
                }
 
104
        }
 
105
}