1- using System ;
2- using System . Collections ;
3- using System . Collections . Generic ;
4- using System . Linq ;
1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright file="HashStack.cs" company="Tynamix">
3+ // © by GothicX
4+ // </copyright>
5+ // <summary>
6+ // A stack-like collection that uses a HashSet under the covers, so elements also need to be unique.
7+ // </summary>
8+ // --------------------------------------------------------------------------------------------------------------------
59
610namespace Tynamix . ObjectFiller
711{
12+ using System . Collections ;
13+ using System . Collections . Generic ;
14+
815 /// <summary>
916 /// A stack-like collection that uses a HashSet under the covers, so elements also need to be unique.
1017 /// </summary>
18+ /// <typeparam name="T">
19+ /// Type of the Hashstack
20+ /// </typeparam>
1121 /// <remarks>
1222 /// I decided to follow the HashSet more closely than the stack,
1323 /// which is why Push returns a boolean. I still think throwing an exception makes more
1424 /// sense, but if HashSet does it people should be already familiar with the paradigm.
15- ///
1625 /// Pop however follows the standard stack implementation, of course.
1726 /// </remarks>
1827 internal class HashStack < T > : IEnumerable < T >
1928 {
20- private readonly HashSet < T > _set ;
21- private readonly Stack < T > _stack ;
29+ /// <summary>
30+ /// The _set.
31+ /// </summary>
32+ private readonly HashSet < T > set ;
2233
23- internal HashStack ( ) : this ( EqualityComparer < T > . Default )
34+ private readonly Stack < T > stack ;
35+
36+ /// <summary>
37+ /// Initializes a new instance of the <see cref="HashStack{T}"/> class.
38+ /// </summary>
39+ internal HashStack ( )
40+ : this ( EqualityComparer < T > . Default )
2441 {
2542 }
2643
44+ /// <summary>
45+ /// Initializes a new instance of the <see cref="HashStack{T}"/> class.
46+ /// </summary>
47+ /// <param name="comparer">
48+ /// The comparer.
49+ /// </param>
2750 internal HashStack ( IEqualityComparer < T > comparer )
2851 {
29- _set = new HashSet < T > ( comparer ) ;
30- _stack = new Stack < T > ( ) ;
52+ this . set = new HashSet < T > ( comparer ) ;
53+ this . stack = new Stack < T > ( ) ;
3154 }
3255
3356 /// <summary>
@@ -36,40 +59,73 @@ internal HashStack(IEqualityComparer<T> comparer)
3659 /// <returns>True if the element is added; false if the element is already present.</returns>
3760 internal bool Push ( T item )
3861 {
39- var added = _set . Add ( item ) ;
40- if ( added ) _stack . Push ( item ) ;
62+ var added = this . set . Add ( item ) ;
63+ if ( added )
64+ {
65+ this . stack . Push ( item ) ;
66+ }
67+
4168 return added ;
4269 }
4370
4471 /// <summary>
4572 /// Removes and returns the last added element.
4673 /// </summary>
74+ /// <returns>
75+ /// The item of type <see cref="T"/>
76+ /// </returns>
4777 internal T Pop ( )
4878 {
49- var last = _stack . Pop ( ) ;
50- _set . Remove ( last ) ;
79+ var last = this . stack . Pop ( ) ;
80+ this . set . Remove ( last ) ;
5181 return last ;
5282 }
5383
84+ /// <summary>
85+ /// Clears the hash stack
86+ /// </summary>
5487 internal void Clear ( )
5588 {
56- _set . Clear ( ) ;
57- _stack . Clear ( ) ;
89+ this . set . Clear ( ) ;
90+ this . stack . Clear ( ) ;
5891 }
5992
93+ /// <summary>
94+ /// Checks if the <see cref="HashStack{T}"/> contains the <see cref="item"/>
95+ /// </summary>
96+ /// <param name="item">
97+ /// The item.
98+ /// </param>
99+ /// <returns>
100+ /// True if the <see cref="HashStack{T}"/> contains the item
101+ /// </returns>
60102 internal bool Contains ( T item )
61103 {
62- return _set . Contains ( item ) ;
104+ return this . set . Contains ( item ) ;
63105 }
64106
107+ /// <summary>
108+ /// Returns an enumerator that iterates through the collection.
109+ /// </summary>
110+ /// <returns>
111+ /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
112+ /// </returns>
113+ /// <filterpriority>1</filterpriority>
65114 public IEnumerator < T > GetEnumerator ( )
66115 {
67- return _stack . GetEnumerator ( ) ;
116+ return this . stack . GetEnumerator ( ) ;
68117 }
69118
119+ /// <summary>
120+ /// Returns an enumerator that iterates through a collection.
121+ /// </summary>
122+ /// <returns>
123+ /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
124+ /// </returns>
125+ /// <filterpriority>2</filterpriority>
70126 IEnumerator IEnumerable . GetEnumerator ( )
71127 {
72- return GetEnumerator ( ) ;
128+ return this . GetEnumerator ( ) ;
73129 }
74130 }
75131}
0 commit comments