PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Microsoft.Dynamic/Debugging/CollectionUtils.cs

#
C# | 70 lines | 45 code | 9 blank | 16 comment | 4 complexity | 21ef97a3ab13d277ccdb0ca2c08263cf MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. namespace Microsoft.Scripting.Debugging {
  19. internal static class CollectionUtils {
  20. internal static T[] RemoveLast<T>(this T[] array) {
  21. T[] result = new T[array.Length - 1];
  22. Array.Copy(array, 0, result, 0, result.Length);
  23. return result;
  24. }
  25. internal static bool ListEquals<T>(this ICollection<T> first, ICollection<T> second) {
  26. if (first.Count != second.Count) {
  27. return false;
  28. }
  29. var cmp = EqualityComparer<T>.Default;
  30. var f = first.GetEnumerator();
  31. var s = second.GetEnumerator();
  32. while (f.MoveNext()) {
  33. s.MoveNext();
  34. if (!cmp.Equals(f.Current, s.Current)) {
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40. internal static int ListHashCode<T>(this IEnumerable<T> list) {
  41. var cmp = EqualityComparer<T>.Default;
  42. int h = 6551;
  43. foreach (T t in list) {
  44. h ^= (h << 5) ^ cmp.GetHashCode(t);
  45. }
  46. return h;
  47. }
  48. }
  49. // Compares two ICollection<T>'s using element equality
  50. internal sealed class ListEqualityComparer<T> : EqualityComparer<ICollection<T>> {
  51. internal static readonly ListEqualityComparer<T> Instance = new ListEqualityComparer<T>();
  52. private ListEqualityComparer() { }
  53. // EqualityComparer<T> handles null and object identity for us
  54. public override bool Equals(ICollection<T> x, ICollection<T> y) {
  55. return x.ListEquals(y);
  56. }
  57. public override int GetHashCode(ICollection<T> obj) {
  58. return obj.ListHashCode();
  59. }
  60. }
  61. }