root / trunk / Pithos.Client.WPF / Utils / EnumerableExtensions.cs @ 8f44fd3a
History | View | Annotate | Download (3.7 kB)
1 |
using System; |
---|---|
2 |
using System.Collections; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Linq.Expressions; |
6 |
using System.Text; |
7 |
using Pithos.Client.WPF.SelectiveSynch; |
8 |
using Pithos.Interfaces; |
9 |
|
10 |
namespace Pithos.Client.WPF.Utils |
11 |
{ |
12 |
public static class EnumerableExtensions |
13 |
{ |
14 |
public static IEnumerable<T> Slice<T>(this IEnumerable<T> collection, int start, int end) |
15 |
{ |
16 |
int index = 0; |
17 |
int count = 0; |
18 |
|
19 |
if (collection == null) |
20 |
throw new ArgumentNullException("collection"); |
21 |
|
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; |
27 |
else |
28 |
{ |
29 |
count = collection.Count(); |
30 |
} |
31 |
|
32 |
// Get start/end indexes, negative numbers start at the end of the collection. |
33 |
if (start < 0) |
34 |
start += count; |
35 |
|
36 |
if (end < 0) |
37 |
end += count; |
38 |
|
39 |
foreach (var item in collection) |
40 |
{ |
41 |
if (index >= end) |
42 |
yield break; |
43 |
|
44 |
if (index >= start) |
45 |
yield return item; |
46 |
|
47 |
++index; |
48 |
} |
49 |
} |
50 |
|
51 |
public static IEnumerable<Node<T>> ToTree<TSource,T>(this IEnumerable<TSource> enumerable,Func<TSource,string> pathFunc,Func<TSource,T> valueFunc,string delimiter="/") |
52 |
{ |
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) |
57 |
{ |
58 |
var path = pathFunc(item); |
59 |
var value = valueFunc(item); |
60 |
var newNode = new Node<T> { Path = path,Data=value }; |
61 |
lookups[path] = newNode; |
62 |
|
63 |
var lastIndex = path.LastIndexOf(delimiter, StringComparison.Ordinal); |
64 |
var upTo = lastIndex < 0 ? path.Length - 1 : lastIndex; |
65 |
var parentPath = path.Substring(0, upTo); |
66 |
|
67 |
Node<T> parent; |
68 |
if (lookups.TryGetValue(parentPath, out parent)) |
69 |
{ |
70 |
parent.Children.Add(newNode); |
71 |
parent.Children.Sort((x,y)=>String.CompareOrdinal(x.Path, y.Path)); |
72 |
} |
73 |
else |
74 |
nodes.Add(newNode); |
75 |
|
76 |
} |
77 |
return nodes; |
78 |
} |
79 |
|
80 |
public static List<DirectoryRecord> ToTree(this IEnumerable<ObjectInfo> enumerable) |
81 |
{ |
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) |
86 |
{ |
87 |
var path = item.Uri.ToString(); |
88 |
var newNode = new DirectoryRecord{ DisplayName=item.Name.Split('/').Last(),ObjectInfo=item}; |
89 |
lookups[path] = newNode; |
90 |
|
91 |
var lastIndex = path.LastIndexOf("/", StringComparison.Ordinal); |
92 |
var upTo = lastIndex < 0 ? path.Length - 1 : lastIndex; |
93 |
var parentPath = path.Substring(0, upTo); |
94 |
|
95 |
DirectoryRecord parent; |
96 |
if (lookups.TryGetValue(parentPath, out parent)) |
97 |
{ |
98 |
parent.Directories.Add(newNode); |
99 |
parent.Directories.Sort((x,y)=>String.CompareOrdinal(x.Uri.ToString(), y.Uri.ToString())); |
100 |
} |
101 |
else |
102 |
nodes.Add(newNode); |
103 |
|
104 |
} |
105 |
return nodes; |
106 |
} |
107 |
} |
108 |
} |