FindallAnswers.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // disable warning on l1, don't see how we can
  68. // code this differently
  69. #pragma warning disable 0168, 0219
  70. /// <summary>
  71. /// This is a simplified findall when the goal is a single call.
  72. /// </summary>
  73. /// <param name="Template"></param>
  74. /// <param name="goal"></param>
  75. /// <param name="Bag"></param>
  76. /// <returns></returns>
  77. public static IEnumerable<bool> findall(object Template, IEnumerable<bool> goal, object Bag)
  78. {
  79. FindallAnswers findallAnswers = new FindallAnswers(Template);
  80. foreach (bool l1 in goal)
  81. findallAnswers.add();
  82. return findallAnswers.result(Bag);
  83. }
  84. /// <summary>
  85. /// Like findall, except return an array of the results.
  86. /// </summary>
  87. /// <param name="template"></param>
  88. /// <param name="goal"></param>
  89. /// <returns></returns>
  90. public static List<object> findallArray(object Template, IEnumerable<bool> goal)
  91. {
  92. FindallAnswers findallAnswers = new FindallAnswers(Template);
  93. foreach (bool l1 in goal)
  94. findallAnswers.add();
  95. return findallAnswers.resultArray();
  96. }
  97. #pragma warning restore 0168, 0219
  98. }
  99. }