LSL_Types.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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. using System;
  28. using System.Collections;
  29. using System.Text.RegularExpressions;
  30. namespace OpenSim.Region.ScriptEngine.Shared
  31. {
  32. [Serializable]
  33. public partial class LSL_Types
  34. {
  35. // Types are kept is separate .dll to avoid having to add whatever .dll it is in it to script AppDomain
  36. [Serializable]
  37. public struct Vector3
  38. {
  39. public double x;
  40. public double y;
  41. public double z;
  42. #region Constructors
  43. public Vector3(Vector3 vector)
  44. {
  45. x = (float)vector.x;
  46. y = (float)vector.y;
  47. z = (float)vector.z;
  48. }
  49. public Vector3(double X, double Y, double Z)
  50. {
  51. x = X;
  52. y = Y;
  53. z = Z;
  54. }
  55. public Vector3(string str)
  56. {
  57. str = str.Replace('<', ' ');
  58. str = str.Replace('>', ' ');
  59. string[] tmps = str.Split(new Char[] { ',', '<', '>' });
  60. if (tmps.Length < 3)
  61. {
  62. x=y=z=0;
  63. return;
  64. }
  65. bool res;
  66. res = Double.TryParse(tmps[0], out x);
  67. res = res & Double.TryParse(tmps[1], out y);
  68. res = res & Double.TryParse(tmps[2], out z);
  69. }
  70. #endregion
  71. #region Overriders
  72. public override string ToString()
  73. {
  74. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", x, y, z);
  75. return s;
  76. }
  77. public static explicit operator LSLString(Vector3 vec)
  78. {
  79. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  80. return new LSLString(s);
  81. }
  82. public static explicit operator string(Vector3 vec)
  83. {
  84. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000}>", vec.x, vec.y, vec.z);
  85. return s;
  86. }
  87. public static explicit operator Vector3(string s)
  88. {
  89. return new Vector3(s);
  90. }
  91. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  92. {
  93. return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
  94. }
  95. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  96. {
  97. return !(lhs == rhs);
  98. }
  99. public override int GetHashCode()
  100. {
  101. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode());
  102. }
  103. public override bool Equals(object o)
  104. {
  105. if (!(o is Vector3)) return false;
  106. Vector3 vector = (Vector3)o;
  107. return (x == vector.x && x == vector.x && z == vector.z);
  108. }
  109. #endregion
  110. #region Vector & Vector Math
  111. // Vector-Vector Math
  112. public static Vector3 operator +(Vector3 lhs, Vector3 rhs)
  113. {
  114. return new Vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  115. }
  116. public static Vector3 operator -(Vector3 lhs, Vector3 rhs)
  117. {
  118. return new Vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
  119. }
  120. public static Vector3 operator *(Vector3 lhs, Vector3 rhs)
  121. {
  122. return new Vector3(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z);
  123. }
  124. public static Vector3 operator %(Vector3 v1, Vector3 v2)
  125. {
  126. //Cross product
  127. Vector3 tv;
  128. tv.x = (v1.y * v2.z) - (v1.z * v2.y);
  129. tv.y = (v1.z * v2.x) - (v1.x * v2.z);
  130. tv.z = (v1.x * v2.y) - (v1.y * v2.x);
  131. return tv;
  132. }
  133. #endregion
  134. #region Vector & Float Math
  135. // Vector-Float and Float-Vector Math
  136. public static Vector3 operator *(Vector3 vec, float val)
  137. {
  138. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  139. }
  140. public static Vector3 operator *(float val, Vector3 vec)
  141. {
  142. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  143. }
  144. public static Vector3 operator /(Vector3 v, float f)
  145. {
  146. v.x = v.x / f;
  147. v.y = v.y / f;
  148. v.z = v.z / f;
  149. return v;
  150. }
  151. #endregion
  152. #region Vector & Double Math
  153. public static Vector3 operator *(Vector3 vec, double val)
  154. {
  155. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  156. }
  157. public static Vector3 operator *(double val, Vector3 vec)
  158. {
  159. return new Vector3(vec.x * val, vec.y * val, vec.z * val);
  160. }
  161. public static Vector3 operator /(Vector3 v, double f)
  162. {
  163. v.x = v.x / f;
  164. v.y = v.y / f;
  165. v.z = v.z / f;
  166. return v;
  167. }
  168. #endregion
  169. #region Vector & Rotation Math
  170. // Vector-Rotation Math
  171. public static Vector3 operator *(Vector3 v, Quaternion r)
  172. {
  173. Quaternion vq = new Quaternion(v.x, v.y, v.z, 0);
  174. Quaternion nq = new Quaternion(-r.x, -r.y, -r.z, r.s);
  175. // adapted for operator * computing "b * a"
  176. Quaternion result = nq * (vq * r);
  177. return new Vector3(result.x, result.y, result.z);
  178. }
  179. public static Vector3 operator /(Vector3 v, Quaternion r)
  180. {
  181. r.s = -r.s;
  182. return v * r;
  183. }
  184. #endregion
  185. #region Static Helper Functions
  186. public static double Dot(Vector3 v1, Vector3 v2)
  187. {
  188. return (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z);
  189. }
  190. public static Vector3 Cross(Vector3 v1, Vector3 v2)
  191. {
  192. return new Vector3
  193. (
  194. v1.y * v2.z - v1.z * v2.y,
  195. v1.z * v2.x - v1.x * v2.z,
  196. v1.x * v2.y - v1.y * v2.x
  197. );
  198. }
  199. public static double Mag(Vector3 v)
  200. {
  201. return Math.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  202. }
  203. public static Vector3 Norm(Vector3 vector)
  204. {
  205. double mag = Mag(vector);
  206. return new Vector3(vector.x / mag, vector.y / mag, vector.z / mag);
  207. }
  208. #endregion
  209. }
  210. [Serializable]
  211. public struct Quaternion
  212. {
  213. public double x;
  214. public double y;
  215. public double z;
  216. public double s;
  217. #region Constructors
  218. public Quaternion(Quaternion Quat)
  219. {
  220. x = (float)Quat.x;
  221. y = (float)Quat.y;
  222. z = (float)Quat.z;
  223. s = (float)Quat.s;
  224. if (x == 0 && y == 0 && z == 0 && s == 0)
  225. s = 1;
  226. }
  227. public Quaternion(double X, double Y, double Z, double S)
  228. {
  229. x = X;
  230. y = Y;
  231. z = Z;
  232. s = S;
  233. if (x == 0 && y == 0 && z == 0 && s == 0)
  234. s = 1;
  235. }
  236. public Quaternion(string str)
  237. {
  238. str = str.Replace('<', ' ');
  239. str = str.Replace('>', ' ');
  240. string[] tmps = str.Split(new Char[] { ',', '<', '>' });
  241. if (tmps.Length < 4)
  242. {
  243. x=y=z=s=0;
  244. return;
  245. }
  246. bool res;
  247. res = Double.TryParse(tmps[0], out x);
  248. res = res & Double.TryParse(tmps[1], out y);
  249. res = res & Double.TryParse(tmps[2], out z);
  250. res = res & Double.TryParse(tmps[3], out s);
  251. if (x == 0 && y == 0 && z == 0 && s == 0)
  252. s = 1;
  253. }
  254. #endregion
  255. #region Overriders
  256. public override int GetHashCode()
  257. {
  258. return (x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ s.GetHashCode());
  259. }
  260. public override bool Equals(object o)
  261. {
  262. if (!(o is Quaternion)) return false;
  263. Quaternion quaternion = (Quaternion)o;
  264. return x == quaternion.x && y == quaternion.y && z == quaternion.z && s == quaternion.s;
  265. }
  266. public override string ToString()
  267. {
  268. string st=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", x, y, z, s);
  269. return st;
  270. }
  271. public static explicit operator string(Quaternion r)
  272. {
  273. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  274. return s;
  275. }
  276. public static explicit operator LSLString(Quaternion r)
  277. {
  278. string s=String.Format("<{0:0.000000},{1:0.000000},{2:0.000000},{3:0.000000}>", r.x, r.y, r.z, r.s);
  279. return new LSLString(s);
  280. }
  281. public static explicit operator Quaternion(string s)
  282. {
  283. return new Quaternion(s);
  284. }
  285. public static bool operator ==(Quaternion lhs, Quaternion rhs)
  286. {
  287. // Return true if the fields match:
  288. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.s == rhs.s;
  289. }
  290. public static bool operator !=(Quaternion lhs, Quaternion rhs)
  291. {
  292. return !(lhs == rhs);
  293. }
  294. #endregion
  295. public static Quaternion operator +(Quaternion a, Quaternion b)
  296. {
  297. return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.s + b.s);
  298. }
  299. public static Quaternion operator /(Quaternion a, Quaternion b)
  300. {
  301. b.s = -b.s;
  302. return a * b;
  303. }
  304. public static Quaternion operator -(Quaternion a, Quaternion b)
  305. {
  306. return new Quaternion(a.x - b.x, a.y - b.y, a.z - b.z, a.s - b.s);
  307. }
  308. // using the equations below, we need to do "b * a" to be compatible with LSL
  309. public static Quaternion operator *(Quaternion b, Quaternion a)
  310. {
  311. Quaternion c;
  312. c.x = a.s * b.x + a.x * b.s + a.y * b.z - a.z * b.y;
  313. c.y = a.s * b.y + a.y * b.s + a.z * b.x - a.x * b.z;
  314. c.z = a.s * b.z + a.z * b.s + a.x * b.y - a.y * b.x;
  315. c.s = a.s * b.s - a.x * b.x - a.y * b.y - a.z * b.z;
  316. return c;
  317. }
  318. }
  319. [Serializable]
  320. public class list
  321. {
  322. private object[] m_data;
  323. public list(params object[] args)
  324. {
  325. m_data = new object[args.Length];
  326. m_data = args;
  327. }
  328. public int Length
  329. {
  330. get {
  331. if (m_data == null)
  332. m_data=new Object[0];
  333. return m_data.Length;
  334. }
  335. }
  336. public object[] Data
  337. {
  338. get {
  339. if (m_data == null)
  340. m_data=new Object[0];
  341. return m_data;
  342. }
  343. }
  344. public static list operator +(list a, list b)
  345. {
  346. object[] tmp;
  347. tmp = new object[a.Length + b.Length];
  348. a.Data.CopyTo(tmp, 0);
  349. b.Data.CopyTo(tmp, a.Length);
  350. return new list(tmp);
  351. }
  352. private void ExtendAndAdd(object o)
  353. {
  354. Array.Resize(ref m_data, Length + 1);
  355. m_data.SetValue(o, Length - 1);
  356. }
  357. public static list operator +(list a, string s)
  358. {
  359. a.ExtendAndAdd(s);
  360. return a;
  361. }
  362. public static list operator +(list a, int i)
  363. {
  364. a.ExtendAndAdd(i);
  365. return a;
  366. }
  367. public static list operator +(list a, double d)
  368. {
  369. a.ExtendAndAdd(d);
  370. return a;
  371. }
  372. public void Add(object o)
  373. {
  374. object[] tmp;
  375. tmp = new object[m_data.Length + 1];
  376. m_data.CopyTo(tmp, 0);
  377. tmp[m_data.Length] = o;
  378. m_data = tmp;
  379. }
  380. public bool Contains(object o)
  381. {
  382. bool ret = false;
  383. foreach (object i in Data)
  384. {
  385. if (i == o)
  386. {
  387. ret = true;
  388. break;
  389. }
  390. }
  391. return ret;
  392. }
  393. public list DeleteSublist(int start, int end)
  394. {
  395. // Not an easy one
  396. // If start <= end, remove that part
  397. // if either is negative, count from the end of the array
  398. // if the resulting start > end, remove all BUT that part
  399. Object[] ret;
  400. if (start < 0)
  401. start=m_data.Length-start;
  402. if (start < 0)
  403. start=0;
  404. if (end < 0)
  405. end=m_data.Length-end;
  406. if (end < 0)
  407. end=0;
  408. if (start > end)
  409. {
  410. if (end >= m_data.Length)
  411. return new list(new Object[0]);
  412. if (start >= m_data.Length)
  413. start=m_data.Length-1;
  414. return GetSublist(end, start);
  415. }
  416. // start >= 0 && end >= 0 here
  417. if (start >= m_data.Length)
  418. {
  419. ret=new Object[m_data.Length];
  420. Array.Copy(m_data, 0, ret, 0, m_data.Length);
  421. return new list(ret);
  422. }
  423. if (end >= m_data.Length)
  424. end=m_data.Length-1;
  425. // now, this makes the math easier
  426. int remove=end+1-start;
  427. ret=new Object[m_data.Length-remove];
  428. if (ret.Length == 0)
  429. return new list(ret);
  430. int src;
  431. int dest=0;
  432. for (src = 0; src < m_data.Length; src++)
  433. {
  434. if (src < start || src > end)
  435. ret[dest++]=m_data[src];
  436. }
  437. return new list(ret);
  438. }
  439. public list GetSublist(int start, int end)
  440. {
  441. object[] ret;
  442. // Take care of neg start or end's
  443. // NOTE that either index may still be negative after
  444. // adding the length, so we must take additional
  445. // measures to protect against this. Note also that
  446. // after normalisation the negative indices are no
  447. // longer relative to the end of the list.
  448. if (start < 0)
  449. {
  450. start = m_data.Length + start;
  451. }
  452. if (end < 0)
  453. {
  454. end = m_data.Length + end;
  455. }
  456. // The conventional case is start <= end
  457. // NOTE that the case of an empty list is
  458. // dealt with by the initial test. Start
  459. // less than end is taken to be the most
  460. // common case.
  461. if (start <= end)
  462. {
  463. // Start sublist beyond length
  464. // Also deals with start AND end still negative
  465. if (start >= m_data.Length || end < 0)
  466. {
  467. return new list();
  468. }
  469. // Sublist extends beyond the end of the supplied list
  470. if (end >= m_data.Length)
  471. {
  472. end = m_data.Length - 1;
  473. }
  474. // Sublist still starts before the beginning of the list
  475. if (start < 0)
  476. {
  477. start = 0;
  478. }
  479. ret = new object[end - start + 1];
  480. Array.Copy(m_data, start, ret, 0, end - start + 1);
  481. return new list(ret);
  482. }
  483. // Deal with the segmented case: 0->end + start->EOL
  484. else
  485. {
  486. list result = null;
  487. // If end is negative, then prefix list is empty
  488. if (end < 0)
  489. {
  490. result = new list();
  491. // If start is still negative, then the whole of
  492. // the existing list is returned. This case is
  493. // only admitted if end is also still negative.
  494. if (start < 0)
  495. {
  496. return this;
  497. }
  498. }
  499. else
  500. {
  501. result = GetSublist(0,end);
  502. }
  503. // If start is outside of list, then just return
  504. // the prefix, whatever it is.
  505. if (start >= m_data.Length)
  506. {
  507. return result;
  508. }
  509. return result + GetSublist(start, Data.Length);
  510. }
  511. }
  512. public list Sort(int stride, int ascending)
  513. {
  514. if (Data.Length == 0)
  515. return new list(); // Don't even bother
  516. string[] keys;
  517. if (stride == 1) // The simple case
  518. {
  519. Object[] ret=new Object[Data.Length];
  520. Array.Copy(Data, 0, ret, 0, Data.Length);
  521. keys=new string[Data.Length];
  522. for (int k = 0; k < Data.Length; k++)
  523. keys[k] = Data[k].ToString();
  524. Array.Sort(keys, ret);
  525. if (ascending == 0)
  526. Array.Reverse(ret);
  527. return new list(ret);
  528. }
  529. int src=0;
  530. int len=(Data.Length+stride-1)/stride;
  531. keys=new string[len];
  532. Object[][] vals=new Object[len][];
  533. int i;
  534. while (src < Data.Length)
  535. {
  536. Object[] o=new Object[stride];
  537. for (i = 0; i < stride; i++)
  538. {
  539. if (src < Data.Length)
  540. o[i]=Data[src++];
  541. else
  542. {
  543. o[i]=new Object();
  544. src++;
  545. }
  546. }
  547. int idx=src/stride-1;
  548. keys[idx]=o[0].ToString();
  549. vals[idx]=o;
  550. }
  551. Array.Sort(keys, vals);
  552. if (ascending == 0)
  553. {
  554. Array.Reverse(vals);
  555. }
  556. Object[] sorted=new Object[stride*vals.Length];
  557. for (i = 0; i < vals.Length; i++)
  558. for (int j = 0; j < stride; j++)
  559. sorted[i*stride+j] = vals[i][j];
  560. return new list(sorted);
  561. }
  562. #region CSV Methods
  563. public static list FromCSV(string csv)
  564. {
  565. return new list(csv.Split(','));
  566. }
  567. public string ToCSV()
  568. {
  569. string ret = "";
  570. foreach (object o in this.Data)
  571. {
  572. if (ret == "")
  573. {
  574. ret = o.ToString();
  575. }
  576. else
  577. {
  578. ret = ret + ", " + o.ToString();
  579. }
  580. }
  581. return ret;
  582. }
  583. private string ToSoup()
  584. {
  585. string output;
  586. output = String.Empty;
  587. if (m_data.Length == 0)
  588. {
  589. return String.Empty;
  590. }
  591. foreach (object o in m_data)
  592. {
  593. output = output + o.ToString();
  594. }
  595. return output;
  596. }
  597. public static explicit operator String(list l)
  598. {
  599. return l.ToSoup();
  600. }
  601. public static explicit operator LSLString(list l)
  602. {
  603. return new LSLString(l.ToSoup());
  604. }
  605. public override string ToString()
  606. {
  607. return ToSoup();
  608. }
  609. #endregion
  610. #region Statistic Methods
  611. public double Min()
  612. {
  613. double minimum = double.PositiveInfinity;
  614. double entry;
  615. for (int i = 0; i < Data.Length; i++)
  616. {
  617. if (double.TryParse(Data[i].ToString(), out entry))
  618. {
  619. if (entry < minimum) minimum = entry;
  620. }
  621. }
  622. return minimum;
  623. }
  624. public double Max()
  625. {
  626. double maximum = double.NegativeInfinity;
  627. double entry;
  628. for (int i = 0; i < Data.Length; i++)
  629. {
  630. if (double.TryParse(Data[i].ToString(), out entry))
  631. {
  632. if (entry > maximum) maximum = entry;
  633. }
  634. }
  635. return maximum;
  636. }
  637. public double Range()
  638. {
  639. return (this.Max() / this.Min());
  640. }
  641. public int NumericLength()
  642. {
  643. int count = 0;
  644. double entry;
  645. for (int i = 0; i < Data.Length; i++)
  646. {
  647. if (double.TryParse(Data[i].ToString(), out entry))
  648. {
  649. count++;
  650. }
  651. }
  652. return count;
  653. }
  654. public static list ToDoubleList(list src)
  655. {
  656. list ret = new list();
  657. double entry;
  658. for (int i = 0; i < src.Data.Length - 1; i++)
  659. {
  660. if (double.TryParse(src.Data[i].ToString(), out entry))
  661. {
  662. ret.Add(entry);
  663. }
  664. }
  665. return ret;
  666. }
  667. public double Sum()
  668. {
  669. double sum = 0;
  670. double entry;
  671. for (int i = 0; i < Data.Length; i++)
  672. {
  673. if (double.TryParse(Data[i].ToString(), out entry))
  674. {
  675. sum = sum + entry;
  676. }
  677. }
  678. return sum;
  679. }
  680. public double SumSqrs()
  681. {
  682. double sum = 0;
  683. double entry;
  684. for (int i = 0; i < Data.Length; i++)
  685. {
  686. if (double.TryParse(Data[i].ToString(), out entry))
  687. {
  688. sum = sum + Math.Pow(entry, 2);
  689. }
  690. }
  691. return sum;
  692. }
  693. public double Mean()
  694. {
  695. return (this.Sum() / this.NumericLength());
  696. }
  697. public void NumericSort()
  698. {
  699. IComparer Numeric = new NumericComparer();
  700. Array.Sort(Data, Numeric);
  701. }
  702. public void AlphaSort()
  703. {
  704. IComparer Alpha = new AlphaCompare();
  705. Array.Sort(Data, Alpha);
  706. }
  707. public double Median()
  708. {
  709. return Qi(0.5);
  710. }
  711. public double GeometricMean()
  712. {
  713. double ret = 1.0;
  714. list nums = ToDoubleList(this);
  715. for (int i = 0; i < nums.Data.Length; i++)
  716. {
  717. ret *= (double)nums.Data[i];
  718. }
  719. return Math.Exp(Math.Log(ret) / (double)nums.Data.Length);
  720. }
  721. public double HarmonicMean()
  722. {
  723. double ret = 0.0;
  724. list nums = ToDoubleList(this);
  725. for (int i = 0; i < nums.Data.Length; i++)
  726. {
  727. ret += 1.0 / (double)nums.Data[i];
  728. }
  729. return ((double)nums.Data.Length / ret);
  730. }
  731. public double Variance()
  732. {
  733. double s = 0;
  734. list num = ToDoubleList(this);
  735. for (int i = 0; i < num.Data.Length; i++)
  736. {
  737. s += Math.Pow((double)num.Data[i], 2);
  738. }
  739. return (s - num.Data.Length * Math.Pow(num.Mean(), 2)) / (num.Data.Length - 1);
  740. }
  741. public double StdDev()
  742. {
  743. return Math.Sqrt(this.Variance());
  744. }
  745. public double Qi(double i)
  746. {
  747. list j = this;
  748. j.NumericSort();
  749. if (Math.Ceiling(this.Length * i) == this.Length * i)
  750. {
  751. return (double)((double)j.Data[(int)(this.Length * i - 1)] + (double)j.Data[(int)(this.Length * i)]) / 2;
  752. }
  753. else
  754. {
  755. return (double)j.Data[((int)(Math.Ceiling(this.Length * i))) - 1];
  756. }
  757. }
  758. #endregion
  759. public string ToPrettyString()
  760. {
  761. string output;
  762. if (m_data.Length == 0)
  763. {
  764. return "[]";
  765. }
  766. output = "[";
  767. foreach (object o in m_data)
  768. {
  769. if (o is String)
  770. {
  771. output = output + "\"" + o + "\", ";
  772. }
  773. else
  774. {
  775. output = output + o.ToString() + ", ";
  776. }
  777. }
  778. output = output.Substring(0, output.Length - 2);
  779. output = output + "]";
  780. return output;
  781. }
  782. public class AlphaCompare : IComparer
  783. {
  784. int IComparer.Compare(object x, object y)
  785. {
  786. return string.Compare(x.ToString(), y.ToString());
  787. }
  788. }
  789. public class NumericComparer : IComparer
  790. {
  791. int IComparer.Compare(object x, object y)
  792. {
  793. double a;
  794. double b;
  795. if (!double.TryParse(x.ToString(), out a))
  796. {
  797. a = 0.0;
  798. }
  799. if (!double.TryParse(y.ToString(), out b))
  800. {
  801. b = 0.0;
  802. }
  803. if (a < b)
  804. {
  805. return -1;
  806. }
  807. else if (a == b)
  808. {
  809. return 0;
  810. }
  811. else
  812. {
  813. return 1;
  814. }
  815. }
  816. }
  817. }
  818. //
  819. // BELOW IS WORK IN PROGRESS... IT WILL CHANGE, SO DON'T USE YET! :)
  820. //
  821. // public struct StringTest
  822. // {
  823. // // Our own little string
  824. // internal string actualString;
  825. // public static implicit operator bool(StringTest mString)
  826. // {
  827. // if (mString.actualString.Length == 0)
  828. // return true;
  829. // return false;
  830. // }
  831. // public override string ToString()
  832. // {
  833. // return actualString;
  834. // }
  835. //
  836. // }
  837. [Serializable]
  838. public struct key
  839. {
  840. public string value;
  841. #region Constructors
  842. public key(string s)
  843. {
  844. value = s;
  845. }
  846. #endregion
  847. #region Methods
  848. static public bool Parse2Key(string s)
  849. {
  850. 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);
  851. if (isuuid.IsMatch(s))
  852. {
  853. return true;
  854. }
  855. else
  856. {
  857. return false;
  858. }
  859. }
  860. #endregion
  861. #region Operators
  862. static public implicit operator Boolean(key k)
  863. {
  864. if (k.value.Length == 0)
  865. {
  866. return false;
  867. }
  868. if (k.value == "00000000-0000-0000-0000-000000000000")
  869. {
  870. return false;
  871. }
  872. 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);
  873. if (isuuid.IsMatch(k.value))
  874. {
  875. return true;
  876. }
  877. else
  878. {
  879. return false;
  880. }
  881. }
  882. static public implicit operator key(string s)
  883. {
  884. return new key(s);
  885. }
  886. static public implicit operator String(key k)
  887. {
  888. return k.value;
  889. }
  890. public static bool operator ==(key k1, key k2)
  891. {
  892. return k1.value == k2.value;
  893. }
  894. public static bool operator !=(key k1, key k2)
  895. {
  896. return k1.value != k2.value;
  897. }
  898. #endregion
  899. #region Overriders
  900. public override bool Equals(object o)
  901. {
  902. return o.ToString() == value;
  903. }
  904. public override int GetHashCode()
  905. {
  906. return value.GetHashCode();
  907. }
  908. #endregion
  909. }
  910. [Serializable]
  911. public struct LSLString
  912. {
  913. public string m_string;
  914. #region Constructors
  915. public LSLString(string s)
  916. {
  917. m_string = s;
  918. }
  919. public LSLString(int i)
  920. {
  921. m_string=i.ToString();
  922. }
  923. public LSLString(double d)
  924. {
  925. string s=String.Format("{0:0.000000}", d);
  926. m_string=s;
  927. }
  928. public LSLString(LSLFloat f)
  929. {
  930. string s=String.Format("{0:0.000000}", f.value);
  931. m_string=s;
  932. }
  933. #endregion
  934. #region Operators
  935. static public implicit operator Boolean(LSLString s)
  936. {
  937. if (s.m_string.Length == 0)
  938. {
  939. return false;
  940. }
  941. else
  942. {
  943. return true;
  944. }
  945. }
  946. static public implicit operator String(LSLString s)
  947. {
  948. return s.m_string;
  949. }
  950. static public implicit operator LSLString(string s)
  951. {
  952. return new LSLString(s);
  953. }
  954. public static string ToString(LSLString s)
  955. {
  956. return s.m_string;
  957. }
  958. public override string ToString()
  959. {
  960. return m_string;
  961. }
  962. public static bool operator ==(LSLString s1, string s2)
  963. {
  964. return s1.m_string == s2;
  965. }
  966. public static bool operator !=(LSLString s1, string s2)
  967. {
  968. return s1.m_string != s2;
  969. }
  970. public static explicit operator double(LSLString s)
  971. {
  972. return Convert.ToDouble(s.m_string);
  973. }
  974. public static explicit operator LSLInteger(LSLString s)
  975. {
  976. return new LSLInteger(Convert.ToInt32(s.m_string));
  977. }
  978. public static explicit operator LSLString(int i)
  979. {
  980. return new LSLString(i);
  981. }
  982. public static explicit operator LSLString(double d)
  983. {
  984. return new LSLString(d);
  985. }
  986. public static explicit operator LSLString(LSLFloat f)
  987. {
  988. return new LSLString(f);
  989. }
  990. public static implicit operator Vector3(LSLString s)
  991. {
  992. return new Vector3(s.m_string);
  993. }
  994. #endregion
  995. #region Overriders
  996. public override bool Equals(object o)
  997. {
  998. return m_string == o.ToString();
  999. }
  1000. public override int GetHashCode()
  1001. {
  1002. return m_string.GetHashCode();
  1003. }
  1004. #endregion
  1005. #region " Standard string functions "
  1006. //Clone,CompareTo,Contains
  1007. //CopyTo,EndsWith,Equals,GetEnumerator,GetHashCode,GetType,GetTypeCode
  1008. //IndexOf,IndexOfAny,Insert,IsNormalized,LastIndexOf,LastIndexOfAny
  1009. //Length,Normalize,PadLeft,PadRight,Remove,Replace,Split,StartsWith,Substring,ToCharArray,ToLowerInvariant
  1010. //ToString,ToUpper,ToUpperInvariant,Trim,TrimEnd,TrimStart
  1011. public bool Contains(string value) { return m_string.Contains(value); }
  1012. public int IndexOf(string value) { return m_string.IndexOf(value); }
  1013. public int Length { get { return m_string.Length; } }
  1014. #endregion
  1015. }
  1016. [Serializable]
  1017. public struct LSLInteger
  1018. {
  1019. public int value;
  1020. #region Constructors
  1021. public LSLInteger(int i)
  1022. {
  1023. value = i;
  1024. }
  1025. public LSLInteger(double d)
  1026. {
  1027. value = (int)d;
  1028. }
  1029. public LSLInteger(Object o)
  1030. {
  1031. if (!(o is Int32))
  1032. value = 0;
  1033. else
  1034. value = (int)o;
  1035. }
  1036. public LSLInteger(string s)
  1037. {
  1038. value = int.Parse(s);
  1039. }
  1040. #endregion
  1041. #region Operators
  1042. static public implicit operator int(LSLInteger i)
  1043. {
  1044. return i.value;
  1045. }
  1046. static public implicit operator uint(LSLInteger i)
  1047. {
  1048. return (uint)i.value;
  1049. }
  1050. static public explicit operator LSLString(LSLInteger i)
  1051. {
  1052. return new LSLString(i.ToString());
  1053. }
  1054. static public explicit operator string(LSLInteger i)
  1055. {
  1056. return i.ToString();
  1057. }
  1058. static public implicit operator Boolean(LSLInteger i)
  1059. {
  1060. if (i.value == 0)
  1061. {
  1062. return false;
  1063. }
  1064. else
  1065. {
  1066. return true;
  1067. }
  1068. }
  1069. static public implicit operator LSLInteger(int i)
  1070. {
  1071. return new LSLInteger(i);
  1072. }
  1073. static public explicit operator LSLInteger(string s)
  1074. {
  1075. return new LSLInteger(int.Parse(s));
  1076. }
  1077. static public implicit operator LSLInteger(double d)
  1078. {
  1079. return new LSLInteger(d);
  1080. }
  1081. static public bool operator ==(LSLInteger i1, LSLInteger i2)
  1082. {
  1083. bool ret = i1.value == i2.value;
  1084. return ret;
  1085. }
  1086. static public bool operator !=(LSLInteger i1, LSLInteger i2)
  1087. {
  1088. bool ret = i1.value != i2.value;
  1089. return ret;
  1090. }
  1091. static public LSLInteger operator &(LSLInteger i1, LSLInteger i2)
  1092. {
  1093. int ret = i1.value & i2.value;
  1094. return ret;
  1095. }
  1096. public static LSLInteger operator ++(LSLInteger i)
  1097. {
  1098. i.value++;
  1099. return i;
  1100. }
  1101. public static LSLInteger operator --(LSLInteger i)
  1102. {
  1103. i.value--;
  1104. return i;
  1105. }
  1106. static public implicit operator System.Double(LSLInteger i)
  1107. {
  1108. return (double)i.value;
  1109. }
  1110. static public implicit operator LSLFloat(LSLInteger i)
  1111. {
  1112. return new LSLFloat((double)i.value);
  1113. }
  1114. public static bool operator true(LSLInteger i)
  1115. {
  1116. return i.value != 0;
  1117. }
  1118. public static bool operator false(LSLInteger i)
  1119. {
  1120. return i.value == 0;
  1121. }
  1122. #endregion
  1123. #region Overriders
  1124. public override string ToString()
  1125. {
  1126. return this.value.ToString();
  1127. }
  1128. public override bool Equals(object o)
  1129. {
  1130. if (o is Int32)
  1131. {
  1132. return value == (Int32)o;
  1133. }
  1134. if (o is LSLInteger)
  1135. {
  1136. return value == ((LSLInteger)o).value;
  1137. }
  1138. return false;
  1139. }
  1140. public override int GetHashCode()
  1141. {
  1142. return value.GetHashCode();
  1143. }
  1144. #endregion
  1145. }
  1146. [Serializable]
  1147. public struct LSLFloat
  1148. {
  1149. public double value;
  1150. #region Constructors
  1151. public LSLFloat(int i)
  1152. {
  1153. value = (double)i;
  1154. }
  1155. public LSLFloat(double d)
  1156. {
  1157. value = d;
  1158. }
  1159. public LSLFloat(string s)
  1160. {
  1161. value = double.Parse(s);
  1162. }
  1163. public LSLFloat(Object o)
  1164. {
  1165. if (!((o is double) || (o is float)))
  1166. {
  1167. value = 0.0;
  1168. return;
  1169. }
  1170. value = (double)o;
  1171. }
  1172. #endregion
  1173. #region Operators
  1174. static public implicit operator int(LSLFloat f)
  1175. {
  1176. return (int)f.value;
  1177. }
  1178. static public implicit operator uint(LSLFloat f)
  1179. {
  1180. return (uint) Math.Abs(f.value);
  1181. }
  1182. static public implicit operator Boolean(LSLFloat f)
  1183. {
  1184. if (f.value == 0.0)
  1185. {
  1186. return false;
  1187. }
  1188. else
  1189. {
  1190. return true;
  1191. }
  1192. }
  1193. static public implicit operator LSLFloat(int i)
  1194. {
  1195. return new LSLFloat(i);
  1196. }
  1197. static public implicit operator LSLFloat(string s)
  1198. {
  1199. return new LSLFloat(double.Parse(s));
  1200. }
  1201. static public implicit operator LSLFloat(double d)
  1202. {
  1203. return new LSLFloat(d);
  1204. }
  1205. static public bool operator ==(LSLFloat f1, LSLFloat f2)
  1206. {
  1207. return f1.value == f2.value;
  1208. }
  1209. static public bool operator !=(LSLFloat f1, LSLFloat f2)
  1210. {
  1211. return f1.value != f2.value;
  1212. }
  1213. public override bool Equals(Object o)
  1214. {
  1215. if (!(o is LSLFloat))
  1216. return false;
  1217. return value == ((LSLFloat)o).value;
  1218. }
  1219. public override int GetHashCode()
  1220. {
  1221. return (int)value;
  1222. }
  1223. static public LSLFloat operator ++(LSLFloat f)
  1224. {
  1225. f.value++;
  1226. return f;
  1227. }
  1228. static public LSLFloat operator --(LSLFloat f)
  1229. {
  1230. f.value--;
  1231. return f;
  1232. }
  1233. static public implicit operator System.Double(LSLFloat f)
  1234. {
  1235. return f.value;
  1236. }
  1237. #endregion
  1238. #region Overriders
  1239. public override string ToString()
  1240. {
  1241. return String.Format("{0:0.000000}", this.value);
  1242. }
  1243. #endregion
  1244. }
  1245. }
  1246. }