aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs156
1 files changed, 156 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs
new file mode 100644
index 0000000..83ace2d
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs
@@ -0,0 +1,156 @@
1/*
2 * Copyright (C) 2007-2008, Jeff Thompson
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31using System;
32using System.Collections.Generic;
33
34namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
35{
36 public class ListPair : Functor2
37 {
38 public ListPair(object head, object tail) : base(Atom.DOT, head, tail)
39 {
40 }
41
42 public static object make(List<object> list)
43 {
44 if (list.Count <= 0)
45 return Atom.NIL;
46
47 object result = Atom.NIL;
48 // Start from the end.
49 for (int i = list.Count - 1; i >= 0; --i)
50 result = new ListPair(list[i], result);
51 return result;
52 }
53
54 public static object make(object[] array)
55 {
56 if (array.Length <= 0)
57 return Atom.NIL;
58
59 object result = Atom.NIL;
60 // Start from the end.
61 for (int i = array.Length - 1; i >= 0; --i)
62 result = new ListPair(array[i], result);
63 return result;
64 }
65
66 /// <summary>
67 /// Return a ListPair version of array, where repeated elements
68 /// (according to YP.termEqual) are removed.
69 /// </summary>
70 /// <param name="array"></param>
71 /// <returns></returns>
72 public static object makeWithoutRepeatedTerms(object[] array)
73 {
74 if (array.Length <= 0)
75 return Atom.NIL;
76
77 // Start from the end.
78 object previousTerm = array[array.Length - 1];
79 object result = new ListPair(previousTerm, Atom.NIL);
80 for (int i = array.Length - 2; i >= 0; --i)
81 {
82 object term = array[i];
83 if (YP.termEqual(term, previousTerm))
84 continue;
85 result = new ListPair(term, result);
86 previousTerm = term;
87 }
88 return result;
89 }
90
91 /// <summary>
92 /// Return a ListPair version of array, where repeated elements
93 /// (according to YP.termEqual) are removed.
94 /// </summary>
95 /// <param name="array"></param>
96 /// <returns></returns>
97 public static object makeWithoutRepeatedTerms(List<object> array)
98 {
99 if (array.Count <= 0)
100 return Atom.NIL;
101
102 // Start from the end.
103 object previousTerm = array[array.Count - 1];
104 object result = new ListPair(previousTerm, Atom.NIL);
105 for (int i = array.Count - 2; i >= 0; --i)
106 {
107 object term = array[i];
108 if (YP.termEqual(term, previousTerm))
109 continue;
110 result = new ListPair(term, result);
111 previousTerm = term;
112 }
113 return result;
114 }
115
116 public static object make(object element1)
117 {
118 return new ListPair(element1, Atom.NIL);
119 }
120
121 public static object make(object element1, object element2)
122 {
123 return new ListPair(element1, new ListPair(element2, Atom.NIL));
124 }
125
126 public static object make(object element1, object element2, object element3)
127 {
128 return new ListPair(element1,
129 new ListPair(element2, new ListPair(element3, Atom.NIL)));
130 }
131
132 /// <summary>
133 /// Return an array of the elements in list or null if it is not
134 /// a proper list. If list is Atom.NIL, return an array of zero elements.
135 /// This does not call YP.getValue on each element.
136 /// </summary>
137 /// <param name="list"></param>
138 /// <returns></returns>
139 public static object[] toArray(object list)
140 {
141 list = YP.getValue(list);
142 if (list.Equals(Atom.NIL))
143 return new object[0];
144
145 List<object> result = new List<object>();
146 for (object element = list;
147 element is Functor2 && ((Functor2)element)._name == Atom.DOT;
148 element = YP.getValue(((Functor2)element)._arg2))
149 result.Add(((Functor2)element)._arg1);
150
151 if (result.Count <= 0)
152 return null;
153 return result.ToArray();
154 }
155 }
156}