source: trunk/CrypPlugins/WorkspaceManager/View/VisualComponents/CryptoLineView/PowerCollections/Util.cs @ 1927

Last change on this file since 1927 was 1927, checked in by matkovic, 11 years ago

-added PowerCollection library
-added QuadTree
-improved path finding performance

File size: 3.8 KB
Line 
1//******************************
2// Written by Peter Golde
3// Copyright (c) 2004-2007, Wintellect
4//
5// Use and restribution of this code is subject to the license agreement
6// contained in the file "License.txt" accompanying this file.
7//******************************
8
9using System;
10using System.Collections;
11using System.Collections.Generic;
12
13namespace Wintellect.PowerCollections
14{
15        /// <summary>
16        /// A holder class for various internal utility functions that need to be shared.
17        /// </summary>
18    internal static class Util
19    {
20        /// <summary>
21        /// Determine if a type is cloneable: either a value type or implementing
22        /// ICloneable.
23        /// </summary>
24        /// <param name="type">Type to check.</param>
25        /// <param name="isValue">Returns if the type is a value type, and does not implement ICloneable.</param>
26        /// <returns>True if the type is cloneable.</returns>
27        public static bool IsCloneableType(Type type, out bool isValue)
28        {
29            isValue = false;
30
31            if (typeof(ICloneable).IsAssignableFrom(type)) {
32                return true;
33            }
34            else if (type.IsValueType) {
35                isValue = true;
36                return true;
37            }
38            else
39                return false;
40        }
41
42        /// <summary>
43        /// Returns the simple name of the class, for use in exception messages.
44        /// </summary>
45        /// <returns>The simple name of this class.</returns>
46        public static string SimpleClassName(Type type)
47        {
48            string name = type.Name;
49
50            // Just use the simple name.
51            int index = name.IndexOfAny(new char[] { '<', '{', '`' });
52            if (index >= 0)
53                name = name.Substring(0, index);
54
55            return name;
56        }
57
58        /// <summary>
59        /// Wrap an enumerable so that clients can't get to the underlying
60        /// implementation via a down-cast.
61        /// </summary>
62        [Serializable]
63        class WrapEnumerable<T> : IEnumerable<T>
64        {
65            IEnumerable<T> wrapped;
66
67            /// <summary>
68            /// Create the wrapper around an enumerable.
69            /// </summary>
70            /// <param name="wrapped">IEnumerable to wrap.</param>
71            public WrapEnumerable(IEnumerable<T> wrapped)
72            {
73                this.wrapped = wrapped;
74            }
75
76            public IEnumerator<T> GetEnumerator()
77            {
78                return wrapped.GetEnumerator();
79            }
80
81            IEnumerator IEnumerable.GetEnumerator()
82            {
83                return ((IEnumerable)wrapped).GetEnumerator();
84            }
85        }
86
87        /// <summary>
88        /// Wrap an enumerable so that clients can't get to the underlying
89        /// implementation via a down-case
90        /// </summary>
91        /// <param name="wrapped">Enumerable to wrap.</param>
92        /// <returns>A wrapper around the enumerable.</returns>
93        public static IEnumerable<T> CreateEnumerableWrapper<T>(IEnumerable<T> wrapped)
94        {
95            return new WrapEnumerable<T>(wrapped);
96        }
97
98        /// <summary>
99        /// Gets the hash code for an object using a comparer. Correctly handles
100        /// null.
101        /// </summary>
102        /// <param name="item">Item to get hash code for. Can be null.</param>
103        /// <param name="equalityComparer">The comparer to use.</param>
104        /// <returns>The hash code for the item.</returns>
105        public static int GetHashCode<T>(T item, IEqualityComparer<T> equalityComparer)
106        {
107            if (item == null)
108                return 0x1786E23C;
109            else
110                return equalityComparer.GetHashCode(item);
111        }
112    }
113}
Note: See TracBrowser for help on using the repository browser.