Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/LibSVM/SupportClass.cs @ 2864

Last change on this file since 2864 was 1819, checked in by mkommend, 16 years ago

created new project for LibSVM source files (ticket #619)

File size: 6.2 KB
Line 
1//
2// In order to convert some functionality to Visual C#, the Java Language Conversion Assistant
3// creates "support classes" that duplicate the original functionality. 
4//
5// Support classes replicate the functionality of the original code, but in some cases they are
6// substantially different architecturally. Although every effort is made to preserve the
7// original architecture of the application in the converted project, the user should be aware that
8// the primary goal of these support classes is to replicate functionality, and that at times
9// the architecture of the resulting solution may differ somewhat.
10//
11
12using System;
13
14/// <summary>
15/// Contains conversion support elements such as classes, interfaces and static methods.
16/// </summary>
17public class SupportClass
18{
19  //Provides access to a static System.Random class instance
20  static public System.Random Random = new System.Random();
21
22  /*******************************/
23  /// <summary>
24  /// The class performs token processing in strings
25  /// </summary>
26  public class Tokenizer: System.Collections.IEnumerator
27  {
28    /// Position over the string
29    private long currentPos = 0;
30
31    /// Include demiliters in the results.
32    private bool includeDelims = false;
33
34    /// Char representation of the String to tokenize.
35    private char[] chars = null;
36     
37    //The tokenizer uses the default delimiter set: the space character, the tab character, the newline character, and the carriage-return character and the form-feed character
38    private string delimiters = " \t\n\r\f";   
39
40    /// <summary>
41    /// Initializes a new class instance with a specified string to process
42    /// </summary>
43    /// <param name="source">String to tokenize</param>
44    public Tokenizer(System.String source)
45    {     
46      this.chars = source.ToCharArray();
47    }
48
49    /// <summary>
50    /// Initializes a new class instance with a specified string to process
51    /// and the specified token delimiters to use
52    /// </summary>
53    /// <param name="source">String to tokenize</param>
54    /// <param name="delimiters">String containing the delimiters</param>
55    public Tokenizer(System.String source, System.String delimiters):this(source)
56    {     
57      this.delimiters = delimiters;
58    }
59
60
61    /// <summary>
62    /// Initializes a new class instance with a specified string to process, the specified token
63    /// delimiters to use, and whether the delimiters must be included in the results.
64    /// </summary>
65    /// <param name="source">String to tokenize</param>
66    /// <param name="delimiters">String containing the delimiters</param>
67    /// <param name="includeDelims">Determines if delimiters are included in the results.</param>
68    public Tokenizer(System.String source, System.String delimiters, bool includeDelims):this(source,delimiters)
69    {
70      this.includeDelims = includeDelims;
71    }
72
73
74    /// <summary>
75    /// Returns the next token from the token list
76    /// </summary>
77    /// <returns>The string value of the token</returns>
78    public System.String NextToken()
79    {       
80      return NextToken(this.delimiters);
81    }
82
83    /// <summary>
84    /// Returns the next token from the source string, using the provided
85    /// token delimiters
86    /// </summary>
87    /// <param name="delimiters">String containing the delimiters to use</param>
88    /// <returns>The string value of the token</returns>
89    public System.String NextToken(System.String delimiters)
90    {
91      //According to documentation, the usage of the received delimiters should be temporary (only for this call).
92      //However, it seems it is not true, so the following line is necessary.
93      this.delimiters = delimiters;
94
95      //at the end
96      if (this.currentPos == this.chars.Length)
97        throw new System.ArgumentOutOfRangeException();
98      //if over a delimiter and delimiters must be returned
99      else if (   (System.Array.IndexOf(delimiters.ToCharArray(),chars[this.currentPos]) != -1)
100             && this.includeDelims )                 
101        return "" + this.chars[this.currentPos++];
102      //need to get the token wo delimiters.
103      else
104        return nextToken(delimiters.ToCharArray());
105    }
106
107    //Returns the nextToken wo delimiters
108    private System.String nextToken(char[] delimiters)
109    {
110      string token="";
111      long pos = this.currentPos;
112
113      //skip possible delimiters
114      while (System.Array.IndexOf(delimiters,this.chars[currentPos]) != -1)
115        //The last one is a delimiter (i.e there is no more tokens)
116        if (++this.currentPos == this.chars.Length)
117        {
118          this.currentPos = pos;
119          throw new System.ArgumentOutOfRangeException();
120        }
121     
122      //getting the token
123      while (System.Array.IndexOf(delimiters,this.chars[this.currentPos]) == -1)
124      {
125        token+=this.chars[this.currentPos];
126        //the last one is not a delimiter
127        if (++this.currentPos == this.chars.Length)
128          break;
129      }
130      return token;
131    }
132
133       
134    /// <summary>
135    /// Determines if there are more tokens to return from the source string
136    /// </summary>
137    /// <returns>True or false, depending if there are more tokens</returns>
138    public bool HasMoreTokens()
139    {
140      //keeping the current pos
141      long pos = this.currentPos;
142     
143      try
144      {
145        this.NextToken();
146      }
147      catch (System.ArgumentOutOfRangeException)
148      {       
149        return false;
150      }
151      finally
152      {
153        this.currentPos = pos;
154      }
155      return true;
156    }
157
158    /// <summary>
159    /// Remaining tokens count
160    /// </summary>
161    public int Count
162    {
163      get
164      {
165        //keeping the current pos
166        long pos = this.currentPos;
167        int i = 0;
168     
169        try
170        {
171          while (true)
172          {
173            this.NextToken();
174            i++;
175          }
176        }
177        catch (System.ArgumentOutOfRangeException)
178        {       
179          this.currentPos = pos;
180          return i;
181        }
182      }
183    }
184
185    /// <summary>
186    ///  Performs the same action as NextToken.
187    /// </summary>
188    public System.Object Current
189    {
190      get
191      {
192        return (Object) this.NextToken();
193      }   
194    }   
195   
196    /// <summary>
197    //  Performs the same action as HasMoreTokens.
198    /// </summary>
199    /// <returns>True or false, depending if there are more tokens</returns>
200    public bool MoveNext()
201    {
202      return this.HasMoreTokens();
203    }
204   
205    /// <summary>
206    /// Does nothing.
207    /// </summary>
208    public void  Reset()
209    {
210      ;
211    }     
212  }
213}
Note: See TracBrowser for help on using the repository browser.