aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs
diff options
context:
space:
mode:
authorlbsa712008-06-24 21:09:49 +0000
committerlbsa712008-06-24 21:09:49 +0000
commit6b7930104bdb845d3b9c085dc04f52b6446f23b1 (patch)
tree05ee45781a455817fa400bb99f30f4d19d4eb1f8 /OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs
parentbased on positive feedback on performance of making keys fixed length (diff)
downloadopensim-SC_OLD-6b7930104bdb845d3b9c085dc04f52b6446f23b1.zip
opensim-SC_OLD-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.gz
opensim-SC_OLD-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.bz2
opensim-SC_OLD-6b7930104bdb845d3b9c085dc04f52b6446f23b1.tar.xz
* Applied patch from Melanie, mantis issue #1581 - "Refactor LSL language, api and compiler out of XEngine"
"First stage in a major Script Engine refactor, that will result in the LSL implementaions ebing reconverged. Not there yet, but one major part is done." Thank you, Melanie!
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs1519
1 files changed, 1519 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs
new file mode 100644
index 0000000..6b080a0
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/LSL_Types.cs
@@ -0,0 +1,1519 @@
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.Shared
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 && x == vector.x && z == vector.z);
127 }
128
129 #endregion
130
131 #region Vector & Vector Math
132
133 // Vector-Vector Math
134 public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
135 {
136 return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
137 }
138
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 Vector3 operator %(Vector3 v1, Vector3 v2)
150 {
151 //Cross product
152 Vector3 tv;
153 tv.x = (v1.y * v2.z) - (v1.z * v2.y);
154 tv.y = (v1.z * v2.x) - (v1.x * v2.z);
155 tv.z = (v1.x * v2.y) - (v1.y * v2.x);
156 return tv;
157 }
158
159 #endregion
160
161 #region Vector & Float Math
162
163 // Vector-Float and Float-Vector Math
164 public static Vector3 operator *(Vector3 vec, float val)
165 {
166 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
167 }
168
169 public static Vector3 operator *(float val, Vector3 vec)
170 {
171 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
172 }
173
174 public static Vector3 operator /(Vector3 v, float f)
175 {
176 v.x = v.x / f;
177 v.y = v.y / f;
178 v.z = v.z / f;
179 return v;
180 }
181
182 #endregion
183
184 #region Vector & Double Math
185
186 public static Vector3 operator *(Vector3 vec, double val)
187 {
188 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
189 }
190
191 public static Vector3 operator *(double val, Vector3 vec)
192 {
193 return new Vector3(vec.x * val, vec.y * val, vec.z * val);
194 }
195
196 public static Vector3 operator /(Vector3 v, double f)
197 {
198 v.x = v.x / f;
199 v.y = v.y / f;
200 v.z = v.z / f;
201 return v;
202 }
203
204 #endregion
205
206 #region Vector & Rotation Math
207
208 // Vector-Rotation Math
209 public static Vector3 operator *(Vector3 v, Quaternion r)
210 {
211 Quaternion vq = new Quaternion(v.x, v.y, v.z, 0);
212 Quaternion nq = new Quaternion(-r.x, -r.y, -r.z, r.s);
213
214 // adapted for operator * computing "b * a"
215 Quaternion result = nq * (vq * r);
216
217 return new Vector3(result.x, result.y, result.z);
218 }
219
220 public static Vector3 operator /(Vector3 v, Quaternion r)
221 {
222 r.s = -r.s;
223 return v * r;
224 }
225
226 #endregion
227
228 #region Static Helper Functions
229
230 public static double Dot(Vector3 v1, Vector3 v2)
231 {
232 return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
233 }
234
235 public static Vector3 Cross(Vector3 v1, Vector3 v2)
236 {
237 return new Vector3
238 (
239 v1.y * v2.z - v1.z * v2.y,
240 v1.z * v2.x - v1.x * v2.z,
241 v1.x * v2.y - v1.y * v2.x
242 );
243 }
244
245 public static double Mag(Vector3 v)
246 {
247 return Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
248 }
249
250 public static Vector3 Norm(Vector3 vector)
251 {
252 double mag = Mag(vector);
253 return new Vector3(vector.x / mag, vector.y / mag, vector.z / mag);
254 }
255
256 #endregion
257 }
258
259 [Serializable]
260 public struct Quaternion
261 {
262 public double x;
263 public double y;
264 public double z;
265 public double s;
266
267 #region Constructors
268
269 public Quaternion(Quaternion Quat)
270 {
271 x = (float)Quat.x;
272 y = (float)Quat.y;
273 z = (float)Quat.z;
274 s = (float)Quat.s;
275 if (x == 0 && y == 0 && z == 0 && s == 0)
276 s = 1;
277 }
278
279 public Quaternion(double X, double Y, double Z, double S)
280 {
281 x = X;
282 y = Y;
283 z = Z;
284 s = S;
285 if (x == 0 && y == 0 && z == 0 && s == 0)
286 s = 1;
287 }
288
289 public Quaternion(string str)
290 {
291 str = str.Replace('<', ' ');
292 str = str.Replace('>', ' ');
293 string[] tmps = str.Split(new Char[] { ',', '<', '>' });
294 if (tmps.Length < 4)
295 {
296 x=y=z=s=0;
297 return;
298 }
299 bool res;
300 res = Double.TryParse(tmps[0], out x);
301 res = res & Double.TryParse(tmps[1], out y);
302 res = res & Double.TryParse(tmps[2], out z);
303 res = res & Double.TryParse(tmps[3], out s);
304 if (x == 0 && y == 0 && z == 0 && s == 0)
305 s = 1;
306 }
307
308 #endregion
309
310 #region Overriders
311
312 public override int GetHashCode()
313 {
314 return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
315 }
316
317 public override bool Equals(object o)
318 {
319 if (!(o is Quaternion)) return false;
320
321 Quaternion quaternion = (Quaternion)o;
322
323 return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
324 }
325
326 public override string ToString()
327 {
328 string st=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", x, y, z, s);
329 return st;
330 }
331
332 public static explicit operator string(Quaternion r)
333 {
334 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
335 return s;
336 }
337
338 public static explicit operator LSLString(Quaternion r)
339 {
340 string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
341 return new LSLString(s);
342 }
343
344 public static explicit operator Quaternion(string s)
345 {
346 return new Quaternion(s);
347 }
348
349 public static bool operator ==(Quaternion lhs, Quaternion rhs)
350 {
351 // Return true if the fields match:
352 return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
353 }
354
355 public static bool operator !=(Quaternion lhs, Quaternion rhs)
356 {
357 return !(lhs == rhs);
358 }
359
360 #endregion
361
362 public static Quaternion operator +(Quaternion a, Quaternion b)
363 {
364 return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
365 }
366
367 public static Quaternion operator /(Quaternion a, Quaternion b)
368 {
369 b.s = -b.s;
370 return a * b;
371 }
372
373 public static Quaternion operator -(Quaternion a, Quaternion b)
374 {
375 return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
376 }
377
378 // using the equations below, we need to do "b * a" to be compatible with LSL
379 public static Quaternion operator *(Quaternion b, Quaternion a)
380 {
381 Quaternion c;
382 c.x = a.s * b.x + a.x * b.s + a.y * b.z - a.z * b.y;
383 c.y = a.s * b.y + a.y * b.s + a.z * b.x - a.x * b.z;
384 c.z = a.s * b.z + a.z * b.s + a.x * b.y - a.y * b.x;
385 c.s = a.s * b.s - a.x * b.x - a.y * b.y - a.z * b.z;
386 return c;
387 }
388 }
389
390 [Serializable]
391 public class list
392 {
393 private object[] m_data;
394
395 public list(params object[] args)
396 {
397 m_data = new object[args.Length];
398 m_data = args;
399 }
400
401 public int Length
402 {
403 get {
404 if (m_data == null)
405 m_data=new Object[0];
406 return m_data.Length;
407 }
408 }
409
410 public object[] Data
411 {
412 get {
413 if (m_data == null)
414 m_data=new Object[0];
415 return m_data;
416 }
417 }
418
419 public static list operator +(list a, list b)
420 {
421 object[] tmp;
422 tmp = new object[a.Length + b.Length];
423 a.Data.CopyTo(tmp, 0);
424 b.Data.CopyTo(tmp, a.Length);
425 return new list(tmp);
426 }
427
428 private void ExtendAndAdd(object o)
429 {
430 Array.Resize(ref m_data, Length + 1);
431 m_data.SetValue(o, Length - 1);
432 }
433
434 public static list operator +(list a, string s)
435 {
436 a.ExtendAndAdd(s);
437 return a;
438 }
439
440 public static list operator +(list a, int i)
441 {
442 a.ExtendAndAdd(i);
443 return a;
444 }
445
446 public static list operator +(list a, double d)
447 {
448 a.ExtendAndAdd(d);
449 return a;
450 }
451
452 public void Add(object o)
453 {
454 object[] tmp;
455 tmp = new object[m_data.Length + 1];
456 m_data.CopyTo(tmp, 0);
457 tmp[m_data.Length] = o;
458 m_data = tmp;
459 }
460
461 public bool Contains(object o)
462 {
463 bool ret = false;
464 foreach (object i in Data)
465 {
466 if (i == o)
467 {
468 ret = true;
469 break;
470 }
471 }
472 return ret;
473 }
474
475 public list DeleteSublist(int start, int end)
476 {
477 // Not an easy one
478 // If start <= end, remove that part
479 // if either is negative, count from the end of the array
480 // if the resulting start > end, remove all BUT that part
481
482 Object[] ret;
483
484 if (start < 0)
485 start=m_data.Length-start;
486
487 if (start < 0)
488 start=0;
489
490 if (end < 0)
491 end=m_data.Length-end;
492 if (end < 0)
493 end=0;
494
495 if (start > end)
496 {
497 if (end >= m_data.Length)
498 return new list(new Object[0]);
499
500 if (start >= m_data.Length)
501 start=m_data.Length-1;
502
503 return GetSublist(end, start);
504 }
505
506 // start >= 0 && end >= 0 here
507 if (start >= m_data.Length)
508 {
509 ret=new Object[m_data.Length];
510 Array.Copy(m_data, 0, ret, 0, m_data.Length);
511
512 return new list(ret);
513 }
514
515 if (end >= m_data.Length)
516 end=m_data.Length-1;
517
518 // now, this makes the math easier
519 int remove=end+1-start;
520
521 ret=new Object[m_data.Length-remove];
522 if (ret.Length == 0)
523 return new list(ret);
524
525 int src;
526 int dest=0;
527
528 for (src = 0; src < m_data.Length; src++)
529 {
530 if (src < start || src > end)
531 ret[dest++]=m_data[src];
532 }
533
534 return new list(ret);
535 }
536
537 public list GetSublist(int start, int end)
538 {
539
540 object[] ret;
541
542 // Take care of neg start or end's
543 // NOTE that either index may still be negative after
544 // adding the length, so we must take additional
545 // measures to protect against this. Note also that
546 // after normalisation the negative indices are no
547 // longer relative to the end of the list.
548
549 if (start < 0)
550 {
551 start = m_data.Length + start;
552 }
553
554 if (end < 0)
555 {
556 end = m_data.Length + end;
557 }
558
559 // The conventional case is start <= end
560 // NOTE that the case of an empty list is
561 // dealt with by the initial test. Start
562 // less than end is taken to be the most
563 // common case.
564
565 if (start <= end)
566 {
567
568 // Start sublist beyond length
569 // Also deals with start AND end still negative
570 if (start >= m_data.Length || end < 0)
571 {
572 return new list();
573 }
574
575 // Sublist extends beyond the end of the supplied list
576 if (end >= m_data.Length)
577 {
578 end = m_data.Length - 1;
579 }
580
581 // Sublist still starts before the beginning of the list
582 if (start < 0)
583 {
584 start = 0;
585 }
586
587 ret = new object[end - start + 1];
588
589 Array.Copy(m_data, start, ret, 0, end - start + 1);
590
591 return new list(ret);
592
593 }
594
595 // Deal with the segmented case: 0->end + start->EOL
596
597 else
598 {
599
600 list result = null;
601
602 // If end is negative, then prefix list is empty
603 if (end < 0)
604 {
605 result = new list();
606 // If start is still negative, then the whole of
607 // the existing list is returned. This case is
608 // only admitted if end is also still negative.
609 if (start < 0)
610 {
611 return this;
612 }
613
614 }
615 else
616 {
617 result = GetSublist(0,end);
618 }
619
620 // If start is outside of list, then just return
621 // the prefix, whatever it is.
622 if (start >= m_data.Length)
623 {
624 return result;
625 }
626
627 return result + GetSublist(start, Data.Length);
628
629 }
630 }
631
632 public list Sort(int stride, int ascending)
633 {
634 if (Data.Length == 0)
635 return new list(); // Don't even bother
636
637 string[] keys;
638
639 if (stride == 1) // The simple case
640 {
641 Object[] ret=new Object[Data.Length];
642
643 Array.Copy(Data, 0, ret, 0, Data.Length);
644
645 keys=new string[Data.Length];
646
647 for (int k = 0; k < Data.Length; k++)
648 keys[k] = Data[k].ToString();
649
650 Array.Sort(keys, ret);
651
652 if (ascending == 0)
653 Array.Reverse(ret);
654 return new list(ret);
655 }
656
657 int src=0;
658
659 int len=(Data.Length+stride-1)/stride;
660
661 keys=new string[len];
662 Object[][] vals=new Object[len][];
663
664 int i;
665
666 while (src < Data.Length)
667 {
668 Object[] o=new Object[stride];
669
670 for (i = 0; i < stride; i++)
671 {
672 if (src < Data.Length)
673 o[i]=Data[src++];
674 else
675 {
676 o[i]=new Object();
677 src++;
678 }
679 }
680
681 int idx=src/stride-1;
682 keys[idx]=o[0].ToString();
683 vals[idx]=o;
684 }
685
686 Array.Sort(keys, vals);
687 if (ascending == 0)
688 {
689 Array.Reverse(vals);
690 }
691
692 Object[] sorted=new Object[stride*vals.Length];
693
694 for (i = 0; i < vals.Length; i++)
695 for (int j = 0; j < stride; j++)
696 sorted[i*stride+j] = vals[i][j];
697
698 return new list(sorted);
699 }
700
701 #region CSV Methods
702
703 public static list FromCSV(string csv)
704 {
705 return new list(csv.Split(','));
706 }
707
708 public string ToCSV()
709 {
710 string ret = "";
711 foreach (object o in this.Data)
712 {
713 if (ret == "")
714 {
715 ret = o.ToString();
716 }
717 else
718 {
719 ret = ret + ", " + o.ToString();
720 }
721 }
722 return ret;
723 }
724
725 private string ToSoup()
726 {
727 string output;
728 output = String.Empty;
729 if (m_data.Length == 0)
730 {
731 return String.Empty;
732 }
733 foreach (object o in m_data)
734 {
735 output = output + o.ToString();
736 }
737 return output;
738 }
739
740 public static explicit operator String(list l)
741 {
742 return l.ToSoup();
743 }
744
745 public static explicit operator LSLString(list l)
746 {
747 return new LSLString(l.ToSoup());
748 }
749
750 public override string ToString()
751 {
752 return ToSoup();
753 }
754
755 #endregion
756
757 #region Statistic Methods
758
759 public double Min()
760 {
761 double minimum = double.PositiveInfinity;
762 double entry;
763 for (int i = 0; i < Data.Length; i++)
764 {
765 if (double.TryParse(Data[i].ToString(), out entry))
766 {
767 if (entry < minimum) minimum = entry;
768 }
769 }
770 return minimum;
771 }
772
773 public double Max()
774 {
775 double maximum = double.NegativeInfinity;
776 double entry;
777 for (int i = 0; i < Data.Length; i++)
778 {
779 if (double.TryParse(Data[i].ToString(), out entry))
780 {
781 if (entry > maximum) maximum = entry;
782 }
783 }
784 return maximum;
785 }
786
787 public double Range()
788 {
789 return (this.Max() / this.Min());
790 }
791
792 public int NumericLength()
793 {
794 int count = 0;
795 double entry;
796 for (int i = 0; i < Data.Length; i++)
797 {
798 if (double.TryParse(Data[i].ToString(), out entry))
799 {
800 count++;
801 }
802 }
803 return count;
804 }
805
806 public static list ToDoubleList(list src)
807 {
808 list ret = new list();
809 double entry;
810 for (int i = 0; i < src.Data.Length - 1; i++)
811 {
812 if (double.TryParse(src.Data[i].ToString(), out entry))
813 {
814 ret.Add(entry);
815 }
816 }
817 return ret;
818 }
819
820 public double Sum()
821 {
822 double sum = 0;
823 double entry;
824 for (int i = 0; i < Data.Length; i++)
825 {
826 if (double.TryParse(Data[i].ToString(), out entry))
827 {
828 sum = sum + entry;
829 }
830 }
831 return sum;
832 }
833
834 public double SumSqrs()
835 {
836 double sum = 0;
837 double entry;
838 for (int i = 0; i < Data.Length; i++)
839 {
840 if (double.TryParse(Data[i].ToString(), out entry))
841 {
842 sum = sum + Math.Pow(entry, 2);
843 }
844 }
845 return sum;
846 }
847
848 public double Mean()
849 {
850 return (this.Sum() / this.NumericLength());
851 }
852
853 public void NumericSort()
854 {
855 IComparer Numeric = new NumericComparer();
856 Array.Sort(Data, Numeric);
857 }
858
859 public void AlphaSort()
860 {
861 IComparer Alpha = new AlphaCompare();
862 Array.Sort(Data, Alpha);
863 }
864
865 public double Median()
866 {
867 return Qi(0.5);
868 }
869
870 public double GeometricMean()
871 {
872 double ret = 1.0;
873 list nums = ToDoubleList(this);
874 for (int i = 0; i < nums.Data.Length; i++)
875 {
876 ret *= (double)nums.Data[i];
877 }
878 return Math.Exp(Math.Log(ret) / (double)nums.Data.Length);
879 }
880
881 public double HarmonicMean()
882 {
883 double ret = 0.0;
884 list nums = ToDoubleList(this);
885 for (int i = 0; i < nums.Data.Length; i++)
886 {
887 ret += 1.0 / (double)nums.Data[i];
888 }
889 return ((double)nums.Data.Length / ret);
890 }
891
892 public double Variance()
893 {
894 double s = 0;
895 list num = ToDoubleList(this);
896 for (int i = 0; i < num.Data.Length; i++)
897 {
898 s += Math.Pow((double)num.Data[i], 2);
899 }
900 return (s - num.Data.Length * Math.Pow(num.Mean(), 2)) / (num.Data.Length - 1);
901 }
902
903 public double StdDev()
904 {
905 return Math.Sqrt(this.Variance());
906 }
907
908 public double Qi(double i)
909 {
910 list j = this;
911 j.NumericSort();
912
913 if (Math.Ceiling(this.Length * i) == this.Length * i)
914 {
915 return (double)((double)j.Data[(int)(this.Length * i - 1)] + (double)j.Data[(int)(this.Length * i)]) / 2;
916 }
917 else
918 {
919 return (double)j.Data[((int)(Math.Ceiling(this.Length * i))) - 1];
920 }
921 }
922
923 #endregion
924
925 public string ToPrettyString()
926 {
927 string output;
928 if (m_data.Length == 0)
929 {
930 return "[]";
931 }
932 output = "[";
933 foreach (object o in m_data)
934 {
935 if (o is String)
936 {
937 output = output + "\"" + o + "\", ";
938 }
939 else
940 {
941 output = output + o.ToString() + ", ";
942 }
943 }
944 output = output.Substring(0, output.Length - 2);
945 output = output + "]";
946 return output;
947 }
948
949 public class AlphaCompare : IComparer
950 {
951 int IComparer.Compare(object x, object y)
952 {
953 return string.Compare(x.ToString(), y.ToString());
954 }
955 }
956
957 public class NumericComparer : IComparer
958 {
959 int IComparer.Compare(object x, object y)
960 {
961 double a;
962 double b;
963 if (!double.TryParse(x.ToString(), out a))
964 {
965 a = 0.0;
966 }
967 if (!double.TryParse(y.ToString(), out b))
968 {
969 b = 0.0;
970 }
971 if (a < b)
972 {
973 return -1;
974 }
975 else if (a == b)
976 {
977 return 0;
978 }
979 else
980 {
981 return 1;
982 }
983 }
984 }
985
986 }
987
988 //
989 // BELOW IS WORK IN PROGRESS... IT WILL CHANGE, SO DON'T USE YET! :)
990 //
991
992 public struct StringTest
993 {
994 // Our own little string
995 internal string actualString;
996 public static implicit operator bool(StringTest mString)
997 {
998 if (mString.actualString.Length == 0)
999 return true;
1000 return false;
1001 }
1002 public override string ToString()
1003 {
1004 return actualString;
1005 }
1006
1007 }
1008
1009 [Serializable]
1010 public struct key
1011 {
1012 public string value;
1013
1014 #region Constructors
1015 public key(string s)
1016 {
1017 value = s;
1018 }
1019
1020 #endregion
1021
1022 #region Methods
1023
1024 static public bool Parse2Key(string s)
1025 {
1026 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);
1027 if (isuuid.IsMatch(s))
1028 {
1029 return true;
1030 }
1031 else
1032 {
1033 return false;
1034 }
1035 }
1036
1037 #endregion
1038
1039 #region Operators
1040
1041 static public implicit operator Boolean(key k)
1042 {
1043 if (k.value.Length == 0)
1044 {
1045 return false;
1046 }
1047
1048 if (k.value == "00000000-0000-0000-0000-000000000000")
1049 {
1050 return false;
1051 }
1052 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);
1053 if (isuuid.IsMatch(k.value))
1054 {
1055 return true;
1056 }
1057 else
1058 {
1059 return false;
1060 }
1061 }
1062
1063 static public implicit operator key(string s)
1064 {
1065 return new key(s);
1066 }
1067
1068 static public implicit operator String(key k)
1069 {
1070 return k.value;
1071 }
1072
1073 public static bool operator ==(key k1, key k2)
1074 {
1075 return k1.value == k2.value;
1076 }
1077 public static bool operator !=(key k1, key k2)
1078 {
1079 return k1.value != k2.value;
1080 }
1081
1082 #endregion
1083
1084 #region Overriders
1085
1086 public override bool Equals(object o)
1087 {
1088 return o.ToString() == value;
1089 }
1090
1091 public override int GetHashCode()
1092 {
1093 return value.GetHashCode();
1094 }
1095
1096 #endregion
1097 }
1098
1099 [Serializable]
1100 public struct LSLString
1101 {
1102 public string m_string;
1103 #region Constructors
1104 public LSLString(string s)
1105 {
1106 m_string = s;
1107 }
1108
1109 public LSLString(int i)
1110 {
1111 m_string=i.ToString();
1112 }
1113
1114 public LSLString(double d)
1115 {
1116 string s=String.Format("{0:0.000000}", d);
1117 m_string=s;
1118 }
1119
1120 public LSLString(LSLFloat f)
1121 {
1122 string s=String.Format("{0:0.000000}", f.value);
1123 m_string=s;
1124 }
1125
1126 #endregion
1127
1128 #region Operators
1129 static public implicit operator Boolean(LSLString s)
1130 {
1131 if (s.m_string.Length == 0)
1132 {
1133 return false;
1134 }
1135 else
1136 {
1137 return true;
1138 }
1139 }
1140
1141
1142
1143 static public implicit operator String(LSLString s)
1144 {
1145 return s.m_string;
1146 }
1147
1148 static public implicit operator LSLString(string s)
1149 {
1150 return new LSLString(s);
1151 }
1152
1153 public static string ToString(LSLString s)
1154 {
1155 return s.m_string;
1156 }
1157
1158 public override string ToString()
1159 {
1160 return m_string;
1161 }
1162
1163 public static bool operator ==(LSLString s1, string s2)
1164 {
1165 return s1.m_string == s2;
1166 }
1167
1168 public static bool operator !=(LSLString s1, string s2)
1169 {
1170 return s1.m_string != s2;
1171 }
1172
1173 public static explicit operator double(LSLString s)
1174 {
1175 return Convert.ToDouble(s.m_string);
1176 }
1177
1178 public static explicit operator LSLInteger(LSLString s)
1179 {
1180 return new LSLInteger(Convert.ToInt32(s.m_string));
1181 }
1182
1183 public static explicit operator LSLString(int i)
1184 {
1185 return new LSLString(i);
1186 }
1187
1188 public static explicit operator LSLString(double d)
1189 {
1190 return new LSLString(d);
1191 }
1192
1193 public static explicit operator LSLString(LSLFloat f)
1194 {
1195 return new LSLString(f);
1196 }
1197
1198 public static implicit operator Vector3(LSLString s)
1199 {
1200 return new Vector3(s.m_string);
1201 }
1202
1203 #endregion
1204
1205 #region Overriders
1206 public override bool Equals(object o)
1207 {
1208 return m_string == o.ToString();
1209 }
1210
1211 public override int GetHashCode()
1212 {
1213 return m_string.GetHashCode();
1214 }
1215
1216 #endregion
1217
1218 #region " Standard string functions "
1219 //Clone,CompareTo,Contains
1220 //CopyTo,EndsWith,Equals,GetEnumerator,GetHashCode,GetType,GetTypeCode
1221 //IndexOf,IndexOfAny,Insert,IsNormalized,LastIndexOf,LastIndexOfAny
1222 //Length,Normalize,PadLeft,PadRight,Remove,Replace,Split,StartsWith,Substring,ToCharArray,ToLowerInvariant
1223 //ToString,ToUpper,ToUpperInvariant,Trim,TrimEnd,TrimStart
1224 public bool Contains(string value) { return m_string.Contains(value); }
1225 public int IndexOf(string value) { return m_string.IndexOf(value); }
1226 public int Length { get { return m_string.Length; } }
1227
1228
1229 #endregion
1230 }
1231
1232 [Serializable]
1233 public struct LSLInteger
1234 {
1235 public int value;
1236
1237 #region Constructors
1238 public LSLInteger(int i)
1239 {
1240 value = i;
1241 }
1242
1243 public LSLInteger(double d)
1244 {
1245 value = (int)d;
1246 }
1247
1248 public LSLInteger(Object o)
1249 {
1250 if (!(o is Int32))
1251 value = 0;
1252 else
1253 value = (int)o;
1254 }
1255
1256 public LSLInteger(string s)
1257 {
1258 value = int.Parse(s);
1259 }
1260
1261 #endregion
1262
1263 #region Operators
1264
1265 static public implicit operator int(LSLInteger i)
1266 {
1267 return i.value;
1268 }
1269
1270 static public implicit operator uint(LSLInteger i)
1271 {
1272 return (uint)i.value;
1273 }
1274
1275 static public explicit operator LSLString(LSLInteger i)
1276 {
1277 return new LSLString(i.ToString());
1278 }
1279
1280 static public explicit operator string(LSLInteger i)
1281 {
1282 return i.ToString();
1283 }
1284
1285 static public implicit operator Boolean(LSLInteger i)
1286 {
1287 if (i.value == 0)
1288 {
1289 return false;
1290 }
1291 else
1292 {
1293 return true;
1294 }
1295 }
1296
1297 static public implicit operator LSLInteger(int i)
1298 {
1299 return new LSLInteger(i);
1300 }
1301
1302 static public explicit operator LSLInteger(string s)
1303 {
1304 return new LSLInteger(int.Parse(s));
1305 }
1306
1307 static public implicit operator LSLInteger(double d)
1308 {
1309 return new LSLInteger(d);
1310 }
1311
1312 static public bool operator ==(LSLInteger i1, LSLInteger i2)
1313 {
1314 bool ret = i1.value == i2.value;
1315 return ret;
1316 }
1317
1318 static public bool operator !=(LSLInteger i1, LSLInteger i2)
1319 {
1320 bool ret = i1.value != i2.value;
1321 return ret;
1322 }
1323
1324 static public LSLInteger operator &(LSLInteger i1, LSLInteger i2)
1325 {
1326 int ret = i1.value & i2.value;
1327 return ret;
1328 }
1329
1330 public static LSLInteger operator ++(LSLInteger i)
1331 {
1332 i.value++;
1333 return i;
1334 }
1335
1336
1337 public static LSLInteger operator --(LSLInteger i)
1338 {
1339 i.value--;
1340 return i;
1341 }
1342
1343 static public implicit operator System.Double(LSLInteger i)
1344 {
1345 return (double)i.value;
1346 }
1347
1348 static public implicit operator LSLFloat(LSLInteger i)
1349 {
1350 return new LSLFloat((double)i.value);
1351 }
1352
1353 public static bool operator true(LSLInteger i)
1354 {
1355 return i.value != 0;
1356 }
1357
1358 public static bool operator false(LSLInteger i)
1359 {
1360 return i.value == 0;
1361 }
1362
1363 #endregion
1364
1365 #region Overriders
1366
1367 public override string ToString()
1368 {
1369 return this.value.ToString();
1370 }
1371
1372 public override bool Equals(object o)
1373 {
1374 if (o is Int32)
1375 {
1376 return value == (Int32)o;
1377 }
1378 if (o is LSLInteger)
1379 {
1380 return value == ((LSLInteger)o).value;
1381 }
1382 return false;
1383 }
1384
1385 public override int GetHashCode()
1386 {
1387 return value.GetHashCode();
1388 }
1389
1390 #endregion
1391 }
1392
1393 [Serializable]
1394 public struct LSLFloat
1395 {
1396 public double value;
1397
1398 #region Constructors
1399
1400 public LSLFloat(int i)
1401 {
1402 value = (double)i;
1403 }
1404
1405 public LSLFloat(double d)
1406 {
1407 value = d;
1408 }
1409
1410 public LSLFloat(string s)
1411 {
1412 value = double.Parse(s);
1413 }
1414
1415 public LSLFloat(Object o)
1416 {
1417 if (!((o is double) || (o is float)))
1418 {
1419 value = 0.0;
1420 return;
1421 }
1422
1423 value = (double)o;
1424 }
1425
1426 #endregion
1427
1428 #region Operators
1429
1430 static public implicit operator int(LSLFloat f)
1431 {
1432 return (int)f.value;
1433 }
1434
1435 static public implicit operator uint(LSLFloat f)
1436 {
1437 return (uint) Math.Abs(f.value);
1438 }
1439
1440 static public implicit operator Boolean(LSLFloat f)
1441 {
1442 if (f.value == 0.0)
1443 {
1444 return false;
1445 }
1446 else
1447 {
1448 return true;
1449 }
1450 }
1451
1452 static public implicit operator LSLFloat(int i)
1453 {
1454 return new LSLFloat(i);
1455 }
1456
1457 static public implicit operator LSLFloat(string s)
1458 {
1459 return new LSLFloat(double.Parse(s));
1460 }
1461
1462 static public implicit operator LSLFloat(double d)
1463 {
1464 return new LSLFloat(d);
1465 }
1466
1467 static public bool operator ==(LSLFloat f1, LSLFloat f2)
1468 {
1469 return f1.value == f2.value;
1470 }
1471
1472 static public bool operator !=(LSLFloat f1, LSLFloat f2)
1473 {
1474 return f1.value != f2.value;
1475 }
1476
1477 public override bool Equals(Object o)
1478 {
1479 if(!(o is LSLFloat))
1480 return false;
1481
1482 return value == ((LSLFloat)o).value;
1483 }
1484
1485 public override int GetHashCode()
1486 {
1487 return (int)value;
1488 }
1489
1490 static public LSLFloat operator ++(LSLFloat f)
1491 {
1492 f.value++;
1493 return f;
1494 }
1495
1496 static public LSLFloat operator --(LSLFloat f)
1497 {
1498 f.value--;
1499 return f;
1500 }
1501
1502 static public implicit operator System.Double(LSLFloat f)
1503 {
1504 return f.value;
1505 }
1506
1507 #endregion
1508
1509 #region Overriders
1510
1511 public override string ToString()
1512 {
1513 return String.Format("{0:0.000000}", this.value);
1514 }
1515
1516 #endregion
1517 }
1518 }
1519}