PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/YieldProlog/Modules/FindallAnswers.cs

https://bitbucket.org/VirtualReality/optional-modules
C# | 105 lines | 46 code | 9 blank | 50 comment | 0 complexity | 139e826fd1d07649f6b06198650ea82b MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Aurora-Sim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
  31. {
  32. /// <summary>
  33. /// A FindallAnswers holds answers for findall.
  34. /// </summary>
  35. public class FindallAnswers
  36. {
  37. private object _template;
  38. private List<object> _bagArray;
  39. public FindallAnswers(object Template)
  40. {
  41. _template = Template;
  42. _bagArray = new List<object>();
  43. }
  44. public void add()
  45. {
  46. _bagArray.Add(YP.makeCopy(_template, new Variable.CopyStore()));
  47. }
  48. public List<object> resultArray()
  49. {
  50. return _bagArray;
  51. }
  52. /// <summary>
  53. /// Unify Bag with the result. This frees the internal answers, so you can only call this once.
  54. /// </summary>
  55. /// <param name="Bag"></param>
  56. /// <returns></returns>
  57. public IEnumerable<bool> result(object Bag)
  58. {
  59. object result = ListPair.make(_bagArray);
  60. // Try to free the memory.
  61. _bagArray = null;
  62. return YP.unify(Bag, result);
  63. }
  64. // disable warning on l1, don't see how we can
  65. // code this differently
  66. #pragma warning disable 0168, 0219
  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. foreach (bool l1 in goal)
  78. findallAnswers.add();
  79. return findallAnswers.result(Bag);
  80. }
  81. /// <summary>
  82. /// Like findall, except return an array of the results.
  83. /// </summary>
  84. /// <param name="template"></param>
  85. /// <param name="goal"></param>
  86. /// <returns></returns>
  87. public static List<object> findallArray(object Template, IEnumerable<bool> goal)
  88. {
  89. FindallAnswers findallAnswers = new FindallAnswers(Template);
  90. foreach (bool l1 in goal)
  91. findallAnswers.add();
  92. return findallAnswers.resultArray();
  93. }
  94. #pragma warning restore 0168, 0219
  95. }
  96. }