Revision 73cdd135 trunk/Pithos.Network/Signature.cs
b/trunk/Pithos.Network/Signature.cs | ||
---|---|---|
163 | 163 |
} |
164 | 164 |
|
165 | 165 |
|
166 |
//The Task Iterator style allows the execution of a sequence of patterns using |
|
167 |
//iterator syntax. |
|
168 |
//This particular variation returns the result of the last task, if there is one |
|
169 |
public static Task<TResult> Iterate<TResult>(IEnumerable<Task> asyncIterator) |
|
170 |
{ |
|
171 |
if (asyncIterator == null) |
|
172 |
throw new ArgumentNullException("asyncIterator"); |
|
173 |
var enumerator = asyncIterator.GetEnumerator(); |
|
174 |
if (enumerator == null) |
|
175 |
throw new InvalidOperationException("Invalid enumerable - GetEnumerator returned null"); |
|
176 |
|
|
177 |
var tcs = new TaskCompletionSource<TResult>(); |
|
178 |
tcs.Task.ContinueWith(_ => enumerator.Dispose(), TaskContinuationOptions.ExecuteSynchronously); |
|
179 |
|
|
180 |
Action<Task> recursiveBody = null; |
|
181 |
recursiveBody = delegate |
|
182 |
{ |
|
183 |
try |
|
184 |
{ |
|
185 |
if (enumerator.MoveNext()) |
|
186 |
enumerator.Current.ContinueWith(recursiveBody, |
|
187 |
TaskContinuationOptions.ExecuteSynchronously); |
|
188 |
else |
|
189 |
{ |
|
190 |
var lastTask = enumerator.Current as Task<TResult>; |
|
191 |
var result = (lastTask !=null ) |
|
192 |
? lastTask.Result |
|
193 |
: default(TResult); |
|
194 |
|
|
195 |
tcs.TrySetResult(result); |
|
196 |
} |
|
197 |
} |
|
198 |
catch (Exception exc) |
|
199 |
{ |
|
200 |
tcs.TrySetException(exc); |
|
201 |
} |
|
202 |
}; |
|
203 |
recursiveBody(null); |
|
204 |
return tcs.Task; |
|
205 |
} |
|
206 |
|
|
207 |
|
|
208 |
/* public static byte[] CalculateTopHash(IEnumerable<byte[]> hashMap, string algorithm) |
|
209 |
{ |
|
210 |
if (hashMap == null) |
|
211 |
throw new ArgumentNullException("hashMap"); |
|
212 |
if (String.IsNullOrWhiteSpace(algorithm)) |
|
213 |
throw new ArgumentNullException("algorithm"); |
|
214 |
Contract.EndContractBlock(); |
|
215 |
|
|
216 |
var hashCount = hashMap.Count(); |
|
217 |
if (hashCount == 0) |
|
218 |
return null; |
|
219 |
using (var hasher = HashAlgorithm.Create(algorithm)) |
|
220 |
{ |
|
221 |
var i = 0; |
|
222 |
var count = hashCount; |
|
223 |
foreach (var block in hashMap) |
|
224 |
{ |
|
225 |
if (i++ != count - 1) |
|
226 |
hasher.TransformBlock(block, 0, block.Length, null, 0); |
|
227 |
else |
|
228 |
hasher.TransformFinalBlock(block, 0, block.Length); |
|
229 |
} |
|
230 |
|
|
231 |
var finalHash = hasher.Hash; |
|
232 |
|
|
233 |
return finalHash; |
|
234 |
} |
|
235 |
}*/ |
|
236 |
|
|
237 | 166 |
public static byte[] CalculateTopHash(IList<byte[]> hashMap, string algorithm) |
238 | 167 |
{ |
239 | 168 |
if (hashMap == null) |
Also available in: Unified diff