FindallAnswers.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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;
  32. using System.Collections.Generic;
  33. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  34. {
  35. /// <summary>
  36. /// A FindallAnswers holds answers for findall.
  37. /// </summary>
  38. public class FindallAnswers
  39. {
  40. private object _template;
  41. private List<object> _bagArray;
  42. public FindallAnswers(object Template)
  43. {
  44. _template = Template;
  45. _bagArray = new List<object>();
  46. }
  47. public void add()
  48. {
  49. _bagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
  50. }
  51. public List<object> resultArray()
  52. {
  53. return _bagArray;
  54. }
  55. /// <summary>
  56. /// Unify Bag with the result. This frees the internal answers, so you can only call this once.
  57. /// </summary>
  58. /// <param name="Bag"></param>
  59. /// <returns></returns>
  60. public IEnumerable<bool> result(object Bag)
  61. {
  62. object result = ListPair.make(_bagArray);
  63. // Try to free the memory.
  64. _bagArray = null;
  65. return YP.unify(Bag, result);
  66. }
  67. /// <summary>
  68. /// This is a simplified findall when the goal is a single call.
  69. /// </summary>
  70. /// <param name="Template"></param>
  71. /// <param name="goal"></param>
  72. /// <param name="Bag"></param>
  73. /// <returns></returns>
  74. public static IEnumerable<bool> findall(object Template, IEnumerable<bool> goal, object Bag)
  75. {
  76. FindallAnswers findallAnswers = new FindallAnswers(Template);
  77. // disable warning on l1, don't see how we can
  78. // code this differently
  79. #pragma warning disable 0168
  80. foreach (bool l1 in goal)
  81. findallAnswers.add();
  82. #pragma warning restore 0168
  83. return findallAnswers.result(Bag);
  84. }
  85. /// <summary>
  86. /// Like findall, except return an array of the results.
  87. /// </summary>
  88. /// <param name="template"></param>
  89. /// <param name="goal"></param>
  90. /// <returns></returns>
  91. public static List<object> findallArray(object Template, IEnumerable<bool> goal)
  92. {
  93. FindallAnswers findallAnswers = new FindallAnswers(Template);
  94. // disable warning on l1, don't see how we can
  95. // code this differently
  96. #pragma warning disable 0168
  97. foreach (bool l1 in goal)
  98. findallAnswers.add();
  99. #pragma warning restore 0168
  100. return findallAnswers.resultArray();
  101. }
  102. }
  103. }