aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/LSL_Types.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/LSL_Types.cs1834
1 files changed, 0 insertions, 1834 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs
deleted file mode 100644
index 21646ea..0000000
--- a/OpenSim/Region/ScriptEngine/Common/LSL_Types.cs
+++ /dev/null
@@ -1,1834 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections;
30using System.Text.RegularExpressions;
31
32namespace OpenSim.Region.ScriptEngine.Common
33{
34 [Serializable]
35 public partial class LSL_Types
36 {
37 // Types are kept is separate .dll to avoid having to add whatever .dll it is in it to script AppDomain
38
39 [Serializable]
40 public struct Vector3
41 {
42 public double x;
43 public double y;
44 public double z;
45
46 #region Constructors
47
48 public Vector3(Vector3 vector)
49 {
50 x = (float)vector.x;
51 y = (float)vector.y;
52 z = (float)vector.z;
53 }
54
55 public Vector3(double X, double Y, double Z)
56 {
57 x = X;
58 y = Y;
59 z = Z;
60 }
61
62 public Vector3(string str)
63 {
64 str = str.Replace('<', ' ');
65 str = str.Replace('>', ' ');
66 string[] tmps = str.Split(new Char[] { ',', '<', '>' });
67 if (tmps.Length < 3)
68 {
69 x=y=z=0;
70 return;
71 }
72 bool res;
73 res = Double.TryParse(tmps[0], out x);
74 res = res & Double.TryParse(tmps[1], out y);
75 res = res & Double.TryParse(tmps[2], out z);
76 }
77
78 #endregion
79
80 #region Overriders
81
82 public override string ToString()
83 {
84 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", x, y, z);
85 return s;
86 }
87
88 public static explicit operator LSLString(Vector3 vec)
89 {
90 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
91 return new LSLString(s);
92 }
93
94 public static explicit operator string(Vector3 vec)
95 {
96 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
97 return s;
98 }
99
100 public static explicit operator Vector3(string s)
101 {
102 return new Vector3(s);
103 }
104
105 public static bool operator ==(Vector3 lhs, Vector3 rhs)
106 {
107 return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
108 }
109
110 public static bool operator !=(Vector3 lhs, Vector3 rhs)
111 {
112 return !(lhs == rhs);
113 }
114
115 public override int GetHashCode()
116 {
117 return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode());
118 }
119
120 public override bool Equals(object o)
121 {
122 if (!(o is Vector3)) return false;
123
124 Vector3 vector = (Vector3)o;
125
126 return (x == vector.x && y == vector.y && z == vector.z);
127 }
128
129 public static Vector3 operator -(Vector3 vector)
130 {
131 return new Vector3(-vector.x, -vector.y, -vector.z);
132 }
133
134 #endregion
135
136 #region Vector & Vector Math
137
138 // Vector-Vector Math
139 public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
140 {
141 return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
142 }
143
144 public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
145 {
146 return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
147 }
148
149 public static LSLFloat operator *(Vector3 lhs, Vector3 rhs)
150 {
151 return Dot(lhs, rhs);
152 }
153
154 public static Vector3 operator %(Vector3 v1, Vector3 v2)
155 {
156 //Cross product
157 Vector3 tv;
158 tv.x = (v1.y * v2.z) - (v1.z * v2.y);
159 tv.y = (v1.z * v2.x) - (v1.x * v2.z);
160 tv.z = (v1.x * v2.y) - (v1.y * v2.x);
161 return tv;
162 }
163
164 #endregion
165
166 #region Vector & Float Math
167
168 // Vector-Float and Float-Vector Math
169 public static Vector3 operator *(Vector3 vec, float val)
170 {
171 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
172 }
173
174 public static Vector3 operator *(float val, Vector3 vec)
175 {
176 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
177 }
178
179 public static Vector3 operator /(Vector3 v, float f)
180 {
181 v.x = v.x / f;
182 v.y = v.y / f;
183 v.z = v.z / f;
184 return v;
185 }
186
187 #endregion
188
189 #region Vector & Double Math
190
191 public static Vector3 operator *(Vector3 vec, double val)
192 {
193 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
194 }
195
196 public static Vector3 operator *(double val, Vector3 vec)
197 {
198 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
199 }
200
201 public static Vector3 operator /(Vector3 v, double f)
202 {
203 v.x = v.x / f;
204 v.y = v.y / f;
205 v.z = v.z / f;
206 return v;
207 }
208
209 #endregion
210
211 #region Vector & Rotation Math
212
213 // Vector-Rotation Math
214 public static Vector3 operator *(Vector3 v, Quaternion r)
215 {
216 Quaternion vq = new Quaternion(v.x, v.y, v.z, 0);
217 Quaternion nq = new Quaternion(-r.x, -r.y, -r.z, r.s);
218
219 // adapted for operator * computing "b * a"
220 Quaternion result = nq * (vq * r);
221
222 return new Vector3(result.x, result.y, result.z);
223 }
224
225 public static Vector3 operator /(Vector3 v, Quaternion r)
226 {
227 r.s = -r.s;
228 return v * r;
229 }
230
231 #endregion
232
233 #region Static Helper Functions
234
235 public static double Dot(Vector3 v1, Vector3 v2)
236 {
237 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
238 }
239
240 public static Vector3 Cross(Vector3 v1, Vector3 v2)
241 {
242 return new Vector3
243 (
244 v1.y * v2.z - v1.z * v2.y,
245 v1.z * v2.x - v1.x * v2.z,
246 v1.x * v2.y - v1.y * v2.x
247 );
248 }
249
250 public static double Mag(Vector3 v)
251 {
252 return Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
253 }
254
255 public static Vector3 Norm(Vector3 vector)
256 {
257 double mag = Mag(vector);
258 return new Vector3(vector.x / mag, vector.y / mag, vector.z / mag);
259 }
260
261 #endregion
262 }
263
264 [Serializable]
265 public struct Quaternion
266 {
267 public double x;
268 public double y;
269 public double z;
270 public double s;
271
272 #region Constructors
273
274 public Quaternion(Quaternion Quat)
275 {
276 x = (float)Quat.x;
277 y = (float)Quat.y;
278 z = (float)Quat.z;
279 s = (float)Quat.s;
280 if (x == 0 && y == 0 && z == 0 && s == 0)
281 s = 1;
282 }
283
284 public Quaternion(double X, double Y, double Z, double S)
285 {
286 x = X;
287 y = Y;
288 z = Z;
289 s = S;
290 if (x == 0 && y == 0 && z == 0 && s == 0)
291 s = 1;
292 }
293
294 public Quaternion(string str)
295 {
296 str = str.Replace('<', ' ');
297 str = str.Replace('>', ' ');
298 string[] tmps = str.Split(new Char[] { ',', '<', '>' });
299 if (tmps.Length < 4)
300 {
301 x=y=z=s=0;
302 return;
303 }
304 bool res;
305 res = Double.TryParse(tmps[0], out x);
306 res = res & Double.TryParse(tmps[1], out y);
307 res = res & Double.TryParse(tmps[2], out z);
308 res = res & Double.TryParse(tmps[3], out s);
309 if (x == 0 && y == 0 && z == 0 && s == 0)
310 s = 1;
311 }
312
313 #endregion
314
315 #region Overriders
316
317 public override int GetHashCode()
318 {
319 return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
320 }
321
322 public override bool Equals(object o)
323 {
324 if (!(o is Quaternion)) return false;
325
326 Quaternion quaternion = (Quaternion)o;
327
328 return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
329 }
330
331 public override string ToString()
332 {
333 string st=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", x, y, z, s);
334 return st;
335 }
336
337 public static explicit operator string(Quaternion r)
338 {
339 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
340 return s;
341 }
342
343 public static explicit operator LSLString(Quaternion r)
344 {
345 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
346 return new LSLString(s);
347 }
348
349 public static explicit operator Quaternion(string s)
350 {
351 return new Quaternion(s);
352 }
353
354 public static bool operator ==(Quaternion lhs, Quaternion rhs)
355 {
356 // Return true if the fields match:
357 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
358 }
359
360 public static bool operator !=(Quaternion lhs, Quaternion rhs)
361 {
362 return !(lhs == rhs);
363 }
364
365 #endregion
366
367 public static Quaternion operator +(Quaternion a, Quaternion b)
368 {
369 return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
370 }
371
372 public static Quaternion operator /(Quaternion a, Quaternion b)
373 {
374 b.s = -b.s;
375 return a * b;
376 }
377
378 public static Quaternion operator -(Quaternion a, Quaternion b)
379 {
380 return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
381 }
382
383 // using the equations below, we need to do "b * a" to be compatible with LSL
384 public static Quaternion operator *(Quaternion b, Quaternion a)
385 {
386 Quaternion c;
387 c.x = a.s * b.x + a.x * b.s + a.y * b.z - a.z * b.y;
388 c.y = a.s * b.y + a.y * b.s + a.z * b.x - a.x * b.z;
389 c.z = a.s * b.z + a.z * b.s + a.x * b.y - a.y * b.x;
390 c.s = a.s * b.s - a.x * b.x - a.y * b.y - a.z * b.z;
391 return c;
392 }
393 }
394
395 [Serializable]
396 public class list
397 {
398 private object[] m_data;
399
400 public list(params object[] args)
401 {
402 m_data = new object[args.Length];
403 m_data = args;
404 }
405
406 public int Length
407 {
408 get {
409 if (m_data == null)
410 m_data=new Object[0];
411 return m_data.Length;
412 }
413 }
414
415 public object[] Data
416 {
417 get {
418 if (m_data == null)
419 m_data=new Object[0];
420 return m_data;
421 }
422
423 set {m_data = value; }
424 }
425
426 // Member functions to obtain item as specific types.
427 // For cases where implicit conversions would apply if items
428 // were not in a list (e.g. integer to float, but not float
429 // to integer) functions check for alternate types so as to
430 // down-cast from Object to the correct type.
431 // Note: no checks for item index being valid are performed
432
433 public LSL_Types.LSLFloat GetLSLFloatItem( int itemIndex )
434 {
435 if (m_data[itemIndex] is LSL_Types.LSLInteger)
436 {
437 return (LSL_Types.LSLInteger)m_data[itemIndex];
438 }
439 else if (m_data[itemIndex] is Int32)
440 {
441 return new LSL_Types.LSLFloat((int)m_data[itemIndex]);
442 }
443 else if (m_data[itemIndex] is float)
444 {
445 return new LSL_Types.LSLFloat((float)m_data[itemIndex]);
446 }
447 else if (m_data[itemIndex] is Double)
448 {
449 return new LSL_Types.LSLFloat((Double)m_data[itemIndex]);
450 }
451 else
452 {
453 return (LSL_Types.LSLFloat)m_data[itemIndex];
454 }
455 }
456
457 public LSL_Types.LSLString GetLSLStringItem(int itemIndex)
458 {
459 if (m_data[itemIndex] is LSL_Types.key)
460 {
461 return (LSL_Types.key)m_data[itemIndex];
462 }
463 else if (m_data[itemIndex] is String)
464 {
465 return new LSL_Types.LSLString((string)m_data[itemIndex]);
466 }
467 else
468 {
469 return (LSL_Types.LSLString)m_data[itemIndex];
470 }
471 }
472
473 public LSL_Types.LSLInteger GetLSLIntegerItem(int itemIndex)
474 {
475 if (m_data[itemIndex] is LSL_Types.LSLInteger)
476 return (LSL_Types.LSLInteger)m_data[itemIndex];
477 else if (m_data[itemIndex] is Int32)
478 return new LSLInteger((int)m_data[itemIndex]);
479 else
480 throw new InvalidCastException();
481 }
482
483 public LSL_Types.Vector3 GetVector3Item(int itemIndex)
484 {
485 return (LSL_Types.Vector3)m_data[itemIndex];
486 }
487
488 public LSL_Types.Quaternion GetQuaternionItem(int itemIndex)
489 {
490 return (LSL_Types.Quaternion)m_data[itemIndex];
491 }
492
493 public LSL_Types.key GetKeyItem(int itemIndex)
494 {
495 return (LSL_Types.key)m_data[itemIndex];
496 }
497
498 public static list operator +(list a, list b)
499 {
500 object[] tmp;
501 tmp = new object[a.Length + b.Length];
502 a.Data.CopyTo(tmp, 0);
503 b.Data.CopyTo(tmp, a.Length);
504 return new list(tmp);
505 }
506
507 private void ExtendAndAdd(object o)
508 {
509 Array.Resize(ref m_data, Length + 1);
510 m_data.SetValue(o, Length - 1);
511 }
512
513 public static list operator +(list a, LSLString s)
514 {
515 a.ExtendAndAdd(s);
516 return a;
517 }
518
519 public static list operator +(list a, LSLInteger i)
520 {
521 a.ExtendAndAdd(i);
522 return a;
523 }
524
525 public static list operator +(list a, LSLFloat d)
526 {
527 a.ExtendAndAdd(d);
528 return a;
529 }
530
531 public void Add(object o)
532 {
533 object[] tmp;
534 tmp = new object[m_data.Length + 1];
535 m_data.CopyTo(tmp, 0);
536 tmp[m_data.Length] = o;
537 m_data = tmp;
538 }
539
540 public bool Contains(object o)
541 {
542 bool ret = false;
543 foreach (object i in Data)
544 {
545 if (i == o)
546 {
547 ret = true;
548 break;
549 }
550 }
551 return ret;
552 }
553
554 public list DeleteSublist(int start, int end)
555 {
556 // Not an easy one
557 // If start <= end, remove that part
558 // if either is negative, count from the end of the array
559 // if the resulting start > end, remove all BUT that part
560
561 Object[] ret;
562
563 if (start < 0)
564 start=m_data.Length-start;
565
566 if (start < 0)
567 start=0;
568
569 if (end < 0)
570 end=m_data.Length-end;
571 if (end < 0)
572 end=0;
573
574 if (start > end)
575 {
576 if (end >= m_data.Length)
577 return new list(new Object[0]);
578
579 if (start >= m_data.Length)
580 start=m_data.Length-1;
581
582 return GetSublist(end, start);
583 }
584
585 // start >= 0 && end >= 0 here
586 if (start >= m_data.Length)
587 {
588 ret=new Object[m_data.Length];
589 Array.Copy(m_data, 0, ret, 0, m_data.Length);
590
591 return new list(ret);
592 }
593
594 if (end >= m_data.Length)
595 end=m_data.Length-1;
596
597 // now, this makes the math easier
598 int remove=end+1-start;
599
600 ret=new Object[m_data.Length-remove];
601 if (ret.Length == 0)
602 return new list(ret);
603
604 int src;
605 int dest=0;
606
607 for (src = 0; src < m_data.Length; src++)
608 {
609 if (src < start || src > end)
610 ret[dest++]=m_data[src];
611 }
612
613 return new list(ret);
614 }
615
616 public list GetSublist(int start, int end)
617 {
618
619 object[] ret;
620
621 // Take care of neg start or end's
622 // NOTE that either index may still be negative after
623 // adding the length, so we must take additional
624 // measures to protect against this. Note also that
625 // after normalisation the negative indices are no
626 // longer relative to the end of the list.
627
628 if (start < 0)
629 {
630 start = m_data.Length + start;
631 }
632
633 if (end < 0)
634 {
635 end = m_data.Length + end;
636 }
637
638 // The conventional case is start <= end
639 // NOTE that the case of an empty list is
640 // dealt with by the initial test. Start
641 // less than end is taken to be the most
642 // common case.
643
644 if (start <= end)
645 {
646
647 // Start sublist beyond length
648 // Also deals with start AND end still negative
649 if (start >= m_data.Length || end < 0)
650 {
651 return new list();
652 }
653
654 // Sublist extends beyond the end of the supplied list
655 if (end >= m_data.Length)
656 {
657 end = m_data.Length - 1;
658 }
659
660 // Sublist still starts before the beginning of the list
661 if (start < 0)
662 {
663 start = 0;
664 }
665
666 ret = new object[end - start + 1];
667
668 Array.Copy(m_data, start, ret, 0, end - start + 1);
669
670 return new list(ret);
671
672 }
673
674 // Deal with the segmented case: 0->end + start->EOL
675
676 else
677 {
678
679 list result = null;
680
681 // If end is negative, then prefix list is empty
682 if (end < 0)
683 {
684 result = new list();
685 // If start is still negative, then the whole of
686 // the existing list is returned. This case is
687 // only admitted if end is also still negative.
688 if (start < 0)
689 {
690 return this;
691 }
692
693 }
694 else
695 {
696 result = GetSublist(0,end);
697 }
698
699 // If start is outside of list, then just return
700 // the prefix, whatever it is.
701 if (start >= m_data.Length)
702 {
703 return result;
704 }
705
706 return result + GetSublist(start, Data.Length);
707
708 }
709 }
710
711 private class AlphanumComparatorFast : IComparer
712 {
713 public int Compare(object x, object y)
714 {
715 string s1 = x as string;
716 if (s1 == null)
717 {
718 return 0;
719 }
720 string s2 = y as string;
721 if (s2 == null)
722 {
723 return 0;
724 }
725
726 int len1 = s1.Length;
727 int len2 = s2.Length;
728 int marker1 = 0;
729 int marker2 = 0;
730
731 // Walk through two the strings with two markers.
732 while (marker1 < len1 && marker2 < len2)
733 {
734 char ch1 = s1[marker1];
735 char ch2 = s2[marker2];
736
737 // Some buffers we can build up characters in for each chunk.
738 char[] space1 = new char[len1];
739 int loc1 = 0;
740 char[] space2 = new char[len2];
741 int loc2 = 0;
742
743 // Walk through all following characters that are digits or
744 // characters in BOTH strings starting at the appropriate marker.
745 // Collect char arrays.
746 do
747 {
748 space1[loc1++] = ch1;
749 marker1++;
750
751 if (marker1 < len1)
752 {
753 ch1 = s1[marker1];
754 }
755 else
756 {
757 break;
758 }
759 } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
760
761 do
762 {
763 space2[loc2++] = ch2;
764 marker2++;
765
766 if (marker2 < len2)
767 {
768 ch2 = s2[marker2];
769 }
770 else
771 {
772 break;
773 }
774 } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
775
776 // If we have collected numbers, compare them numerically.
777 // Otherwise, if we have strings, compare them alphabetically.
778 string str1 = new string(space1);
779 string str2 = new string(space2);
780
781 int result;
782
783 if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
784 {
785 int thisNumericChunk = int.Parse(str1);
786 int thatNumericChunk = int.Parse(str2);
787 result = thisNumericChunk.CompareTo(thatNumericChunk);
788 }
789 else
790 {
791 result = str1.CompareTo(str2);
792 }
793
794 if (result != 0)
795 {
796 return result;
797 }
798 }
799 return len1 - len2;
800 }
801 }
802
803 public list Sort(int stride, int ascending)
804 {
805 if (Data.Length == 0)
806 return new list(); // Don't even bother
807
808 string[] keys;
809
810 if (stride == 1) // The simple case
811 {
812 Object[] ret=new Object[Data.Length];
813
814 Array.Copy(Data, 0, ret, 0, Data.Length);
815
816 keys=new string[Data.Length];
817
818 for (int k = 0; k < Data.Length; k++)
819 keys[k] = Data[k].ToString();
820
821 Array.Sort( keys, ret, new AlphanumComparatorFast() );
822
823 if (ascending == 0)
824 Array.Reverse(ret);
825 return new list(ret);
826 }
827
828 int src=0;
829
830 int len=(Data.Length+stride-1)/stride;
831
832 keys=new string[len];
833 Object[][] vals=new Object[len][];
834
835 int i;
836
837 while (src < Data.Length)
838 {
839 Object[] o=new Object[stride];
840
841 for (i = 0; i < stride; i++)
842 {
843 if (src < Data.Length)
844 o[i]=Data[src++];
845 else
846 {
847 o[i]=new Object();
848 src++;
849 }
850 }
851
852 int idx=src/stride-1;
853 keys[idx]=o[0].ToString();
854 vals[idx]=o;
855 }
856
857 Array.Sort(keys, vals, new AlphanumComparatorFast());
858 if (ascending == 0)
859 {
860 Array.Reverse(vals);
861 }
862
863 Object[] sorted=new Object[stride*vals.Length];
864
865 for (i = 0; i < vals.Length; i++)
866 for (int j = 0; j < stride; j++)
867 sorted[i*stride+j] = vals[i][j];
868
869 return new list(sorted);
870 }
871
872 #region CSV Methods
873
874 public static list FromCSV(string csv)
875 {
876 return new list(csv.Split(','));
877 }
878
879 public string ToCSV()
880 {
881 string ret = "";
882 foreach (object o in this.Data)
883 {
884 if (ret == "")
885 {
886 ret = o.ToString();
887 }
888 else
889 {
890 ret = ret + ", " + o.ToString();
891 }
892 }
893 return ret;
894 }
895
896 private string ToSoup()
897 {
898 string output;
899 output = String.Empty;
900 if (m_data.Length == 0)
901 {
902 return String.Empty;
903 }
904 foreach (object o in m_data)
905 {
906 output = output + o.ToString();
907 }
908 return output;
909 }
910
911 public static explicit operator String(list l)
912 {
913 return l.ToSoup();
914 }
915
916 public static explicit operator LSLString(list l)
917 {
918 return new LSLString(l.ToSoup());
919 }
920
921 public override string ToString()
922 {
923 return ToSoup();
924 }
925
926 #endregion
927
928 #region Statistic Methods
929
930 public double Min()
931 {
932 double minimum = double.PositiveInfinity;
933 double entry;
934 for (int i = 0; i < Data.Length; i++)
935 {
936 if (double.TryParse(Data[i].ToString(), out entry))
937 {
938 if (entry < minimum) minimum = entry;
939 }
940 }
941 return minimum;
942 }
943
944 public double Max()
945 {
946 double maximum = double.NegativeInfinity;
947 double entry;
948 for (int i = 0; i < Data.Length; i++)
949 {
950 if (double.TryParse(Data[i].ToString(), out entry))
951 {
952 if (entry > maximum) maximum = entry;
953 }
954 }
955 return maximum;
956 }
957
958 public double Range()
959 {
960 return (this.Max() / this.Min());
961 }
962
963 public int NumericLength()
964 {
965 int count = 0;
966 double entry;
967 for (int i = 0; i < Data.Length; i++)
968 {
969 if (double.TryParse(Data[i].ToString(), out entry))
970 {
971 count++;
972 }
973 }
974 return count;
975 }
976
977 public static list ToDoubleList(list src)
978 {
979 list ret = new list();
980 double entry;
981 for (int i = 0; i < src.Data.Length - 1; i++)
982 {
983 if (double.TryParse(src.Data[i].ToString(), out entry))
984 {
985 ret.Add(entry);
986 }
987 }
988 return ret;
989 }
990
991 public double Sum()
992 {
993 double sum = 0;
994 double entry;
995 for (int i = 0; i < Data.Length; i++)
996 {
997 if (double.TryParse(Data[i].ToString(), out entry))
998 {
999 sum = sum + entry;
1000 }
1001 }
1002 return sum;
1003 }
1004
1005 public double SumSqrs()
1006 {
1007 double sum = 0;
1008 double entry;
1009 for (int i = 0; i < Data.Length; i++)
1010 {
1011 if (double.TryParse(Data[i].ToString(), out entry))
1012 {
1013 sum = sum + Math.Pow(entry, 2);
1014 }
1015 }
1016 return sum;
1017 }
1018
1019 public double Mean()
1020 {
1021 return (this.Sum() / this.NumericLength());
1022 }
1023
1024 public void NumericSort()
1025 {
1026 IComparer Numeric = new NumericComparer();
1027 Array.Sort(Data, Numeric);
1028 }
1029
1030 public void AlphaSort()
1031 {
1032 IComparer Alpha = new AlphaCompare();
1033 Array.Sort(Data, Alpha);
1034 }
1035
1036 public double Median()
1037 {
1038 return Qi(0.5);
1039 }
1040
1041 public double GeometricMean()
1042 {
1043 double ret = 1.0;
1044 list nums = ToDoubleList(this);
1045 for (int i = 0; i < nums.Data.Length; i++)
1046 {
1047 ret *= (double)nums.Data[i];
1048 }
1049 return Math.Exp(Math.Log(ret) / (double)nums.Data.Length);
1050 }
1051
1052 public double HarmonicMean()
1053 {
1054 double ret = 0.0;
1055 list nums = ToDoubleList(this);
1056 for (int i = 0; i < nums.Data.Length; i++)
1057 {
1058 ret += 1.0 / (double)nums.Data[i];
1059 }
1060 return ((double)nums.Data.Length / ret);
1061 }
1062
1063 public double Variance()
1064 {
1065 double s = 0;
1066 list num = ToDoubleList(this);
1067 for (int i = 0; i < num.Data.Length; i++)
1068 {
1069 s += Math.Pow((double)num.Data[i], 2);
1070 }
1071 return (s - num.Data.Length * Math.Pow(num.Mean(), 2)) / (num.Data.Length - 1);
1072 }
1073
1074 public double StdDev()
1075 {
1076 return Math.Sqrt(this.Variance());
1077 }
1078
1079 public double Qi(double i)
1080 {
1081 list j = this;
1082 j.NumericSort();
1083
1084 if (Math.Ceiling(this.Length * i) == this.Length * i)
1085 {
1086 return (double)((double)j.Data[(int)(this.Length * i - 1)] + (double)j.Data[(int)(this.Length * i)]) / 2;
1087 }
1088 else
1089 {
1090 return (double)j.Data[((int)(Math.Ceiling(this.Length * i))) - 1];
1091 }
1092 }
1093
1094 #endregion
1095
1096 public string ToPrettyString()
1097 {
1098 string output;
1099 if (m_data.Length == 0)
1100 {
1101 return "[]";
1102 }
1103 output = "[";
1104 foreach (object o in m_data)
1105 {
1106 if (o is String)
1107 {
1108 output = output + "\"" + o + "\", ";
1109 }
1110 else
1111 {
1112 output = output + o.ToString() + ", ";
1113 }
1114 }
1115 output = output.Substring(0, output.Length - 2);
1116 output = output + "]";
1117 return output;
1118 }
1119
1120 public class AlphaCompare : IComparer
1121 {
1122 int IComparer.Compare(object x, object y)
1123 {
1124 return string.Compare(x.ToString(), y.ToString());
1125 }
1126 }
1127
1128 public class NumericComparer : IComparer
1129 {
1130 int IComparer.Compare(object x, object y)
1131 {
1132 double a;
1133 double b;
1134 if (!double.TryParse(x.ToString(), out a))
1135 {
1136 a = 0.0;
1137 }
1138 if (!double.TryParse(y.ToString(), out b))
1139 {
1140 b = 0.0;
1141 }
1142 if (a < b)
1143 {
1144 return -1;
1145 }
1146 else if (a == b)
1147 {
1148 return 0;
1149 }
1150 else
1151 {
1152 return 1;
1153 }
1154 }
1155 }
1156
1157 }
1158
1159 //
1160 // BELOW IS WORK IN PROGRESS... IT WILL CHANGE, SO DON'T USE YET! :)
1161 //
1162
1163 public struct StringTest
1164 {
1165 // Our own little string
1166 internal string actualString;
1167 public static implicit operator bool(StringTest mString)
1168 {
1169 if (mString.actualString.Length == 0)
1170 return true;
1171 return false;
1172 }
1173 public override string ToString()
1174 {
1175 return actualString;
1176 }
1177
1178 }
1179
1180 [Serializable]
1181 public struct key
1182 {
1183 public string value;
1184
1185 #region Constructors
1186 public key(string s)
1187 {
1188 value = s;
1189 }
1190
1191 #endregion
1192
1193 #region Methods
1194
1195 static public bool Parse2Key(string s)
1196 {
1197 Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
1198 if (isuuid.IsMatch(s))
1199 {
1200 return true;
1201 }
1202 else
1203 {
1204 return false;
1205 }
1206 }
1207
1208 #endregion
1209
1210 #region Operators
1211
1212 static public implicit operator Boolean(key k)
1213 {
1214 if (k.value.Length == 0)
1215 {
1216 return false;
1217 }
1218
1219 if (k.value == "00000000-0000-0000-0000-000000000000")
1220 {
1221 return false;
1222 }
1223 Regex isuuid = new Regex(@"^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
1224 if (isuuid.IsMatch(k.value))
1225 {
1226 return true;
1227 }
1228 else
1229 {
1230 return false;
1231 }
1232 }
1233
1234 static public implicit operator key(string s)
1235 {
1236 return new key(s);
1237 }
1238
1239 static public implicit operator String(key k)
1240 {
1241 return k.value;
1242 }
1243
1244 static public implicit operator LSLString(key k)
1245 {
1246 return k.value;
1247 }
1248
1249 public static bool operator ==(key k1, key k2)
1250 {
1251 return k1.value == k2.value;
1252 }
1253 public static bool operator !=(key k1, key k2)
1254 {
1255 return k1.value != k2.value;
1256 }
1257
1258 #endregion
1259
1260 #region Overriders
1261
1262 public override bool Equals(object o)
1263 {
1264 return o.ToString() == value;
1265 }
1266
1267 public override int GetHashCode()
1268 {
1269 return value.GetHashCode();
1270 }
1271
1272 public override string ToString()
1273 {
1274 return value;
1275 }
1276
1277 #endregion
1278 }
1279
1280 [Serializable]
1281 public struct LSLString
1282 {
1283 public string m_string;
1284 #region Constructors
1285 public LSLString(string s)
1286 {
1287 m_string = s;
1288 }
1289
1290 public LSLString(double d)
1291 {
1292 string s=String.Format("{0:0.000000}", d);
1293 m_string=s;
1294 }
1295
1296 public LSLString(LSLFloat f)
1297 {
1298 string s=String.Format("{0:0.000000}", f.value);
1299 m_string=s;
1300 }
1301
1302 #endregion
1303
1304 #region Operators
1305 static public implicit operator Boolean(LSLString s)
1306 {
1307 if (s.m_string.Length == 0)
1308 {
1309 return false;
1310 }
1311 else
1312 {
1313 return true;
1314 }
1315 }
1316
1317
1318
1319 static public implicit operator String(LSLString s)
1320 {
1321 return s.m_string;
1322 }
1323
1324 static public implicit operator LSLString(string s)
1325 {
1326 return new LSLString(s);
1327 }
1328
1329 public static string ToString(LSLString s)
1330 {
1331 return s.m_string;
1332 }
1333
1334 public override string ToString()
1335 {
1336 return m_string;
1337 }
1338
1339 public static bool operator ==(LSLString s1, string s2)
1340 {
1341 return s1.m_string == s2;
1342 }
1343
1344 public static bool operator !=(LSLString s1, string s2)
1345 {
1346 return s1.m_string != s2;
1347 }
1348
1349 public static explicit operator double(LSLString s)
1350 {
1351 return Convert.ToDouble(s.m_string);
1352 }
1353
1354 public static explicit operator LSLInteger(LSLString s)
1355 {
1356 return new LSLInteger(s.m_string);
1357 }
1358
1359 public static explicit operator LSLString(double d)
1360 {
1361 return new LSLString(d);
1362 }
1363
1364 public static explicit operator LSLString(LSLFloat f)
1365 {
1366 return new LSLString(f);
1367 }
1368
1369 static public explicit operator LSLString(bool b)
1370 {
1371 if (b)
1372 return new LSLString("1");
1373 else
1374 return new LSLString("0");
1375 }
1376
1377 public static implicit operator Vector3(LSLString s)
1378 {
1379 return new Vector3(s.m_string);
1380 }
1381
1382 public static implicit operator Quaternion(LSLString s)
1383 {
1384 return new Quaternion(s.m_string);
1385 }
1386
1387 public static implicit operator LSLFloat(LSLString s)
1388 {
1389 return new LSLFloat(Convert.ToDouble(s.m_string));
1390 }
1391
1392 #endregion
1393
1394 #region Overriders
1395 public override bool Equals(object o)
1396 {
1397 return m_string == o.ToString();
1398 }
1399
1400 public override int GetHashCode()
1401 {
1402 return m_string.GetHashCode();
1403 }
1404
1405 #endregion
1406
1407 #region " Standard string functions "
1408 //Clone,CompareTo,Contains
1409 //CopyTo,EndsWith,Equals,GetEnumerator,GetHashCode,GetType,GetTypeCode
1410 //IndexOf,IndexOfAny,Insert,IsNormalized,LastIndexOf,LastIndexOfAny
1411 //Length,Normalize,PadLeft,PadRight,Remove,Replace,Split,StartsWith,Substring,ToCharArray,ToLowerInvariant
1412 //ToString,ToUpper,ToUpperInvariant,Trim,TrimEnd,TrimStart
1413 public bool Contains(string value) { return m_string.Contains(value); }
1414 public int IndexOf(string value) { return m_string.IndexOf(value); }
1415 public int Length { get { return m_string.Length; } }
1416
1417
1418 #endregion
1419 }
1420
1421 [Serializable]
1422 public struct LSLInteger
1423 {
1424 public int value;
1425
1426 #region Constructors
1427 public LSLInteger(int i)
1428 {
1429 value = i;
1430 }
1431
1432 public LSLInteger(double d)
1433 {
1434 value = (int)d;
1435 }
1436
1437 public LSLInteger(string s)
1438 {
1439 Regex r = new Regex("^[ ]*-?[0-9][0-9xX]?[0-9a-fA-F]*");
1440 Match m = r.Match(s);
1441 string v = m.Groups[0].Value;
1442
1443 if (v == String.Empty)
1444 {
1445 value = 0;
1446 }
1447 else
1448 {
1449 try
1450 {
1451 if (v.Contains("x") || v.Contains("X"))
1452 {
1453 value = int.Parse(v.Substring(2), System.Globalization.NumberStyles.HexNumber);
1454 }
1455 else
1456 {
1457 value = int.Parse(v, System.Globalization.NumberStyles.Integer);
1458 }
1459 }
1460 catch (OverflowException)
1461 {
1462 value = -1;
1463 }
1464 }
1465 }
1466
1467 #endregion
1468
1469 #region Operators
1470
1471 static public implicit operator int(LSLInteger i)
1472 {
1473 return i.value;
1474 }
1475
1476 static public implicit operator uint(LSLInteger i)
1477 {
1478 return (uint)i.value;
1479 }
1480
1481 static public explicit operator LSLString(LSLInteger i)
1482 {
1483 return new LSLString(i.ToString());
1484 }
1485
1486 static public implicit operator Boolean(LSLInteger i)
1487 {
1488 if (i.value == 0)
1489 {
1490 return false;
1491 }
1492 else
1493 {
1494 return true;
1495 }
1496 }
1497
1498 static public implicit operator LSLInteger(int i)
1499 {
1500 return new LSLInteger(i);
1501 }
1502
1503 static public explicit operator LSLInteger(string s)
1504 {
1505 return new LSLInteger(s);
1506 }
1507
1508 static public implicit operator LSLInteger(uint u)
1509 {
1510 return new LSLInteger(u);
1511 }
1512
1513 static public explicit operator LSLInteger(double d)
1514 {
1515 return new LSLInteger(d);
1516 }
1517
1518 static public explicit operator LSLInteger(LSLFloat f)
1519 {
1520 return new LSLInteger(f.value);
1521 }
1522
1523 static public implicit operator LSLInteger(bool b)
1524 {
1525 if (b)
1526 return new LSLInteger(1);
1527 else
1528 return new LSLInteger(0);
1529 }
1530
1531 static public bool operator ==(LSLInteger i1, LSLInteger i2)
1532 {
1533 bool ret = i1.value == i2.value;
1534 return ret;
1535 }
1536
1537 static public bool operator !=(LSLInteger i1, LSLInteger i2)
1538 {
1539 bool ret = i1.value != i2.value;
1540 return ret;
1541 }
1542
1543 static public LSLInteger operator +(LSLInteger i1, int i2)
1544 {
1545 return new LSLInteger(i1.value + i2);
1546 }
1547
1548 static public LSLInteger operator -(LSLInteger i1, int i2)
1549 {
1550 return new LSLInteger(i1.value - i2);
1551 }
1552
1553 static public LSLInteger operator *(LSLInteger i1, int i2)
1554 {
1555 return new LSLInteger(i1.value * i2);
1556 }
1557
1558 static public LSLInteger operator /(LSLInteger i1, int i2)
1559 {
1560 return new LSLInteger(i1.value / i2);
1561 }
1562
1563// static public LSLFloat operator +(LSLInteger i1, double f)
1564// {
1565// return new LSLFloat((double)i1.value + f);
1566// }
1567//
1568// static public LSLFloat operator -(LSLInteger i1, double f)
1569// {
1570// return new LSLFloat((double)i1.value - f);
1571// }
1572//
1573// static public LSLFloat operator *(LSLInteger i1, double f)
1574// {
1575// return new LSLFloat((double)i1.value * f);
1576// }
1577//
1578// static public LSLFloat operator /(LSLInteger i1, double f)
1579// {
1580// return new LSLFloat((double)i1.value / f);
1581// }
1582
1583 static public LSLInteger operator -(LSLInteger i)
1584 {
1585 return new LSLInteger(-i.value);
1586 }
1587
1588 public override bool Equals(Object o)
1589 {
1590 if (!(o is LSLInteger))
1591 return false;
1592 return value == ((LSLInteger)o).value;
1593 }
1594
1595 public override int GetHashCode()
1596 {
1597 return value;
1598 }
1599
1600 static public LSLInteger operator &(LSLInteger i1, LSLInteger i2)
1601 {
1602 int ret = i1.value & i2.value;
1603 return ret;
1604 }
1605
1606 public static LSLInteger operator ++(LSLInteger i)
1607 {
1608 i.value++;
1609 return i;
1610 }
1611
1612
1613 public static LSLInteger operator --(LSLInteger i)
1614 {
1615 i.value--;
1616 return i;
1617 }
1618
1619 static public implicit operator System.Double(LSLInteger i)
1620 {
1621 return (double)i.value;
1622 }
1623
1624 public static bool operator true(LSLInteger i)
1625 {
1626 return i.value != 0;
1627 }
1628
1629 public static bool operator false(LSLInteger i)
1630 {
1631 return i.value == 0;
1632 }
1633
1634 #endregion
1635
1636 #region Overriders
1637
1638 public override string ToString()
1639 {
1640 return this.value.ToString();
1641 }
1642
1643 #endregion
1644 }
1645
1646 [Serializable]
1647 public struct LSLFloat
1648 {
1649 public double value;
1650
1651 #region Constructors
1652
1653 public LSLFloat(int i)
1654 {
1655 this.value = (double)i;
1656 }
1657
1658 public LSLFloat(double d)
1659 {
1660 this.value = d;
1661 }
1662
1663 public LSLFloat(string s)
1664 {
1665 this.value = double.Parse(s);
1666 }
1667
1668 #endregion
1669
1670 #region Operators
1671
1672 static public explicit operator float(LSLFloat f)
1673 {
1674 return (float)f.value;
1675 }
1676
1677 static public explicit operator int(LSLFloat f)
1678 {
1679 return (int)f.value;
1680 }
1681
1682 static public explicit operator uint(LSLFloat f)
1683 {
1684 return (uint) Math.Abs(f.value);
1685 }
1686
1687 static public implicit operator Boolean(LSLFloat f)
1688 {
1689 if (f.value == 0.0)
1690 {
1691 return false;
1692 }
1693 else
1694 {
1695 return true;
1696 }
1697 }
1698
1699 static public implicit operator LSLFloat(int i)
1700 {
1701 return new LSLFloat(i);
1702 }
1703
1704 static public implicit operator LSLFloat(LSLInteger i)
1705 {
1706 return new LSLFloat(i.value);
1707 }
1708
1709 static public explicit operator LSLFloat(string s)
1710 {
1711 Regex r = new Regex("^[ ]*-?[0-9]*\\.?[0-9]*[eE]?-?[0-9]*");
1712 Match m = r.Match(s);
1713 string v = m.Groups[0].Value;
1714
1715 while (v.Length > 0 && v.Substring(0, 1) == " ")
1716 v = v.Substring(1);
1717
1718 if (v == String.Empty)
1719 v = "0";
1720
1721 return new LSLFloat(double.Parse(v));
1722 }
1723
1724 static public implicit operator LSLFloat(double d)
1725 {
1726 return new LSLFloat(d);
1727 }
1728
1729 static public implicit operator LSLFloat(bool b)
1730 {
1731 if (b)
1732 return new LSLFloat(1.0);
1733 else
1734 return new LSLFloat(0.0);
1735 }
1736
1737 static public bool operator ==(LSLFloat f1, LSLFloat f2)
1738 {
1739 return f1.value == f2.value;
1740 }
1741
1742 static public bool operator !=(LSLFloat f1, LSLFloat f2)
1743 {
1744 return f1.value != f2.value;
1745 }
1746
1747 static public LSLFloat operator ++(LSLFloat f)
1748 {
1749 f.value++;
1750 return f;
1751 }
1752
1753 static public LSLFloat operator --(LSLFloat f)
1754 {
1755 f.value--;
1756 return f;
1757 }
1758
1759 static public LSLFloat operator +(LSLFloat f, int i)
1760 {
1761 return new LSLFloat(f.value + (double)i);
1762 }
1763
1764 static public LSLFloat operator -(LSLFloat f, int i)
1765 {
1766 return new LSLFloat(f.value - (double)i);
1767 }
1768
1769 static public LSLFloat operator *(LSLFloat f, int i)
1770 {
1771 return new LSLFloat(f.value * (double)i);
1772 }
1773
1774 static public LSLFloat operator /(LSLFloat f, int i)
1775 {
1776 return new LSLFloat(f.value / (double)i);
1777 }
1778
1779 static public LSLFloat operator +(LSLFloat lhs, LSLFloat rhs)
1780 {
1781 return new LSLFloat(lhs.value + rhs.value);
1782 }
1783
1784 static public LSLFloat operator -(LSLFloat lhs, LSLFloat rhs)
1785 {
1786 return new LSLFloat(lhs.value - rhs.value);
1787 }
1788
1789 static public LSLFloat operator *(LSLFloat lhs, LSLFloat rhs)
1790 {
1791 return new LSLFloat(lhs.value * rhs.value);
1792 }
1793
1794 static public LSLFloat operator /(LSLFloat lhs, LSLFloat rhs)
1795 {
1796 return new LSLFloat(lhs.value / rhs.value);
1797 }
1798
1799 static public LSLFloat operator -(LSLFloat f)
1800 {
1801 return new LSLFloat(-f.value);
1802 }
1803
1804 static public implicit operator System.Double(LSLFloat f)
1805 {
1806 return f.value;
1807 }
1808
1809 #endregion
1810
1811 #region Overriders
1812
1813 public override string ToString()
1814 {
1815 return String.Format("{0:0.000000}", this.value);
1816 }
1817
1818 public override bool Equals(Object o)
1819 {
1820 if (!(o is LSLFloat))
1821 return false;
1822 return value == ((LSLFloat)o).value;
1823 }
1824
1825 public override int GetHashCode()
1826 {
1827 return Convert.ToInt32(value);
1828 }
1829
1830
1831 #endregion
1832 }
1833 }
1834}