opensim /OpenSim/Region/ScriptEngine/Shared/Api/Runtime/YieldProlog/ListPair.cs

Language C# Lines 167
MD5 Hash 58cd7f4364f5f52e96601b91d5565591 Estimated Cost $2,307 (why?)
Repository https://bitbucket.org/KyanhaLLC/opensim.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2007-2008, Jeff Thompson
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;

namespace OpenSim.Region.ScriptEngine.Shared.YieldProlog
{
    public class ListPair : Functor2
    {
        public ListPair(object head, object tail) : base(Atom.DOT, head, tail)
        {
        }

        public static object make(List<object> list)
        {
            if (list.Count <= 0)
                return Atom.NIL;

            object result = Atom.NIL;
            // Start from the end.
            for (int i = list.Count - 1; i >= 0; --i)
                result = new ListPair(list[i], result);
            return result;
        }

        public static object make(object[] array)
        {
            if (array.Length <= 0)
                return Atom.NIL;

            object result = Atom.NIL;
            // Start from the end.
            for (int i = array.Length - 1; i >= 0; --i)
                result = new ListPair(array[i], result);
            return result;
        }

        /// <summary>
        /// Return a ListPair version of array, where repeated elements
        /// (according to YP.termEqual) are removed.
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static object makeWithoutRepeatedTerms(object[] array)
        {
            if (array.Length <= 0)
                return Atom.NIL;

            // Start from the end.
            object previousTerm = array[array.Length - 1];
            object result = new ListPair(previousTerm, Atom.NIL);
            for (int i = array.Length - 2; i >= 0; --i)
            {
                object term = array[i];
                if (YP.termEqual(term, previousTerm))
                    continue;
                result = new ListPair(term, result);
                previousTerm = term;
            }
            return result;
        }

        /// <summary>
        /// Return a ListPair version of array, where repeated elements
        /// (according to YP.termEqual) are removed.
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static object makeWithoutRepeatedTerms(List<object> array)
        {
            if (array.Count <= 0)
                return Atom.NIL;

            // Start from the end.
            object previousTerm = array[array.Count - 1];
            object result = new ListPair(previousTerm, Atom.NIL);
            for (int i = array.Count - 2; i >= 0; --i)
            {
                object term = array[i];
                if (YP.termEqual(term, previousTerm))
                    continue;
                result = new ListPair(term, result);
                previousTerm = term;
            }
            return result;
        }

        public static object make(object element1)
        {
            return new ListPair(element1, Atom.NIL);
        }

        public static object make(object element1, object element2)
        {
            return new ListPair(element1, new ListPair(element2, Atom.NIL));
        }

        public static object make(object element1, object element2, object element3)
        {
            return new ListPair(element1,
                new ListPair(element2, new ListPair(element3, Atom.NIL)));
        }

        /// <summary>
        /// Return an array of the elements in list or null if it is not
        /// a proper list.  If list is Atom.NIL, return an array of zero elements.
        /// If the list or one of the tails of the list is Variable, raise an instantiation_error.
        /// This does not call YP.getValue on each element.
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static object[] toArray(object list)
        {
            list = YP.getValue(list);
            if (list.Equals(Atom.NIL))
                return new object[0];

            List<object> result = new List<object>();
            object element = list;
            while (true) {
                if (element == Atom.NIL)
                    break;
                if (element is Variable)
                    throw new PrologException(Atom.a("instantiation_error"),
                        "List tail is an unbound variable");
                if (!(element is Functor2 && ((Functor2)element)._name == Atom.DOT))
                    // Not a proper list.
                    return null;
                result.Add(((Functor2)element)._arg1);
                element = YP.getValue(((Functor2)element)._arg2);
            }

            if (result.Count <= 0)
                return null;
            return result.ToArray();
        }
    }
}
Back to Top