2 using System.Collections;
3 using System.Collections.Generic;
5 using System.Linq.Expressions;
7 using Pithos.Client.WPF.SelectiveSynch;
8 using Pithos.Interfaces;
10 namespace Pithos.Client.WPF.Utils
12 public static class EnumerableExtensions
14 public static IEnumerable<T> Slice<T>(this IEnumerable<T> collection, int start, int end)
19 if (collection == null)
20 throw new ArgumentNullException("collection");
22 // Optimise item count for ICollection interfaces.
23 if (collection is ICollection<T>)
24 count = ((ICollection<T>)collection).Count;
25 else if (collection is ICollection)
26 count = ((ICollection)collection).Count;
29 count = collection.Count();
32 // Get start/end indexes, negative numbers start at the end of the collection.
39 foreach (var item in collection)
51 public static IEnumerable<Node<T>> ToTree<TSource,T>(this IEnumerable<TSource> enumerable,Func<TSource,string> pathFunc,Func<TSource,T> valueFunc,string delimiter="/")
53 var orderedItems=enumerable.OrderBy(pathFunc);
54 var lookups = new Dictionary<string,Node<T>>();
55 var nodes = new List<Node<T>>();
56 foreach (var item in orderedItems)
58 var path = pathFunc(item);
59 var value = valueFunc(item);
60 var newNode = new Node<T> { Path = path,Data=value };
61 lookups[path] = newNode;
63 var lastIndex = path.LastIndexOf(delimiter, StringComparison.Ordinal);
64 var upTo = lastIndex < 0 ? path.Length - 1 : lastIndex;
65 var parentPath = path.Substring(0, upTo);
68 if (lookups.TryGetValue(parentPath, out parent))
70 parent.Children.Add(newNode);
71 parent.Children.Sort((x,y)=>String.CompareOrdinal(x.Path, y.Path));
80 public static List<DirectoryRecord> ToTree(this IEnumerable<ObjectInfo> enumerable)
82 var orderedItems=enumerable.OrderBy(o=>o.Uri.ToString());
83 var lookups = new Dictionary<string,DirectoryRecord>();
84 var nodes = new List<DirectoryRecord>();
85 foreach (var item in orderedItems)
87 var path = item.Uri.ToString();
88 var newNode = new DirectoryRecord{ DisplayName=item.Name,ObjectInfo=item};
89 lookups[path] = newNode;
91 var lastIndex = path.LastIndexOf("/", StringComparison.Ordinal);
92 var upTo = lastIndex < 0 ? path.Length - 1 : lastIndex;
93 var parentPath = path.Substring(0, upTo);
95 DirectoryRecord parent;
96 if (lookups.TryGetValue(parentPath, out parent))
98 parent.Directories.Add(newNode);
99 parent.Directories.Sort((x,y)=>String.CompareOrdinal(x.Uri.ToString(), y.Uri.ToString()));