Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 704 lines (583 sloc) 23.621 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using IronRuby.Runtime;
using Microsoft.Scripting;
using Microsoft.Scripting.Utils;
using IronRuby.Runtime.Calls;
using IronRuby.Runtime.Conversions;
using System.Runtime.Serialization;

namespace IronRuby.Builtins {

    /// <summary>
    /// Implements Ruby array.
    /// Not thread safe (even when frozen).
    /// </summary>
[Serializable]
[DebuggerDisplay("{GetDebugView()}")]
    public partial class RubyArray : IList<object>, IList, IRubyObjectState, IDuplicable {
        private object[]/*!*/ _content;
        
        private int _start;
        private int _count;

        private uint _flags;
        private const uint IsFrozenFlag = 1;
        private const uint IsTaintedFlag = 2;
        private const uint IsUntrustedFlag = 4;

[Conditional("DEBUG")]
        private void ObjectInvariant() {
            Debug.Assert(_start >= 0 && _count >= 0);
            Debug.Assert(_start + _count <= _content.Length);
            Debug.Assert(_content.Length == 0 || _content.Length >= Utils.MinListSize);
        }

[Conditional("DEBUG")]
        internal void RequireNullEmptySlots() {
            for (int i = 0; i < _content.Length; i++) {
                if (i < _start || i >= _start + _count) {
                    Debug.Assert(_content[i] == null);
                }
            }
        }

        #region Construction

        internal RubyArray(object[]/*!*/ content, int start, int count) {
            _start = start;
            _content = content;
            _count = count;
            ObjectInvariant();
        }

        public RubyArray()
            : this(ArrayUtils.EmptyObjects, 0, 0) {
        }

        public RubyArray(int capacity)
            : this(new object[Math.Max(capacity, Utils.MinListSize)], 0, 0) {
        }

        public RubyArray(RubyArray/*!*/ items)
            : this(items, 0, items.Count) {
        }

        public RubyArray(RubyArray/*!*/ items, int start, int count)
            : this(count) {
            ContractUtils.RequiresNotNull(items, "items");
            ContractUtils.RequiresArrayRange(items.Count, start, count, "start", "count");

            AddVector(items._content, items._start + start, count);
        }

        public RubyArray(IList/*!*/ items)
            : this(items, 0, items.Count) {
        }

        public RubyArray(IList/*!*/ items, int start, int count)
            : this(count) {
            AddRange(items, start, count);
        }

        public RubyArray(ICollection/*!*/ items)
            : this(items.Count) {
            AddCollection(items);
        }

        public RubyArray(IEnumerable/*!*/ items)
            : this() {
            AddRange(items);
        }

        public static RubyArray/*!*/ Create(object item) {
            var content = new object[Utils.MinListSize];
            content[0] = item;
            return new RubyArray(content, 0, 1);
        }

        /// <summary>
        /// Creates a blank instance of a RubyArray or its subclass given the Ruby class object.
        /// </summary>
        public static RubyArray/*!*/ CreateInstance(RubyClass/*!*/ rubyClass) {
            return (rubyClass.GetUnderlyingSystemType() == typeof(RubyArray)) ? new RubyArray() : new RubyArray.Subclass(rubyClass);
        }

        /// <summary>
        /// Creates an empty instance.
        /// Doesn't copy instance data.
        /// Preserves the class of the Array.
        /// </summary>
        public virtual RubyArray/*!*/ CreateInstance() {
            return new RubyArray();
        }

        object IDuplicable.Duplicate(RubyContext/*!*/ context, bool copySingletonMembers) {
            var result = CreateInstance();
            context.CopyInstanceData(this, result, copySingletonMembers);
            return result;
        }

        #endregion

        #region Flags

        public void RequireNotFrozen() {
            if ((_flags & IsFrozenFlag) != 0) {
                // throw in a separate method to allow inlining of the current one
                ThrowArrayFrozenException();
            }
        }

        private static void ThrowArrayFrozenException() {
            throw RubyExceptions.CreateArrayFrozenError();
        }

        private void Mutate() {
            RequireNotFrozen();
        }

        public bool IsTainted {
            get {
                return (_flags & IsTaintedFlag) != 0;
            }
            set {
                Mutate();
                _flags = (_flags & ~IsTaintedFlag) | (value ? IsTaintedFlag : 0);
            }
        }

        public bool IsUntrusted {
            get {
                return (_flags & IsUntrustedFlag) != 0;
            }
            set {
                Mutate();
                _flags = (_flags & ~IsUntrustedFlag) | (value ? IsUntrustedFlag : 0);
            }
        }

        public bool IsFrozen {
            get {
                return (_flags & IsFrozenFlag) != 0;
            }
        }

        void IRubyObjectState.Freeze() {
            Freeze();
        }

        public RubyArray/*!*/ Freeze() {
            _flags |= IsFrozenFlag;
            return this;
        }

        #endregion

        #region HashCode, Equality

[MultiRuntimeAware]
        private static RubyUtils.RecursionTracker _HashTracker = new RubyUtils.RecursionTracker();

        public static int GetHashCode(UnaryOpStorage/*!*/ hashStorage, ConversionStorage<int>/*!*/ fixnumCast, IList/*!*/ self) {
            int hash = self.Count;
            using (IDisposable handle = _HashTracker.TrackObject(self)) {
                if (handle == null) {
                    // hashing of recursive array
                    return 0;
                }

                var hashSite = hashStorage.GetCallSite("hash");
                var toIntSite = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));
                foreach (object item in self) {
                    hash = (hash << 1) ^ toIntSite.Target(toIntSite, hashSite.Target(hashSite, item));
                }
            }
            return hash;
        }

[MultiRuntimeAware]
        private static RubyUtils.RecursionTracker _EqualsTracker = new RubyUtils.RecursionTracker();

        public static bool Equals(BinaryOpStorage/*!*/ eqlStorage, IList/*!*/ self, object obj) {
            if (ReferenceEquals(self, obj)) {
                return true;
            }

            IList other = obj as IList;
            if (other == null || self.Count != other.Count) {
                return false;
            }

            using (IDisposable handleSelf = _EqualsTracker.TrackObject(self), handleOther = _EqualsTracker.TrackObject(other)) {
                if (handleSelf == null && handleOther == null) {
                    // both arrays went recursive:
                    return true;
                }

                var site = eqlStorage.GetCallSite("eql?");
                for (int i = 0; i < self.Count; i++) {
                    if (!Protocols.IsEqual(site, self[i], other[i])) {
                        return false;
                    }
                }
            }
            return true;
        }

        #endregion

        #region Add, AddRange

        /// <summary>
        /// Adds count to _count and resizes the storage to make space for count elements if necessary.
        /// </summary>
        /// <param name="additionalCount">The number of elements that will be appended.</param>
        /// <param name="clear">
        /// True if the slots should be cleared (null initialized).
        /// The caller isn't filling the slots with non-null values.
        /// </param>
        /// <returns>The index of the first slot that will contain the new elements.</returns>
        private int ResizeForAppend(int additionalCount, bool clear) {
            int oldCount = _count;
            _count += additionalCount;

            if (_start + oldCount > _content.Length - additionalCount) {
                // not enough space for additional items:
                object[] newContent;
                if (_count > _content.Length) {
                    newContent = new object[Utils.GetExpandedSize(_content, _count)];
                } else {
                    newContent = _content;
                }
                Array.Copy(_content, _start, newContent, 0, oldCount);
                if (newContent == _content && (additionalCount < _start || clear)) {
                    if (_start < oldCount) {
                        Utils.Fill(newContent, oldCount, null, _start);
                    } else {
                        Utils.Fill(newContent, _start, null, oldCount);
                    }
                }
                _content = newContent;
                _start = 0;
            }
            ObjectInvariant();
            return _start + oldCount;
        }

        public void Add(object item) {
            Mutate();
            int index = ResizeForAppend(1, false);
            _content[index] = item;
        }

        int IList.Add(object value) {
            int result = _count;
            Add(value);
            return result;
        }

        /// <summary>
        /// Ensures that the underlying storage is prepared to store at least the current number plus capacity items.
        /// </summary>
        /// <param name="capacity">Additional capacity.</param>
        /// <returns>Self.</returns>
        /// <remarks>
        /// Use to avoid reallocation of the underlying storage if the number of elements that will eventually by added to this array is known.
        /// Doesn't increase the number of elements in the array.
        /// Call <see cref="AddMultiple"/> to add multiple (possibly null) elements into the array.
        /// </remarks>
        public RubyArray/*!*/ AddCapacity(int capacity) {
            if (capacity < 0) {
                throw new ArgumentOutOfRangeException("capacity");
            }

            Mutate();
            int oldCount = _count;
            // We're not writing to the slots, so require them to be cleared so that we don't
            // accidentally keep objects alive whose references might have been in the slots.
            // (the resize could've just copied the content left to make space for new elements).
            ResizeForAppend(capacity, true);
            _count = oldCount;
            return this;
        }

        public RubyArray/*!*/ AddMultiple(int count, object value) {
            Mutate();
            if (value != null) {
                int start = ResizeForAppend(count, false);
                int end = start + count;
                for (int i = start; i < end; i++) {
                    _content[i] = value;
                }
            } else {
                ResizeForAppend(count, true);
            }
            return this;
        }

        public RubyArray/*!*/ AddRange(IList/*!*/ items, int start, int count) {
            ContractUtils.RequiresNotNull(items, "items");
            ContractUtils.RequiresArrayRange(items.Count, start, count, "start", "count");
            Mutate();

            RubyArray array;
            object[] vector;
            if ((array = items as RubyArray) != null) {
                AddVector(array._content, array._start + start, count);
            } else if ((vector = items as object[]) != null) {
                AddVector(vector, start, count);
            } else {
                AddList(items, start, count);
            }

            return this;
        }

        public RubyArray/*!*/ AddRange(IEnumerable/*!*/ items) {
            ContractUtils.RequiresNotNull(items, "items");
            Mutate();

            RubyArray array;
            ICollection collection;
            object[] vector;

            if ((array = items as RubyArray) != null) {
                AddVector(array._content, array._start, array._count);
            } else if ((vector = items as object[]) != null) {
                AddVector(vector, 0, vector.Length);
            } else if ((collection = items as ICollection) != null) {
                AddCollection(collection);
            } else {
                AddSequence(items);
            }

            return this;
        }

        private void AddList(IList/*!*/ items, int start, int count) {
            int s = ResizeForAppend(count, false);
            for (int i = 0; i < count; i++) {
                _content[s + i] = items[start + i];
            }
        }

        internal void AddVector(object[]/*!*/ items, int start, int count) {
            int s = ResizeForAppend(count, false);
            Array.Copy(items, start, _content, s, count);
        }

        private void AddCollection(ICollection/*!*/ items) {
            int i = ResizeForAppend(items.Count, false);
            foreach (object item in items) {
                _content[i++] = item;
            }
        }

        private void AddSequence(IEnumerable/*!*/ items) {
            foreach (var item in items) {
                Add(item);
            }
        }

        #endregion

        #region Insert, InsertRange

        private int ResizeForInsertion(int index, int size) {
            if (_count + size > _content.Length) {
                object[] newContent = new object[Utils.GetExpandedSize(_content, _count + size)];
                Array.Copy(_content, _start, newContent, 0, index);
                Array.Copy(_content, _start + index, newContent, index + size, _count - index);
                _count += size;
                _content = newContent;
                return index;
            }

            int rindex = _start + index;
            int result = rindex;
            int spaceRight = _content.Length - _start - _count;
            int shiftLeft = 0, shiftRight = 0;
            if (_start >= size) {
                if (spaceRight >= size) {
                    if (index < _count / 2) {
                        shiftLeft = size;
                        result -= size;
                    } else {
                        shiftRight = size;
                    }
                } else {
                    shiftLeft = size;
                    result -= size;
                }
            } else if (spaceRight >= size) {
                shiftRight = size;
            } else {
                shiftLeft = _start;
                shiftRight = size - shiftLeft;
                result -= shiftLeft;
            }
            
            if (shiftLeft > 0) {
                var newStart = _start - shiftLeft;
                Array.Copy(_content, _start, _content, newStart, index);
                _start = newStart;
            }

            if (shiftRight > 0) {
                Array.Copy(_content, rindex, _content, rindex + shiftRight, _count - index);
            }

            _count += size;
            return result;
        }

        public void Insert(int index, object item) {
            ContractUtils.RequiresArrayInsertIndex(_count, index, "index");
            Mutate();
            int i = ResizeForInsertion(index, 1);
            _content[i] = item;
            ObjectInvariant();
        }

        public void InsertRange(int index, IList/*!*/ items, int start, int count) {
            ContractUtils.RequiresNotNull(items, "items");
            ContractUtils.RequiresArrayInsertIndex(_count, index, "index");
            ContractUtils.RequiresArrayRange(items.Count, start, count, "start", "count");
            Mutate();

            RubyArray array;
            object[] vector;

            if ((array = items as RubyArray) != null) {
                InsertVector(index, array._content, start, count);
            } else if ((vector = items as object[]) != null) {
                InsertVector(index, vector, start, count);
            } else {
                InsertList(index, items, start, count);
            }
        }

        public void InsertRange(int index, IEnumerable/*!*/ items) {
            ContractUtils.RequiresNotNull(items, "items");
            ContractUtils.RequiresArrayInsertIndex(_count, index, "index");
            Mutate();
            
            RubyArray array;
            IList list;
            object[] vector;

            if ((array = items as RubyArray) != null) {
                InsertArray(index, array);
            } else if ((vector = items as object[]) != null) {
                InsertVector(index, vector, 0, vector.Length);
            } else if ((list = items as IList) != null) {
                InsertList(index, list, 0, list.Count);
            } else {
                InsertArray(index, new RubyArray(items));
            }
        }

        private void InsertArray(int index, RubyArray/*!*/ array) {
            InsertVector(index, array._content, array._start, array._count);
        }

        private void InsertVector(int index, object[]/*!*/ items, int start, int count) {
            int s = ResizeForInsertion(index, count);
            Array.Copy(items, start, _content, s, count);
        }

        private void InsertList(int index, IList/*!*/ items, int start, int count) {
            int s = ResizeForInsertion(index, count);
            for (int i = 0; i < count; i++) {
                _content[s + i] = items[start + i];
            }
        }

        #endregion

        #region RemoveAt, RemoveRange, Remove, Clear

        public void RemoveRange(int index, int size) {
            ContractUtils.RequiresArrayRange(_count, index, size, "index", "size");
            Mutate();

            int newCount = _count - size;
            int length = _content.Length;
            
            int newLength = 2 * newCount - 1;
            if (newLength <= length / 2) {
                // resize:
                object[] newContent = new object[Math.Max(Utils.MinListSize, newLength)];
                Array.Copy(_content, _start, newContent, 0, index);
                Array.Copy(_content, _start + index + size, newContent, index, newCount - index);
                _content = newContent;
                _start = 0;
            } else if (index <= newCount / 2) {
                // shift right:
                int newStart = _start + size;
                Array.Copy(_content, _start, _content, newStart, index);
                Utils.Fill(_content, _start, null, size);
                _start = newStart;
            } else {
                // shift left:
                Array.Copy(_content, _start + index + size, _content, _start + index, newCount - index);
                Utils.Fill(_content, _start + newCount, null, size);
            }
            _count = newCount;
            ObjectInvariant();
        }

        public void RemoveAt(int index) {
            RemoveRange(index, 1);
        }

        public bool Remove(object item) {
            int index = IndexOf(item);
            if (index >= 0) {
                RemoveAt(index);
                return true;
            }
            return false;
        }

        void IList.Remove(object value) {
            Remove(value);
        }

        public void Clear() {
            Mutate();
            _content = ArrayUtils.EmptyObjects;
            _start = _count = 0;
        }

        #endregion

        #region CopyTo, ToArray

        public void CopyTo(object[]/*!*/ array, int index) {
            Array.Copy(_content, _start, array, index, _count);
        }

        void ICollection.CopyTo(Array array, int index) {
            Array.Copy(_content, _start, array, index, _count);
        }

        public object[]/*!*/ ToArray() {
            object[] result = new object[_count];
            CopyTo(result, 0);
            return result;
        }

        #endregion

        #region IndexOf, Contains, FindIndex, GetEnumerator

        public int IndexOf(object item) {
            return IndexOf(item, 0, _count);
        }

        public int IndexOf(object item, int startIndex) {
            return Array.IndexOf(_content, item, startIndex, _count - startIndex);
        }
        
        public int IndexOf(object item, int startIndex, int count) {
            return Array.IndexOf(_content, item, _start + startIndex, count);
        }

#if !SILVERLIGHT && !WP75 // TODO: replace by linq
        public int FindIndex(Predicate<object> match) {
            return FindIndex(0, _count, match);
        }

        public int FindIndex(int startIndex, Predicate<object>/*!*/ match) {
            return FindIndex(startIndex, _count - startIndex, match);
        }

        public int FindIndex(int startIndex, int count, Predicate<object>/*!*/ match) {
            return Array.FindIndex(_content, _start + startIndex, count, match);
        }
#endif

        public bool Contains(object item) {
            return IndexOf(item) >= 0;
        }

        public IEnumerator<object>/*!*/ GetEnumerator() {
            for (int i = 0, start = _start, count = _count; i < count; i++) {
                yield return _content[start + i];
            }
        }

        IEnumerator/*!*/ IEnumerable.GetEnumerator() {
            return ((IEnumerable<object>)this).GetEnumerator();
        }

        #endregion

        #region Misc

        public int Count {
            get { return _count; }
        }

        public bool IsReadOnly {
            get { return IsFrozen; }
        }

        bool IList.IsFixedSize {
            get { return IsReadOnly; }
        }

        bool ICollection.IsSynchronized {
            get { return false; }
        }

        object ICollection.SyncRoot {
            get { return this; }
        }

        public int Capacity {
            get { return _content.Length; }
        }

        public object this[int index] {
            get {
                if (index < 0 || index >= _count) {
                    throw new IndexOutOfRangeException();
                }
                return _content[_start + index];
            }
            set {
                Mutate();
                int delta = index - _count;
                if (delta >= 0) {
                    ResizeForAppend(delta + 1, true);
                }
                _content[_start + index] = value;
            }
        }

        public void Reverse() {
            Mutate();
            Array.Reverse(_content, _start, _count);
        }

        public void Sort() {
            Mutate();
            Array.Sort(_content, _start, _count);
        }

        public void Sort(Comparison<object>/*!*/ comparison) {
            Mutate();
            Array.Sort(_content, _start, _count, ArrayUtils.ToComparer(comparison));
        }

        #endregion

        #region DebugView

        internal string/*!*/ GetDebugView() {
            return RubyContext._Default != null ? RubyContext._Default.Inspect(this).ToString() : ToString();
        }

        #endregion
    }
}
Something went wrong with that request. Please try again.