1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
using System;
using System.Collections;
namespace IceUtilInternal
{
public sealed class Arrays
{
public static bool Equals(object[] arr1, object[] arr2)
{
if(object.ReferenceEquals(arr1, arr2))
{
return true;
}
if((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null))
{
return false;
}
if(arr1.Length == arr2.Length)
{
for(int i = 0; i < arr1.Length; i++)
{
if(arr1[i] == null)
{
if(arr2[i] != null)
{
return false;
}
}
else if(!arr1[i].Equals(arr2[i]))
{
return false;
}
}
return true;
}
return false;
}
public static bool Equals(Array arr1, Array arr2)
{
if(object.ReferenceEquals(arr1, arr2))
{
return true;
}
if((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null))
{
return false;
}
if(arr1.Length == arr2.Length)
{
IEnumerator e1 = arr1.GetEnumerator();
IEnumerator e2 = arr2.GetEnumerator();
while(e1.MoveNext())
{
e2.MoveNext();
if(e1.Current == null)
{
if(e2.Current != null)
{
return false;
}
}
else if(!e1.Current.Equals(e2.Current))
{
return false;
}
}
return true;
}
return false;
}
public static int GetHashCode(object[] arr)
{
int h = 0;
for(int i = 0; i < arr.Length; i++)
{
h = 5 * h + arr[i].GetHashCode();
}
return h;
}
public static int GetHashCode(Array arr)
{
int h = 0;
foreach(object o in arr)
{
h = 5 * h + o.GetHashCode();
}
return h;
}
public static void Sort(ref ArrayList array, IComparer comparator)
{
//
// This Sort method implements the merge sort algorithm
// which is a stable sort (unlike the Sort method of the
// System.Collections.ArrayList which is unstable).
//
Sort1(ref array, 0, array.Count, comparator);
}
private static void Sort1(ref ArrayList array, int begin, int end, IComparer comparator)
{
int mid;
if(end - begin <= 1)
{
return;
}
mid = (begin + end) / 2;
Sort1(ref array, begin, mid, comparator);
Sort1(ref array, mid, end, comparator);
Merge(ref array, begin, mid, end, comparator);
}
private static void Merge(ref ArrayList array, int begin, int mid, int end, IComparer comparator)
{
int i = begin;
int j = mid;
int k = 0;
object[] tmp = new object[end - begin];
while(i < mid && j < end)
{
if(comparator.Compare(array[i], array[j]) <= 0)
{
tmp[k++] = array[i++];
}
else
{
tmp[k++] = array[j++];
}
}
while(i < mid)
{
tmp[k++] = array[i++];
}
while(j < end)
{
tmp[k++] = array[j++];
}
for(i = 0; i < (end - begin); ++i)
{
array[begin + i] = tmp[i];
}
}
}
}
|