Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Algorithms/Sorting/ILKeyMapper.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 9.7 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43
44namespace ILNumerics.Algorithms {
45    /// <summary>
46    /// Key mapper class, to be overriden for user defined classes to be sorted with bucket sort
47    /// </summary>
48    /// <typeparam name="ElementType">Type of elements. Elements are constructed out of any number of subelements</typeparam>
49    /// <typeparam name="SubelementType">Type of subelements</typeparam>
50    /// <remarks>This class can be extended to enable sorting (bucket sort) for arbitrary types. The elements of those types may be devidable into subelements.
51    /// <para>Examples of sortable classes:
52    /// <list>
53    /// <item>colors: number/type of subelements: 1/any (e.g. the color code). One should write a <![CDATA[ILKeyMapper<Color,int>]]>.</item>
54    /// <item>strings: number/type of subelements: arbitrary/char. Here a sample ILASCIKeyMapper implementation exists already. This implementation is the default implementation used for bucket sort via ILMath.sort().</item>
55    /// <item>trees: number/type of subelements: arbitrary/tree nodes. One should write a key mapper to map a node of a tree to a bucket number</item>
56    /// <item>...</item></list></para></remarks>
57    internal abstract class ILKeyMapper<ElementType,SubelementType> {
58        /// <summary>
59        /// Maps subelement types to bucket index
60        /// </summary>
61        /// <param name="inSubelement">Item</param>
62        /// <returns>Bucket index</returns>
63        public abstract int Map (SubelementType inSubelement);
64        /// <summary>
65        /// Map subelemt - provide fallback on error
66        /// </summary>
67        /// <param name="element">Element item</param>
68        /// <param name="position">Position of subelement in element item to be mapped</param>
69        /// <param name="fallback">If position is out of range, give back fallback</param>
70        /// <returns>Mapped bucket for subelement or fallback on error</returns>
71        public virtual int Map(ElementType element, int position, int fallback) {
72            if (SubelementsCount(element) > position)
73                return Map(GetSubelement(element,position));
74            else
75                return fallback;
76        }
77        /// <summary>
78        /// Count subelements in an element
79        /// </summary>
80        /// <param name="element"></param>
81        /// <returns></returns>
82        public abstract int SubelementsCount (ElementType element);
83        /// <summary>
84        /// Get subelement from element item
85        /// </summary>
86        /// <param name="element">Element item</param>
87        /// <param name="idx">Position of subitem in element</param>
88        /// <returns>Subitem referenced</returns>
89        public abstract SubelementType GetSubelement (ElementType element, int idx);
90       
91        private int m_numberOfKeys = 0;
92        /// <summary>
93        /// Maximum number of keys (different subitems)
94        /// </summary>
95        public int NumberOfKeys {
96            get {
97                return m_numberOfKeys;
98            }
99        }
100        /// <summary>
101        /// Construct key mapper
102        /// </summary>
103        /// <param name="NumberOfKeys">Maximm number of different subitems (keys)</param>
104        public ILKeyMapper (int NumberOfKeys) {
105            m_numberOfKeys = NumberOfKeys;
106        }
107    }
108    /// <summary>
109    /// Concrete implementation of a key mapper for strings
110    /// </summary>
111    /// <remarks>this class is the default key mapper, used for bucket sort on strings</remarks>
112    internal class ILASCIIKeyMapper : ILKeyMapper<string, char> {
113        /// <summary>
114        /// map subelement to bucket
115        /// </summary>
116        /// <param name="inSubelement">subelement to be mapped</param>
117        /// <returns>ASCII code of the subelement character</returns>
118        public override int  Map(char inSubelement) {
119            return (int)inSubelement;
120        }
121        /// <summary>
122        /// Map char out of string with fallback
123        /// </summary>
124        /// <param name="element">full string item</param>
125        /// <param name="position">position of character in string</param>
126        /// <param name="fallback">fallback bucket number, if position is out of range</param>
127        /// <returns>ASCII code for character specified, fallback on error</returns>
128        public override int Map(string element, int position, int fallback) {
129            if (element.Length > position)
130                return (int)element[position];
131            else
132                return fallback;
133        }
134        /// <summary>
135        /// give one char from string
136        /// </summary>
137        /// <param name="element">full string item</param>
138        /// <param name="idx">character position in string</param>
139        /// <returns>character in string</returns>
140        /// <exception cref="IndexOutOfRangeException"> if idx is not within element ranges</exception>
141        public override char  GetSubelement(string element, int idx) {
142             return element[idx];
143        }
144        /// <summary>
145        /// Count numer of characters in string
146        /// </summary>
147        /// <param name="element">element item</param>
148        /// <returns>number of characters in string - length of string</returns>
149        public override int SubelementsCount(string element) {
150            if (object.Equals(element,null))
151                return 0;
152            return element.Length;             
153        }
154        /// <summary>
155        /// construct ASCII key mapper for 256 buckets
156        /// </summary>
157        public ILASCIIKeyMapper() : base(256) {}
158    }
159    /// <summary>
160    /// Integer key mapper - sample implementation for bucket sort
161    /// </summary>
162    /// <remarks>This mapper may be used for sorting integers with bucketsort.
163    /// <para>The integers to be sorted must be positive and limited. It corresponds to the number of buckets to be created.</para>
164    /// <para>This implementation serves as a sample implementation for bucket sort. You should consider using quicksort instead, which is implemented for ILMath.sort()</para></remarks>
165    internal class ILIntLimitedKeyMapper : ILKeyMapper<int,int> {
166        /// <summary>
167        /// Gives subelement - i.e. the element itself
168        /// </summary>
169        /// <param name="element">element</param>
170        /// <param name="idx">(ignored)</param>
171        /// <returns>element</returns>
172        public override int GetSubelement(int element, int idx) {
173            return element;
174        }
175        /// <summary>
176        /// map element - ignoring position &amp; fallback
177        /// </summary>
178        /// <param name="element">integer element</param>
179        /// <param name="position">(ignored)</param>
180        /// <param name="fallback">(ignored)</param>
181        /// <returns>integer element</returns>
182        public override int Map(int element, int position, int fallback) {
183            return element;
184        }
185        /// <summary>
186        /// map (copy) subelement
187        /// </summary>
188        /// <param name="inSubelement">subelement</param>
189        /// <returns>subelement</returns>
190        public override int Map(int inSubelement) {
191            return inSubelement;
192        }
193        /// <summary>
194        /// number of subelements in an element (Here: always 1)
195        /// </summary>
196        /// <param name="element">element</param>
197        /// <returns>1</returns>
198        public override int SubelementsCount(int element) {
199            return 1;
200        }
201        /// <summary>
202        /// construct integer key mapper
203        /// </summary>
204        /// <param name="limit">maximum number of buckets to be used</param>
205        public ILIntLimitedKeyMapper (int limit) : base(limit) {}
206    }
207}
208
Note: See TracBrowser for help on using the repository browser.