~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/Collections.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Sharpen
 
2
{
 
3
        using System;
 
4
        using System.Collections;
 
5
        using System.Collections.Generic;
 
6
        using System.Collections.ObjectModel;
 
7
 
 
8
        internal static class Collections
 
9
        {
 
10
                public static bool AddAll<T> (ICollection<T> list, IEnumerable toAdd)
 
11
                {
 
12
                        foreach (T t in toAdd)
 
13
                                list.Add (t);
 
14
                        return true;
 
15
                }
 
16
 
 
17
                public static V Remove<K, V> (IDictionary<K, V> map, K toRemove) where K : class
 
18
                {
 
19
                        V local;
 
20
                        if (map.TryGetValue (toRemove, out local)) {
 
21
                                map.Remove (toRemove);
 
22
                                return local;
 
23
                        }
 
24
                        return default(V);
 
25
                }
 
26
 
 
27
                public static object[] ToArray (ArrayList list)
 
28
                {
 
29
                        return list.ToArray ();
 
30
                }
 
31
 
 
32
                public static T[] ToArray<T> (ICollection<T> list)
 
33
                {
 
34
                        T[] array = new T[list.Count];
 
35
                        list.CopyTo (array, 0);
 
36
                        return array;
 
37
                }
 
38
 
 
39
                public static U[] ToArray<T,U> (ICollection<T> list, U[] res) where T:U
 
40
                {
 
41
                        if (res.Length < list.Count)
 
42
                                res = new U [list.Count];
 
43
                        
 
44
                        int n = 0;
 
45
                        foreach (T t in list)
 
46
                                res [n++] = t;
 
47
                        
 
48
                        if (res.Length > list.Count)
 
49
                                res [list.Count] = default (T);
 
50
                        return res;
 
51
                }
 
52
                
 
53
                public static IDictionary<K,V> EmptyMap<K,V> ()
 
54
                {
 
55
                        return new Dictionary<K,V> ();
 
56
                }
 
57
 
 
58
                public static IList<T> EmptyList<T> ()
 
59
                {
 
60
                        return new T[0];
 
61
                }
 
62
 
 
63
                public static ICollection<T> EmptySet<T> ()
 
64
                {
 
65
                        return new T[0];
 
66
                }
 
67
 
 
68
                public static IList<T> NCopies<T> (int n, T elem)
 
69
                {
 
70
                        List<T> list = new List<T> (n);
 
71
                        while (n-- > 0) {
 
72
                                list.Add (elem);
 
73
                        }
 
74
                        return list;
 
75
                }
 
76
 
 
77
                public static void Reverse<T> (IList<T> list)
 
78
                {
 
79
                        int end = list.Count - 1;
 
80
                        int index = 0;
 
81
                        while (index < end) {
 
82
                                T tmp = list [index];
 
83
                                list [index] = list [end];
 
84
                                list [end] = tmp;
 
85
                                ++index;
 
86
                                --end;
 
87
                        }
 
88
                }
 
89
 
 
90
                public static ICollection<T> Singleton<T> (T item)
 
91
                {
 
92
                        List<T> list = new List<T> (1);
 
93
                        list.Add (item);
 
94
                        return list;
 
95
                }
 
96
 
 
97
                public static IList<T> SingletonList<T> (T item)
 
98
                {
 
99
                        List<T> list = new List<T> (1);
 
100
                        list.Add (item);
 
101
                        return list;
 
102
                }
 
103
 
 
104
                public static IList<T> SynchronizedList<T> (IList<T> list)
 
105
                {
 
106
                        return new Sharpen.SynchronizedList<T> (list);
 
107
                }
 
108
 
 
109
                public static ICollection<T> UnmodifiableCollection<T> (ICollection<T> list)
 
110
                {
 
111
                        return list;
 
112
                }
 
113
 
 
114
                public static IList<T> UnmodifiableList<T> (IList<T> list)
 
115
                {
 
116
                        return new ReadOnlyCollection<T> (list);
 
117
                }
 
118
 
 
119
                public static ICollection<T> UnmodifiableSet<T> (ICollection<T> list)
 
120
                {
 
121
                        return list;
 
122
                }
 
123
                
 
124
                public static IDictionary<K,V> UnmodifiableMap<K,V> (IDictionary<K,V> dict)
 
125
                {
 
126
                        return dict;
 
127
                }
 
128
        }
 
129
}