opensim-libs /trunk/managed/csharp-sqlite/TCL/src/base/TclList.cs

Language C# Lines 450
MD5 Hash 8f67117aeaa8639e3feb0d9a659208ef Estimated Cost $5,326 (why?)
Repository https://bitbucket.org/KyanhaLLC/opensim-libs.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* TclList.java
*
* Copyright (c) 1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
* 
* Included in SQLite3 port to C# for use in testharness only;  2008 Noah B Hart
* $Header$
* RCS @(#) $Id: TclList.java,v 1.5 2003/01/09 02:15:39 mdejong Exp $
*
*/
using System;
using System.Collections;

namespace tcl.lang
{

  /// <summary> This class implements the list object type in Tcl.</summary>
  public class TclList : InternalRep
  {

    /// <summary> Internal representation of a list value.</summary>
    private ArrayList vector;

    /// <summary> Create a new empty Tcl List.</summary>
    private TclList()
    {
      vector = new ArrayList( 10 );
    }

    /// <summary> Create a new empty Tcl List, with the vector pre-allocated to
    /// the given size.
    /// 
    /// </summary>
    /// <param name="size">the number of slots pre-allocated in the vector.
    /// </param>
    private TclList( int size )
    {
      vector = new ArrayList( size );
    }

    /// <summary> Called to free any storage for the type's internal rep.</summary>
    /// <param name="obj">the TclObject that contains this internalRep.
    /// </param>
    public void dispose()
    {
      int size = vector.Count;
      for ( int i = 0 ; i < size ; i++ )
      {
        ( (TclObject)vector[i] ).release();
      }
    }

    /// <summary> DupListInternalRep -> duplicate
    /// 
    /// Returns a dupilcate of the current object.
    /// 
    /// </summary>
    /// <param name="obj">the TclObject that contains this internalRep.
    /// </param>
    public InternalRep duplicate()
    {
      int size = vector.Count;
      TclList newList = new TclList( size );

      for ( int i = 0 ; i < size ; i++ )
      {
        TclObject tobj = (TclObject)vector[i];
        tobj.preserve();
        newList.vector.Add( tobj );
      }

      return newList;
    }

    /// <summary> Called to query the string representation of the Tcl object. This
    /// method is called only by TclObject.toString() when
    /// TclObject.stringRep is null.
    /// 
    /// </summary>
    /// <returns> the string representation of the Tcl object.
    /// </returns>
    public override string ToString()
    {
      System.Text.StringBuilder sbuf = new System.Text.StringBuilder();
      int size = vector.Count;

      try
      {
        for ( int i = 0 ; i < size ; i++ )
        {
          Object elm = vector[i];
          if ( elm != null )
          {

            Util.appendElement( null, sbuf, elm.ToString() );
          }
          else
          {
            Util.appendElement( null, sbuf, "" );
          }
        }
      }
      catch ( TclException e )
      {
        throw new TclRuntimeError( "unexpected TclException: " + e.Message, e );
      }

      return sbuf.ToString();
    }

    /// <summary> Creates a new instance of a TclObject with a TclList internal
    /// rep.
    /// 
    /// </summary>
    /// <returns> the TclObject with the given list value.
    /// </returns>

    public static TclObject newInstance()
    {
      return new TclObject( new TclList() );
    }

    /// <summary> Called to convert the other object's internal rep to list.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to convert to use the List internal rep.
    /// </param>
    /// <exception cref=""> TclException if the object doesn't contain a valid list.
    /// </exception>
    internal static void setListFromAny( Interp interp, TclObject tobj )
    {
      InternalRep rep = tobj.InternalRep;

      if ( !( rep is TclList ) )
      {
        TclList tlist = new TclList();

        splitList( interp, tlist.vector, tobj.ToString() );
        tobj.InternalRep = tlist;
      }
    }

    /// <summary> Splits a list (in string rep) up into its constituent fields.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="v">store the list elements in this vector.
    /// </param>
    /// <param name="s">the string to convert into a list.
    /// </param>
    /// <exception cref=""> TclException if the object doesn't contain a valid list.
    /// </exception>
    private static void splitList( Interp interp, ArrayList v, string s )
    {
      int len = s.Length;
      int i = 0;

      while ( i < len )
      {
        FindElemResult res = Util.findElement( interp, s, i, len );
        if ( res == null )
        {
          break;
        }
        else
        {
          TclObject tobj = TclString.newInstance( res.elem );
          tobj.preserve();
          v.Add( tobj );
        }
        i = res.elemEnd;
      }
    }


    /// <summary> Tcl_ListObjAppendElement -> TclList.append()
    /// 
    /// Appends a TclObject element to a list object.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to append an element to.
    /// </param>
    /// <param name="elemObj">the element to append to the object.
    /// </param>
    /// <exception cref=""> TclException if tobj cannot be converted into a list.
    /// </exception>
    public static void append( Interp interp, TclObject tobj, TclObject elemObj )
    {
      if ( tobj.Shared )
      {
        throw new TclRuntimeError( "TclList.append() called with shared object" );
      }
      setListFromAny( interp, tobj );
      tobj.invalidateStringRep();

      TclList tlist = (TclList)tobj.InternalRep;

      if ( !String.IsNullOrEmpty( elemObj.stringRep ) && elemObj.stringRep[0] == '{' ) elemObj = TclString.newInstance( elemObj.stringRep.Substring( 1, elemObj.stringRep.Length - 2 ) );
      elemObj.preserve();
      tlist.vector.Add( elemObj );
    }

    /// <summary> Queries the length of the list. If tobj is not a list object,
    /// an attempt will be made to convert it to a list.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to use as a list.
    /// </param>
    /// <returns> the length of the list.
    /// </returns>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>
    public static int getLength( Interp interp, TclObject tobj )
    {
      setListFromAny( interp, tobj );

      TclList tlist = (TclList)tobj.InternalRep;
      return tlist.vector.Count;
    }

    /// <summary> Returns a TclObject array of the elements in a list object.  If
    /// tobj is not a list object, an attempt will be made to convert
    /// it to a list. <p>
    /// 
    /// The objects referenced by the returned array should be treated
    /// as readonly and their ref counts are _not_ incremented; the
    /// caller must do that if it holds on to a reference.
    /// 
    /// </summary>
    /// <param name="interp">the current interpreter.
    /// </param>
    /// <param name="tobj">the list to sort.
    /// </param>
    /// <returns> a TclObject array of the elements in a list object.
    /// </returns>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>
    public static TclObject[] getElements( Interp interp, TclObject tobj )
    {
      setListFromAny( interp, tobj );
      TclList tlist = (TclList)tobj.InternalRep;

      int size = tlist.vector.Count;
      TclObject[] objArray = new TclObject[size];
      for ( int i = 0 ; i < size ; i++ )
      {
        objArray[i] = (TclObject)tlist.vector[i];
      }
      return objArray;
    }

    /// <summary> This procedure returns a pointer to the index'th object from
    /// the list referenced by tobj. The first element has index
    /// 0. If index is negative or greater than or equal to the number
    /// of elements in the list, a null is returned. If tobj is not a
    /// list object, an attempt will be made to convert it to a list.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to use as a list.
    /// </param>
    /// <param name="index">the index of the requested element.
    /// </param>
    /// <returns> the the requested element.
    /// </returns>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>
    public static TclObject index( Interp interp, TclObject tobj, int index )
    {
      setListFromAny( interp, tobj );

      TclList tlist = (TclList)tobj.InternalRep;
      if ( index < 0 || index >= tlist.vector.Count )
      {
        return null;
      }
      else
      {
        return (TclObject)tlist.vector[index];
      }
    }

    /// <summary> This procedure inserts the elements in elements[] into the list at
    /// the given index. If tobj is not a list object, an attempt will
    /// be made to convert it to a list.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to use as a list.
    /// </param>
    /// <param name="index">the starting index of the insertion operation. <=0 means
    /// the beginning of the list. >= TclList.getLength(tobj) means
    /// the end of the list.
    /// </param>
    /// <param name="elements">the element(s) to insert.
    /// </param>
    /// <param name="from">insert elements starting from elements[from] (inclusive)
    /// </param>
    /// <param name="to">insert elements up to elements[to] (inclusive)
    /// </param>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>
    internal static void insert( Interp interp, TclObject tobj, int index, TclObject[] elements, int from, int to )
    {
      if ( tobj.Shared )
      {
        throw new TclRuntimeError( "TclList.insert() called with shared object" );
      }
      replace( interp, tobj, index, 0, elements, from, to );
    }

    /// <summary> This procedure replaces zero or more elements of the list
    /// referenced by tobj with the objects from an TclObject array.
    /// If tobj is not a list object, an attempt will be made to
    /// convert it to a list.
    /// 
    /// </summary>
    /// <param name="interp">current interpreter.
    /// </param>
    /// <param name="tobj">the TclObject to use as a list.
    /// </param>
    /// <param name="index">the starting index of the replace operation. <=0 means
    /// the beginning of the list. >= TclList.getLength(tobj) means
    /// the end of the list.
    /// </param>
    /// <param name="count">the number of elements to delete from the list. <=0 means
    /// no elements should be deleted and the operation is equivalent to
    /// an insertion operation.
    /// </param>
    /// <param name="elements">the element(s) to insert.
    /// </param>
    /// <param name="from">insert elements starting from elements[from] (inclusive)
    /// </param>
    /// <param name="to">insert elements up to elements[to] (inclusive)
    /// </param>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>
    public static void replace( Interp interp, TclObject tobj, int index, int count, TclObject[] elements, int from, int to )
    {
      if ( tobj.Shared )
      {
        throw new TclRuntimeError( "TclList.replace() called with shared object" );
      }
      setListFromAny( interp, tobj );
      tobj.invalidateStringRep();
      TclList tlist = (TclList)tobj.InternalRep;

      int size = tlist.vector.Count;
      int i;

      if ( index >= size )
      {
        // Append to the end of the list. There is no need for deleting
        // elements.
        index = size;
      }
      else
      {
        if ( index < 0 )
        {
          index = 0;
        }
        if ( count > size - index )
        {
          count = size - index;
        }
        for ( i = 0 ; i < count ; i++ )
        {
          TclObject obj = (TclObject)tlist.vector[index];
          obj.release();
          tlist.vector.RemoveAt( index );
        }
      }
      for ( i = from ; i <= to ; i++ )
      {
        elements[i].preserve();
        tlist.vector.Insert( index++, elements[i] );
      }
    }

    /// <summary> Sorts the list according to the sort mode and (optional) sort command.
    /// The resulting list will contain no duplicates, if argument unique is
    /// specifed as true.
    /// If tobj is not a list object, an attempt will be made to
    /// convert it to a list.
    /// 
    /// </summary>
    /// <param name="interp">the current interpreter.
    /// </param>
    /// <param name="tobj">the list to sort.
    /// </param>
    /// <param name="sortMode">the sorting mode.
    /// </param>
    /// <param name="sortIncreasing">true if to sort the elements in increasing order.
    /// </param>
    /// <param name="command">the command to compute the order of two elements.
    /// </param>
    /// <param name="unique">true if the result should contain no duplicates.
    /// </param>
    /// <exception cref=""> TclException if tobj is not a valid list.
    /// </exception>

    internal static void sort( Interp interp, TclObject tobj, int sortMode, int sortIndex, bool sortIncreasing, string command, bool unique )
    {
      setListFromAny( interp, tobj );
      tobj.invalidateStringRep();
      TclList tlist = (TclList)tobj.InternalRep;

      int size = tlist.vector.Count;

      if ( size <= 1 )
      {
        return;
      }

      TclObject[] objArray = new TclObject[size];
      for ( int i = 0 ; i < size ; i++ )
      {
        objArray[i] = (TclObject)tlist.vector[i];
      }

      QSort s = new QSort();
      int newsize = s.sort( interp, objArray, sortMode, sortIndex, sortIncreasing, command, unique );

      for ( int i = 0 ; i < size   ; i++ )
      {
        if ( i < newsize )
        {
          tlist.vector[i] = objArray[i];
          objArray[i] = null;
        }
        else tlist.vector.RemoveAt( newsize  );
      }
    }
  }
}
Back to Top