Bringing C5 1.0 into the main branch.
[mono.git] / mcs / class / Mono.C5 / current / UserGuideExamples / Fileindex.cs
1 /*\r
2  Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft\r
3  Permission is hereby granted, free of charge, to any person obtaining a copy\r
4  of this software and associated documentation files (the "Software"), to deal\r
5  in the Software without restriction, including without limitation the rights\r
6  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
7  copies of the Software, and to permit persons to whom the Software is\r
8  furnished to do so, subject to the following conditions:\r
9  \r
10  The above copyright notice and this permission notice shall be included in\r
11  all copies or substantial portions of the Software.\r
12  \r
13  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
14  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
15  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
16  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
17  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
18  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
19  SOFTWARE.\r
20 */\r
21 \r
22 // C5 example: File index: read a text file, build and print a list of\r
23 // words and the line numbers (without duplicates) on which they occur.\r
24 \r
25 // Compile with \r
26 //   csc /r:C5.dll Fileindex.cs \r
27 \r
28 using System;                           // Console\r
29 using System.IO;                        // StreamReader, TextReader\r
30 using System.Text.RegularExpressions;   // Regex\r
31 using C5;                               // IDictionary, TreeDictionary, TreeSet\r
32 \r
33 namespace FileIndex\r
34 {\r
35   class Fileindex\r
36   {\r
37     static void Main(String[] args)\r
38     {\r
39       if (args.Length != 1)\r
40         Console.WriteLine("Usage: Fileindex <filename>\n");\r
41       else\r
42       {\r
43         IDictionary<String, TreeSet<int>> index = IndexFile(args[0]);\r
44         PrintIndex(index);\r
45       }\r
46     }\r
47 \r
48     static IDictionary<String, TreeSet<int>> IndexFile(String filename)\r
49     {\r
50       IDictionary<String, TreeSet<int>> index = new TreeDictionary<String, TreeSet<int>>();\r
51       Regex delim = new Regex("[^a-zA-Z0-9]+");\r
52       using (TextReader rd = new StreamReader(filename))\r
53       {\r
54         int lineno = 0;\r
55         for (String line = rd.ReadLine(); line != null; line = rd.ReadLine())\r
56         {\r
57           String[] res = delim.Split(line);\r
58           lineno++;\r
59           foreach (String s in res)\r
60             if (s != "")\r
61             {\r
62               if (!index.Contains(s))\r
63                 index[s] = new TreeSet<int>();\r
64               index[s].Add(lineno);\r
65             }\r
66         }\r
67       }\r
68       return index;\r
69     }\r
70 \r
71     static void PrintIndex(IDictionary<String, TreeSet<int>> index)\r
72     {\r
73       foreach (String word in index.Keys)\r
74       {\r
75         Console.Write("{0}: ", word);\r
76         foreach (int ln in index[word])\r
77           Console.Write("{0} ", ln);\r
78         Console.WriteLine();\r
79       }\r
80     }\r
81   }\r
82 }