ListPair.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. using System;
  31. using System.Collections.Generic;
  32. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  33. {
  34. public class ListPair : Functor2
  35. {
  36. public ListPair(object head, object tail) : base(Atom.DOT, head, tail)
  37. {
  38. }
  39. public static object make(List<object> list)
  40. {
  41. if (list.Count <= 0)
  42. return Atom.NIL;
  43. object result = Atom.NIL;
  44. // Start from the end.
  45. for (int i = list.Count - 1; i >= 0; --i)
  46. result = new ListPair(list[i], result);
  47. return result;
  48. }
  49. public static object make(object[] array)
  50. {
  51. if (array.Length <= 0)
  52. return Atom.NIL;
  53. object result = Atom.NIL;
  54. // Start from the end.
  55. for (int i = array.Length - 1; i >= 0; --i)
  56. result = new ListPair(array[i], result);
  57. return result;
  58. }
  59. /// <summary>
  60. /// Return a ListPair version of array, where repeated elements
  61. /// (according to YP.termEqual) are removed.
  62. /// </summary>
  63. /// <param name="array"></param>
  64. /// <returns></returns>
  65. public static object makeWithoutRepeatedTerms(object[] array)
  66. {
  67. if (array.Length <= 0)
  68. return Atom.NIL;
  69. // Start from the end.
  70. object previousTerm = array[array.Length - 1];
  71. object result = new ListPair(previousTerm, Atom.NIL);
  72. for (int i = array.Length - 2; i >= 0; --i)
  73. {
  74. object term = array[i];
  75. if (YP.termEqual(term, previousTerm))
  76. continue;
  77. result = new ListPair(term, result);
  78. previousTerm = term;
  79. }
  80. return result;
  81. }
  82. /// <summary>
  83. /// Return a ListPair version of array, where repeated elements
  84. /// (according to YP.termEqual) are removed.
  85. /// </summary>
  86. /// <param name="array"></param>
  87. /// <returns></returns>
  88. public static object makeWithoutRepeatedTerms(List<object> array)
  89. {
  90. if (array.Count <= 0)
  91. return Atom.NIL;
  92. // Start from the end.
  93. object previousTerm = array[array.Count - 1];
  94. object result = new ListPair(previousTerm, Atom.NIL);
  95. for (int i = array.Count - 2; i >= 0; --i)
  96. {
  97. object term = array[i];
  98. if (YP.termEqual(term, previousTerm))
  99. continue;
  100. result = new ListPair(term, result);
  101. previousTerm = term;
  102. }
  103. return result;
  104. }
  105. public static object make(object element1)
  106. {
  107. return new ListPair(element1, Atom.NIL);
  108. }
  109. public static object make(object element1, object element2)
  110. {
  111. return new ListPair(element1, new ListPair(element2, Atom.NIL));
  112. }
  113. public static object make(object element1, object element2, object element3)
  114. {
  115. return new ListPair(element1,
  116. new ListPair(element2, new ListPair(element3, Atom.NIL)));
  117. }
  118. /// <summary>
  119. /// Return an array of the elements in list or null if it is not
  120. /// a proper list. If list is Atom.NIL, return an array of zero elements.
  121. /// If the list or one of the tails of the list is Variable, raise an instantiation_error.
  122. /// This does not call YP.getValue on each element.
  123. /// </summary>
  124. /// <param name="list"></param>
  125. /// <returns></returns>
  126. public static object[] toArray(object list)
  127. {
  128. list = YP.getValue(list);
  129. if (list.Equals(Atom.NIL))
  130. return new object[0];
  131. List<object> result = new List<object>();
  132. object element = list;
  133. while (true) {
  134. if (element == Atom.NIL)
  135. break;
  136. if (element is Variable)
  137. throw new PrologException(Atom.a("instantiation_error"),
  138. "List tail is an unbound variable");
  139. if (!(element is Functor2 && ((Functor2)element)._name == Atom.DOT))
  140. // Not a proper list.
  141. return null;
  142. result.Add(((Functor2)element)._arg1);
  143. element = YP.getValue(((Functor2)element)._arg2);
  144. }
  145. if (result.Count <= 0)
  146. return null;
  147. return result.ToArray();
  148. }
  149. }
  150. }