orderedlistasarray.cs

来自「Data Structures and Algorithms with Obj」· CS 代码 · 共 295 行

CS
295
字号
namespace Opus6
{
    using System;
    using System.Collections;
    using System.Reflection;

    [Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: OrderedListAsArray.cs,v 1.6 2001/10/28 19:50:09 brpreiss Exp $")]
    public class OrderedListAsArray : AbstractSearchableContainer, OrderedList, SearchableContainer, Container, IComparable, IEnumerable
    {
        public OrderedListAsArray(int size)
        {
            this.array = new ComparableObject[size];
        }

        public override void Accept(Visitor visitor)
        {
            for (int num1 = 0; num1 < base.count; num1++)
            {
                visitor.Visit(this.array[num1]);
                if (visitor.IsDone)
                {
                    return;
                }
            }
        }

        public override int CompareTo(object arg)
        {
            throw new MethodNotImplementedException();
        }

        public override ComparableObject Find(ComparableObject arg)
        {
            for (int num1 = 0; num1 < base.count; num1++)
            {
                if (this.array[num1] == arg)
                {
                    return this.array[num1];
                }
            }
            return null;
        }

        public virtual Cursor FindPosition(ComparableObject obj)
        {
            int num1 = 0;
            while ((num1 < base.count) && (this.array[num1] != obj))
            {
                num1++;
            }
            return new Opus6.OrderedListAsArray.MyCursor(this, num1);
        }

        public override IEnumerator GetEnumerator()
        {
            return new Opus6.OrderedListAsArray.Enumerator(this);
        }

        public override void Insert(ComparableObject obj)
        {
            if (base.count == this.array.Length)
            {
                throw new ContainerFullException();
            }
            this.array[base.count] = obj;
            base.count++;
        }

        public override bool IsMember(ComparableObject obj)
        {
            for (int num1 = 0; num1 < base.count; num1++)
            {
                if (this.array[num1] == obj)
                {
                    return true;
                }
            }
            return false;
        }

        public static void Main()
        {
            Opus6.Console.WriteLine("OrderedListAsArray Test");
            OrderedList list1 = new OrderedListAsArray(10);
            OrderedListAsArray.TestOrderedList(list1);
        }

        public override void Purge()
        {
            while (base.count > 0)
            {
                this.array[--base.count] = null;
            }
        }

        internal static void TestOrderedList(OrderedList list)
        {
            list.Insert((ComparableObject) 1);
            list.Insert((ComparableObject) 2);
            list.Insert((ComparableObject) 3);
            list.Insert((ComparableObject) 4);
            Opus6.Console.WriteLine(list);
            ComparableObject obj1 = list.Find((ComparableObject) 2);
            list.Withdraw(obj1);
            Opus6.Console.WriteLine(list);
            Cursor cursor1 = list.FindPosition((ComparableObject) 3);
            cursor1.InsertAfter((ComparableObject) 5);
            Opus6.Console.WriteLine(list);
            cursor1.InsertBefore((ComparableObject) 6);
            Opus6.Console.WriteLine(list);
            cursor1.Withdraw();
            Opus6.Console.WriteLine(list);
            foreach (ComparableObject obj2 in list)
            {
                Opus6.Console.WriteLine(obj2);
            }
        }

        public override void Withdraw(ComparableObject obj)
        {
            if (base.count == 0)
            {
                throw new ContainerEmptyException();
            }
            int num1 = 0;
            while ((num1 < base.count) && (this.array[num1] != obj))
            {
                num1++;
            }
            if (num1 == base.count)
            {
                throw new ArgumentException("object not found");
            }
            while (num1 < (base.count - 1))
            {
                this.array[num1] = this.array[num1 + 1];
                num1++;
            }
            this.array[num1] = null;
            base.count--;
        }


        public override bool IsFull
        {
            get
            {
                return (base.count == this.array.Length);
            }
        }

        public ComparableObject this[int offset]
        {
            get
            {
                if ((offset < 0) || (offset >= base.count))
                {
                    throw new IndexOutOfRangeException();
                }
                return this.array[offset];
            }
        }


        protected ComparableObject[] array;


        private class Enumerator : IEnumerator
        {
            internal Enumerator(OrderedListAsArray list)
            {
                this.position = -1;
                this.list = list;
            }

            public bool MoveNext()
            {
                if (++this.position == this.list.Count)
                {
                    this.position = -1;
                }
                return (this.position >= 0);
            }

            public void Reset()
            {
                this.position = -1;
            }


            public object Current
            {
                get
                {
                    if (this.position < 0)
                    {
                        throw new InvalidOperationException();
                    }
                    return this.list.array[this.position];
                }
            }


            private OrderedListAsArray list;
            private int position;
        }

        protected class MyCursor : Cursor
        {
            internal MyCursor(OrderedListAsArray list, int offset)
            {
                this.list = list;
                this.offset = offset;
            }

            public virtual void InsertAfter(ComparableObject obj)
            {
                if ((this.offset < 0) || (this.offset >= this.list.count))
                {
                    throw new IndexOutOfRangeException();
                }
                if (this.list.count == this.list.array.Length)
                {
                    throw new ContainerFullException();
                }
                int num1 = this.offset + 1;
                for (int num2 = this.list.count; num2 > num1; num2--)
                {
                    this.list.array[num2] = this.list.array[num2 - 1];
                }
                this.list.array[num1] = obj;
                this.list.count++;
            }

            public virtual void InsertBefore(ComparableObject obj)
            {
                if ((this.offset < 0) || (this.offset >= this.list.count))
                {
                    throw new IndexOutOfRangeException();
                }
                if (this.list.count == this.list.array.Length)
                {
                    throw new ContainerFullException();
                }
                int num1 = this.offset;
                for (int num2 = this.list.count; num2 > num1; num2--)
                {
                    this.list.array[num2] = this.list.array[num2 - 1];
                }
                this.list.array[num1] = obj;
                this.list.count++;
                this.offset++;
            }

            public void Withdraw()
            {
                if ((this.offset < 0) || (this.offset >= this.list.count))
                {
                    throw new IndexOutOfRangeException();
                }
                if (this.list.count == 0)
                {
                    throw new ContainerEmptyException();
                }
                int num1 = this.offset;
                while (num1 < (this.list.count - 1))
                {
                    this.list.array[num1] = this.list.array[num1 + 1];
                    num1++;
                }
                this.list.array[num1] = null;
                this.list.count--;
            }


            public ComparableObject Datum
            {
                get
                {
                    if ((this.offset < 0) || (this.offset >= this.list.count))
                    {
                        throw new IndexOutOfRangeException();
                    }
                    return this.list.array[this.offset];
                }
            }


            private OrderedListAsArray list;
            private int offset;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?