
The C5 Generic Collection Library for C# and CLI Version 1.1.0 of 2008-02-10 Niels Kokholm Peter Sestoft IT University Technical Report Series TR-2006-76 ISSN 1600–6100 January 2006 Copyright c 2006 Niels Kokholm Peter Sestoft IT University of Copenhagen All rights reserved. Reproduction of all or part of this work is permitted for educational or research use on condition that this copyright notice is included in any copy. ISSN 1600–6100 ISBN 87-7949-114-6 Copies may be obtained by contacting: IT University of Copenhagen Rued Langgaardsvej 7 DK-2300 Copenhagen S Denmark Telephone: +45 72 18 50 00 Telefax: +45 72 18 50 01 Web www.itu.dk Preface This book describes the C5 library of generic collection classes (or container classes) for the C# programming language and other generics-enabled languages on version 2.0 of the CLI platform, as implemented by Microsoft .NET and Mono. The C5 library provides a wide range of classic data structures, rich functionality, the best possible asymptotic time complexity, documented performance, and a thoroughly tested implementation. Goals of the C5 library The overall goal is for C5 to be a generic collection li- brary for the C# programming language [11, 17, 27] and the Common Language In- frastructure (CLI) [12] whose functionality, efficiency and quality meets or exceeds what is available for similar contemporary programming platforms. The design has been influenced by the collection libraries for Java and Smalltalk and the published critique of these. However, it contains functionality and a regularity of design that considerably exceeds that of the standard libraries for those languages. Why yet another generic collection library? There are already other generic collection libraries for C#/CLI, including the System.Collections.Generic namespace of the CLI or .NET Framework class library (included with Microsoft Visual Studio 2005), and the PowerCollections library developed by Peter Golde [15]. The CLI collection library as implemented by Microsoft .NET Framework 2.0 provides a limited choice of data structures. In general, the CLI Framework library has a proliferation of method variants and rather poor orthogonality. Collection implementations such as array lists and linked lists have much the same function- ality but do not implement a common interface. This impairs the learnability of the library. Some of these design decisions are well-motivated by the use of the CLI class library in contexts where nano-second efficiency is more important than rich functionality, and the need to support also rather resource-constrained run-time systems. The PowerCollections library by Peter Golde augments the CLI version 2.0 col- lection library with various data structures and algorithms. However, it accepts the basic design of the CLI collection classes and therefore suffers from some of the same shortcomings, and also does not provide most of the advanced functionality (updatable list views, snapshots, directed enumeration, priority queue handles, . ) 1 2 of C5. Thus, in our opinion, C5 provides the most powerful, well-structured and scal- able generic collections library available for C#/CLI. However, although the size of the compiled C5.dll is only about 300 KB, you probably would not choose to use it on your .NET 2.0 Compact Framework wristwatch just now. What does the name C5 stand for? This is not entirely clear, but it may stand for Copenhagen Comprehensive Collection Classes for C#, although the library may be used from VB.NET, F# [30] and other CLI languages, not just C#. It has nothing to do with a Microsoft Dynamics product that used to be called Concorde C5/Damgaard C5/Navision C5, nor a real-time operating system called C5 (or Chorus), nor the C5 Corporation (system visualization), nor an Eclipse plug-in called C5, nor with cars such as Citroën C5 or Corvette C5 or Clive Sinclair's ill-fated C5 concept vehicle. The name may be inspired by the versatile C4 plastic explosive known from e.g. James Bond movies. All trademarks belong to their owners. State of completion At the time of writing, library design and implementation are complete, and extensive unit tests have been written and applied systematically. Most of the library API documentation is in place but requires proof-reading. The C5 implementation uses most of the features of C# 2.0: generic types and methods, type parameter constraints, iterator blocks, anonymous methods, and nul- lable value types. It was developed using alpha, beta and final releases of Microsoft .NET 2.0, but uses only standard libraries and CLI features, and the library builds and passes the units test on the Mono 1.1.15 implementation on CLI. This book The present book is a guide to effective use of C5. It gives an overview of the library and its design rationale, describes the entire API in detail, including the time complexity of all operations, presents more than a hundred small usage patterns (idioms), and several larger, complete examples. Finally it explains some of the techniques used in the implementation. C5 availability and license The complete C5 library implementation, including documentation, is available in binary and source form from the IT University of Copenhagen: http://www.itu.dk/research/c5/ 3 The library is copyrighted by the authors and distributed under a BSD-style license: Copyright c 2003-2008 Niels Kokholm and Peter Sestoft Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRAN- TIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. So you can use and modify it for any purpose, including commerce, without a license fee, but the copyright notice must remain in place, and you cannot blame us or our employers for any consequences of using or abusing the library. The authors Niels Kokholm holds an MSc and a PhD in mathematics and an MSc in information technology and software development. He makes software for the insurance industry at Edlund A/S in Copenhagen. Peter Sestoft holds an MSc and a PhD in computer science, is a member of the Ecma C# and CLI standardization committees, and the author of C# Precisely [27] and Java Precisely (MIT Press). He is professor at the IT University of Copenhagen and the Royal Veterinary and Agricultural University (KVL), Denmark. Acknowledgements A visit to Microsoft Research, Cambridge UK, in late 2001 permitted Peter to write a first rudimentary generic collection library for C#, which was considerably redesigned, extended and improved by Niels during 2003–2004. We thank Microsoft Research University Relations for a grant that enabled us to complete, improve and document the implementation in 2004–2005. Thanks to Daniel Morton and Jon Jagger for comments on the design and imple- mentation of C5, and to the IT University of Copenhagen and the Royal Veterinary and Agricultural University (KVL), Denmark, for their support. 4 Notational conventions Symbol Use Type Section act action delegate Act<A1> 3.6.1 arr array T[ ] cmp comparer SCG.IComparer<T> 2.6 cq circular queue IQueue<T> 6.1 csn comparison delegate System.Comparison<T> 2.6 eqc equality comparer SCG.IEqualityComparer<T> 2.3 f function delegate Fun<A1,R> 3.6.2 h priority queue handle IPriorityQueueHandle<T> 1.4.12 i, j offset or index into collection int k key in dictionary K ks sequence of keys in dictionary SCG.IEnumerable<K> kv (key,value) pair, entry KeyValuePair<K,V> kvs sequence of (key,value) pairs 3.5 m, n, N count, length, or size int obj any object System.Object p predicate delegate Fun<T, bool> 3.6.2 rnd random number generator System.Random 3.8 T, U, K, V generic type parameter v value of key in dictionary V w, u view IList<T> 8.1 x, y collection item xs, ys item sequence, enumerable SCG.IEnumerable<T> The abbreviation SCG stands for System.Collections.Generic, that is, the CLI class library namespace for generic collection classes. Likewise, SC stands for the name- space System.Collections of non-generic collection classes. These namespaces are standardized as part of Ecma CLI [12]. Common example declaration header All program fragments and code examples shown in this book are supposed to be preceded by these declarations: using System; using C5; using SCG = System.Collections.Generic; Contents 1 Collection concepts 9 1.1 Getting started . 9 1.2 Design goals of C5 . 11 1.3 Overview of collection concepts . 12 1.4 The collection interfaces . 13 1.5 Dictionary interfaces . 19 1.6 Comparison with other collection libraries . 22 2 Equality and comparison 25 2.1 Natural equality . 26 2.2 Equatable types . 27 2.3 Equality comparers . 27 2.4 Creating equality comparers . 28 2.5 Comparable types . 30 2.6 Comparers . 31 2.7 Creating comparers . 32 3 Auxiliary types 35 3.1 Enum type EventTypeEnum .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages272 Page
-
File Size-