Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 302 lines (272 sloc) 8.392 kb
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
/*
* Copyright (c) 2004-2008 The University of Wroclaw.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 3. The name of the University may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``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 UNIVERSITY 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.
*/

namespace Nemerle.Collections {
  using Nemerle.Assertions;

  /**
* Doubly linked mutable list.
*
* Insert and Remove operations on this list require constant time irrespective of whether it is
* a single item or another LinkedList object, that is added.
*/
  public class LinkedList ['a] : System.Collections.Generic.LinkedList['a],
    ICollection['a],
    System.IEquatable[LinkedList['a]]
  {
    public this () {
      base ()
    }
    
    /** Constructor initiliasing object with contents of a Nemerle list. */
    public this (some_list : list ['a])
    {
      unless (some_list == null)
        foreach (item in some_list)
          _ = AddLast (item);
    }

    concat_helper (separator : string, sb : System.Text.StringBuilder) : void
    {
      unless (IsEmpty) {
        def e = GetEnumerator ();
        _ = e.MoveNext ();
        _ = sb.Append (e.Current);
        while (e.MoveNext ()) {
          _ = sb.Append (separator);
          _ = sb.Append (e.Current);
        }
      }
    }

    /** Returns string representing contents of the list. */
    public override ToString () : string
    {
      def sb = System.Text.StringBuilder ("[");
      concat_helper (", ", sb);
      sb.Append ("]").ToString ();
    }

    /** Constructs string out of list contents using given argument as a separator.
* <param name="separator">String to use a separator - it will be put between each
* two items of the list.</param>
*/
    public ToString (separator : string) : string
    {
      def sb = System.Text.StringBuilder ();
      concat_helper (separator, sb);
      sb.ToString ();
    }

    /** Compares two lists item by item using Equals method of contained objects. */
[Nemerle.OverrideObjectEquals]
    public Equals (another_list : LinkedList ['a]) : bool
      implements System.IEquatable[LinkedList['a]].Equals
    {
      def e = GetEnumerator ();
      def f = another_list.GetEnumerator ();

      def compare () : bool {
        def rete = e.MoveNext ();
        def retf = f.MoveNext ();
        if (rete != retf)
          false;
        else
          if (rete == true) // there is something to compare
            if (e.Current.Equals (f.Current))
              compare ();
            else
              false;
          else // everything has been compared
            true;
      }

      compare ();
    }

    /** Reverses elements of the list. Complexity is O(n). */
    public Reverse () : void
    {
      unless (IsEmpty) {
        mutable current = Last.Previous;
        while (current != null) {
          _ = AddLast (current.Value);
          def prev = current.Previous;
          Remove (current);
          current = prev;
        }
      }
    }

    /** Adds item at the beginning of the list. */
    public Prepend (item : 'a) : void
    {
      _ = AddFirst (item)
    }

    /** Add given list at the beginning. The source will be cleared. */
    public Prepend ([NotNull] l : LinkedList ['a]) : void
    {
      unless (l.IsEmpty) {
        def enu = l.GetEnumerator ();
        assert(enu.MoveNext ());
        
        mutable last_added = AddFirst (enu.Current);
        while (enu.MoveNext ()) {
          last_added = AddAfter (last_added, enu.Current);
        }
      }
    }

    /** Append item to the list. */
    public Append (item : 'a) : void
    {
      _ = AddLast (item)
    }

    /** Append another list to an end. The source list will be cleared. */
    public Append ([NotNull] l : LinkedList ['a]) : void
    {
      foreach (x in l)
        _ = AddLast (x);
    }

    // Now the ICollection['a] interface implementation


    /** Returns true, if the list is empty. */
    public IsEmpty : bool
    {
      get { Count == 0 }
    }

    /** Adds item at the beginning of the list. */
    public Add (item : 'a) : void
    {
      _ = AddFirst (item);
    }

    /** Returns first element of the list as an option. */
    public new First () : option ['a]
    {
      match (base.First) {
        | null => None ()
        | node => Some (node.Value)
      }
    }

    /** Returns shallow copy of the list. */
    public Clone () : ICollection ['a]
    {
      def l = LinkedList ();
      foreach (item in this)
        l.Append (item);
      l;
    }

    /**
* Folds the list using the specified fold function and an initial
* value. Elements are folded in order of appearance.
*/
    public Fold ['b] (f : 'a * 'b -> 'b, x : 'b) : 'b
    {
      mutable retval = x;
      foreach (item in this)
        retval = f (item, retval);
      retval;
    }

    /**
* Creates new list with elements from the original with supplied
* function applied.
*/
    public Map ['b] (f : 'a -> 'b) : ICollection ['b]
    {
      def l = LinkedList();
      foreach (item in this)
        l.Append (f (item));
      l;
    }

    /**
* Calls the supplied function for all the elements of the list.
*/
    public Iter (f : 'a -> void) : void
    {
      foreach (item in this)
        f (item);
    }

    /**
* Checks if all the members of this list satisfy the supplied
* predicate.
*/
    public ForAll (f : 'a -> bool) : bool
    {
      def e = GetEnumerator ();
      def check () : bool {
        if (e.MoveNext())
          if (f (e.Current))
            check ();
          else
            false;
        else
          true;
      }
      check ();
    }

    /**
* Checks if there exists a member of list that satisfies
* the supplied condition.
*/
    public Exists (f : 'a -> bool) : bool
    {
      def e = GetEnumerator ();
      def check () : bool {
        if (e.MoveNext())
          if (f (e.Current))
            true;
          else
            check ();
        else
          false;
      }
      check ();
    }

    /**
* Filters the list removing all elements that do not satisfy
* the supplied predicate.
*/
    public Filter (f : 'a -> bool) : void
    {
      unless (IsEmpty) {
        mutable current = base.First;
        while (current != null) {
          def next = current.Next;
          unless (f (current.Value))
            Remove (current);
          current = next;
        }
      }
    }


    /**
* Partitions list into two lists: elements that satisfy
* and elements that do not satisfy the supplied predicate.
*/
    public Partition (f : 'a -> bool) : ICollection ['a] * ICollection ['a]
    {
      def does = LinkedList ();
      def donot = LinkedList ();
      foreach (item in this)
        if (f (item))
          does.Append (item);
        else
          donot.Append (item);
      (does, donot);
    }

    /** Remove all occurences of item from list */
    public new Remove (item : 'a) : void {
      while (base.Remove (item)) { }
    }
  } // LinkedList
} // namespace
Something went wrong with that request. Please try again.