PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/FindallAnswers.cs

https://bitbucket.org/KyanhaLLC/opensim
C# | 108 lines | 46 code | 9 blank | 53 comment | 0 complexity | 0a8f22dae6cba2cf43a65c989fadd699 MD5 | raw file
Possible License(s): Unlicense, MIT, BSD-3-Clause
  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. }