opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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  
31 using System;
32 using System.Collections.Generic;
33  
34 namespace 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 /// If the list or one of the tails of the list is Variable, raise an instantiation_error.
136 /// This does not call YP.getValue on each element.
137 /// </summary>
138 /// <param name="list"></param>
139 /// <returns></returns>
140 public static object[] toArray(object list)
141 {
142 list = YP.getValue(list);
143 if (list.Equals(Atom.NIL))
144 return new object[0];
145  
146 List<object> result = new List<object>();
147 object element = list;
148 while (true) {
149 if (element == Atom.NIL)
150 break;
151 if (element is Variable)
152 throw new PrologException(Atom.a("instantiation_error"),
153 "List tail is an unbound variable");
154 if (!(element is Functor2 && ((Functor2)element)._name == Atom.DOT))
155 // Not a proper list.
156 return null;
157 result.Add(((Functor2)element)._arg1);
158 element = YP.getValue(((Functor2)element)._arg2);
159 }
160  
161 if (result.Count <= 0)
162 return null;
163 return result.ToArray();
164 }
165 }
166 }