Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/Utils/ComparableList.cs @ 11700

Last change on this file since 11700 was 11700, checked in by jkarder, 9 years ago

#2077: created branch and added first version

File size: 3.6 KB
Line 
1//
2// NullValueAnalysisTests.cs
3//
4// Author:
5//       Luís Reis <luiscubal@gmail.com
6//
7// Copyright (c) 2013 Luís Reis
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25// THE SOFTWARE.
26using System;
27using System.Collections;
28using System.Collections.Generic;
29
30namespace ICSharpCode.NRefactory.Utils
31{
32  /// <summary>
33  /// A list that can be compared to other ComparableLists for equality.
34  /// Can not be used to store null values.
35  /// </summary>
36  public sealed class ComparableList<T> : IList<T>, IEquatable<ComparableList<T>>
37  {
38    List<T> elements;
39
40    public ComparableList()
41    {
42      elements = new List<T> ();
43    }
44
45    public ComparableList(IEnumerable<T> values)
46    {
47      elements = new List<T> (values);
48    }
49
50    public int IndexOf (T item)
51    {
52      if (item == null)
53        throw new ArgumentNullException ("item");
54      return elements.IndexOf (item);
55    }
56
57    public void Insert (int index, T item)
58    {
59      elements.Insert (index, item);
60    }
61
62    public void RemoveAt (int index)
63    {
64      elements.RemoveAt (index);
65    }
66
67    public T this [int index] {
68      get {
69        return elements [index];
70      }
71      set {
72        elements [index] = value;
73      }
74    }
75
76    public void Add (T item)
77    {
78      elements.Add (item);
79    }
80
81    public void Clear ()
82    {
83      elements.Clear ();
84    }
85
86    public bool Contains (T item)
87    {
88      return elements.Contains (item);
89    }
90
91    public void CopyTo (T[] array, int arrayIndex)
92    {
93      elements.CopyTo (array, arrayIndex);
94    }
95
96    public bool Remove (T item)
97    {
98      return elements.Remove (item);
99    }
100
101    public int Count {
102      get {
103        return elements.Count;
104      }
105    }
106
107    public bool IsReadOnly {
108      get {
109        return false;
110      }
111    }
112
113    public IEnumerator<T> GetEnumerator()
114    {
115      return elements.GetEnumerator();
116    }
117
118    IEnumerator IEnumerable.GetEnumerator()
119    {
120      return GetEnumerator ();
121    }
122
123    public override bool Equals (object obj)
124    {
125      return Equals (obj as ComparableList<T>);
126    }
127
128    public bool Equals (ComparableList<T> obj)
129    {
130      if (obj == null || Count != obj.Count) {
131        return false;
132      }
133
134      for (int index = 0; index < Count; ++index) {
135        if (!this [index].Equals(obj [index])) {
136          return false;
137        }
138      }
139
140      return true;
141    }
142
143    public override int GetHashCode ()
144    {
145      int hash = 19;
146      foreach (var item in this) {
147        unchecked {
148          hash *= 31;
149          hash += item.GetHashCode();
150        }
151      }
152      return hash;
153    }
154
155    public static bool operator==(ComparableList<T> item1, ComparableList<T> item2) {
156      if (object.ReferenceEquals (item1, null))
157        return object.ReferenceEquals (item2, null);
158      return item1.Equals (item2);
159    }
160
161    public static bool operator!=(ComparableList<T> item1, ComparableList<T> item2) {
162      return !(item1 == item2);
163    }
164  }
165}
166
Note: See TracBrowser for help on using the repository browser.