• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Lyt
            posts - 16,comments - 61,trackbacks - 0

            發現這里被我荒廢了很久,自動腸粉--

            最近在湊數據結構,發現要湊個稍微好看點的代碼難度好大,模仿了.NET改了自己以前的結構,憋了一天終于把可擴展的List折騰出來了

            比較怪異的是迭代器iterator,怎么看怎么別扭,暫時先湊合著吧

            IIterator.h

                    template<typename _Type>
                    
            class IIterator    //迭代器
                    {
                    
            protected:
                        _Type Current;

                    
            public:
                        
            virtual ~IIterator()
                        
            {
                        }


                        
            virtual _Type CurrentItem()const=0;        //Current是否有效
                        virtual void First()=0;                    //Current移到首位
                        virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
                        virtual bool IsFirst()const=0;            //Current是否在首位
                        virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
                        virtual bool IsAvailable()const=0;        //Current是否有效
                        virtual void MoveNext()=0;                //Current移到下一位置
                    }
            ;

                    template
            <typename _Type>
                    
            class IForwordIterator : public IIterator<_Type>    //單向迭代器
                    {
                    
            public:
                        
            virtual ~IForwordIterator()
                        
            {
                        }


                        
            virtual _Type CurrentItem()const=0;        //Current是否有效
                        virtual void First()=0;                    //Current移到首位
                        virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
                        virtual bool IsFirst()const=0;            //Current是否在首位
                        virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
                        virtual bool IsAvailable()const=0;        //Current是否有效
                        virtual void MoveNext()=0;                //Current移到下一位置
                    }
            ;

                    template
            <typename _Type>
                    
            class IBidirectionalIterator : public IIterator<_Type>    //雙向迭代器
                    {
                    
            public:
                        
            virtual ~IBidirectionalIterator()
                        
            {
                        }


                        
            virtual _Type CurrentItem()const=0;        //Current是否有效
                        virtual void First()=0;                    //Current移到首位
                        virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
                        virtual bool IsFirst()const=0;            //Current是否在首位
                        virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
                        virtual bool IsAvailable()const=0;        //Current是否有效
                        virtual void MoveNext()=0;                //Current移到下一位置

                        
            virtual void MovePrev()=0;                //Current移到前一位置
                    }
            ;

                    template
            <typename _Type>
                    
            class IRandomIterator : public IIterator<_Type>    //隨機迭代器
                    {
                    
            public:
                        
            virtual ~IRandomIterator()
                        
            {
                        }


                        
            virtual _Type CurrentItem()const=0;        //Current是否有效
                        virtual void First()=0;                    //Current移到首位
                        virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
                        virtual bool IsFirst()const=0;            //Current是否在首位
                        virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
                        virtual bool IsAvailable()const=0;        //Current是否有效
                        virtual void MoveNext()=0;                //Current移到下一位置
                        
                        
            virtual void MovePrev()=0;                //Current移到前一位置
                        virtual void SkipPrev(const int n)=0;    //Current前移n位
                        virtual void SkipNext(const int n)=0;    //Current后移n位
                    }
            ;

                    template
            <typename _Type>
                    
            class IIterative    //可迭代的
                    {
                    
            public:
                        
            virtual ~IIterative()
                        
            {
                        }


                        
            virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;    //返回迭代器
                    }
            ;

            ListIterator

                    template<typename _Type>
                    
            class ListIterator : public IRandomIterator<_Type>        //List迭代器
                    {
                    
            public:
                        
            const List<_Type>* Data;
                        
            int Index;

                        ListIterator(
            const List<_Type>* Object)
                        
            {
                            
            if (!Object || Object->IsEmpty()) throw L"List迭代器初始化出錯";
                            
            else
                            
            {
                                Data
            =Object;
                                Index
            =0;
                            }

                        }


                        ListIterator(
            const ListIterator<_Type>& Object)
                        
            {
                            Data
            =Object.Data;
                            Index
            =Object.Index;
                            Current
            =Object.Current;
                        }


                        
            virtual ~ListIterator()
                        
            {
                        }


                        
            virtual _Type CurrentItem()const        //Current是否有效
                        {
                            
            if (IsAvailable()) return Current;
                            
            else throw L"非法訪問當前元素";
                        }


                        
            virtual void First()                    //Current移到首位
                        {
                            
            if (Data->IsEmpty()) throw L"List為空";
                            
            else
                            
            {
                                Index
            =0;
                                Current
            =Data->operator [](0);
                            }

                        }


                        
            virtual bool IsDone()const                //Current是否到達末尾,true的時候CurrentItem()失效
                        {
                            
            return Index>=Data->GetCount();
                        }


                        
            virtual bool IsFirst()const                //Current是否在首位
                        {
                            
            return Index==0;
                        }


                        
            virtual bool IsLast()const                //Current是否到達末尾,ture的時候CurrentItem()有效
                        {
                            
            return Index==Data->GetCount()-1;
                        }


                        
            virtual bool IsAvailable()const            //Current是否有效
                        {
                            
            return (Index>=0 && Index<=Data->GetCount()-1);
                        }


                        
            virtual void MoveNext()                    //Current移到下一位置
                        {
                            
            ++Index;
                            
            if (IsAvailable()) Current=Data->operator [](Index);
                        }

                        
                        
            virtual void MovePrev()                    //Current移到前一位置
                        {
                            
            --Index;
                            
            if (IsAvailable()) Current=Data->operator [](Index);
                        }


                        
            virtual void SkipPrev(const int n)        //Current前移n位
                        {
                            Index
            -=n;
                            
            if (IsAvailable()) Current=Data->operator [](Index);
                        }


                        
            virtual void SkipNext(const int n)        //Current后移n位
                        {
                            Index
            +=n;
                            
            if (IsAvailable()) Current=Data->operator [](Index);
                        }

                    }
            ;

             

            ICollecion.h

                    template<typename _Type>
                    
            class ICollection : public IIterative<_Type>
                    
            {
                    
            protected:
                        
            int Count;            //元素個數

                    
            public:
                        
            virtual ~ICollection()
                        
            {
                        }


                        
            virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;    //返回迭代器,繼承自IIterative

                        
            virtual const int GetCount()const=0;                    //返回元素個數
                        virtual bool IsEmpty()const=0;                            //是否為空
                        virtual bool Add(const _Type& Object)=0;                        //從尾部添加元素
                        virtual void Clear()=0;                                    //刪除所有元素
                        virtual bool Contains(const _Type& Object)const=0;                //是否包含指定元素
                        virtual bool Remove(const _Type& Object)=0;                    //刪除指定元素,若有多個,則刪除第一個
                    }
            ;

            IList.h

                    template<typename _Type>
                    
            class IList : public ICollection<_Type>
                    
            {
                    
            public:
                        
            virtual ~IList()
                        
            {
                        }


                        
            virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;        //返回迭代器,繼承自IIterative
                        virtual const int GetCount()const=0;                            //返回元素個數,繼承自ICollection
                        virtual bool IsEmpty()const=0;                                    //是否為空,繼承自ICollection
                        virtual bool Add(const _Type& Object)=0;                        //從尾部添加元素,繼承自ICollection
                        virtual void Clear()=0;                                            //刪除所有元素,繼承自ICollection
                        virtual bool Contains(const _Type& Object)const=0;                //是否包含指定元素,繼承自ICollection
                        virtual bool Remove(const _Type& Object)=0;                        //刪除指定元素,若有多個,則刪除第一個,繼承自ICollection

                        
            virtual int IndexOf(const _Type& Object)const=0;                //返回指定元素所在位置,若不存在返回-1,若有多個,返回第一個
                        virtual bool Insert(const int Index, const _Type& Object)=0;    //在指定位置插入指定元素
                        virtual bool RemoveAt(const int Index)=0;                        //在指定位置刪除元素
                    }
            ;
            List.h
                    template<typename _Type>
                    
            class List : public IList<_Type>
                    
            {
                    
            private:
                        
            bool Grow();                                                        //如果Capacity有增大,則返回真
                        void Update();                                                        //Capacity增大導致Items需要更新
                
                    
            protected:
                        _Type
            * Items;
                        
            int Capacity;                                                        //List可容納元素個數
                        static const int GRAW_CAPACITY;                                        //List每次增大的容量

                    
            public:
                        
            bool IsFixedSize;                                                    //List大小是否固定

                        List(
            const bool Fixed=false);
                        List(
            const int ObjectCount, const bool Fixed=false);
                        List(
            const List<_Type>& Object);

                        
            virtual ~List();

                        
            virtual AutoPtr<IIterator<_Type>> CreateIterator()const;            //返回迭代器,繼承自IIterative
                        virtual void Clear();                                                //刪除所有元素,繼承自ICollection
                        virtual const int GetCount()const;                                    //返回元素個數,繼承自ICollection
                        virtual int IndexOf(const _Type& Object)const;                        //返回指定元素所在位置,若不存在返回-1,繼承自IList
                        virtual int LastIndexOf(const _Type& Object)const                    //返回指定元素所在位置,若不存在返回-1,若有多個,返回最后一個
                        virtual bool IsEmpty()const;                                        //是否為空,繼承自ICollection
                        virtual bool Add(const _Type& Object);                                //從List尾部添加元素,繼承自ICollection
                        virtual bool Add(const List<_Type>& Object);                        //在List末尾添加列表
                        virtual bool Contains(const _Type& Object)const;                    //是否包含指定元素,繼承自ICollection
                        virtual bool Remove(const _Type& Object);                            //刪除指定元素,若有多個,則刪除第一個,繼承自ICollection
                        virtual bool Remove(const int Index, const int n);                    //從Index開始刪除n個元素
                        virtual bool RemoveAt(const int Index);                                //在指定位置刪除元素,繼承自IList
                        virtual bool Insert(const int Index, const _Type& Object);            //在指定位置插入指定元素,繼承自IList
                        virtual bool Insert(const int Index, const List<_Type>& Object);    //從Index開始插入Object
                        virtual List<_Type> Sub(const int Index, const int n)const;            //從Index開始取n個元素
                        virtual List<_Type> Left(const int n)const;                            //取左邊n個元素
                        virtual List<_Type> Right(const int n)const;                        //取右邊n個元素
                        _Type operator[](const int Index)const;
                        _Type
            & operator[](const int Index);
                        List
            <_Type> operator=(const List<_Type>& Object);
                        
            bool operator==(const List<_Type>& Object)const;
                        
            bool operator!=(const List<_Type>& Object)const;
                        List
            <_Type> operator+(const _Type& Object)const;
                        List
            <_Type> operator+(const List<_Type>& Object)const;
                    }
            ;

            例子:

            void Print(AutoPtr<ListIterator<int>> Iterator)
            {
                
            for (Iterator->First(); !Iterator->IsDone(); Iterator->MoveNext()) cout<<Iterator->CurrentItem()<<" ";
                cout
            <<endl;
            }


            void main(int argc , char* argv[])
            {
                setlocale(LC_ALL,
            "chs");
                List
            <int> A(1);
                A[
            0]=1;
                AutoPtr
            <ListIterator<int>> Iterator=A.CreateIterator();
                Print(Iterator);
                A.Add(
            2);
                cout
            <<"After A.Add(2) : ";
                Print(Iterator);
                
            if (A.Contains(1)) cout<<"A contains 1"<<endl;
                
            if (!A.Contains(3)) cout<<"A doesn't contain 3"<<endl;
                cout
            <<"index of 1 is "<<A.IndexOf(1)<<endl;
                cout
            <<"index of 3 is "<<A.IndexOf(3)<<endl;
                A.Insert(
            0,0);
                cout
            <<"After A.Insert(0,0) : ";
                Print(Iterator);
                A.Insert(
            1,3);
                cout
            <<"After A.Insert(1,3) : ";
                Print(Iterator);
                A.RemoveAt(
            1);
                cout
            <<"After A.RemoveAt(1) : ";
                Print(Iterator);
                List
            <int> B=A.Right(2);
                A.Insert(
            1, B);
                cout
            <<"After B=A.Right(2); A.Insert(1, B) : ";
                Print(Iterator);
                A
            =A.Sub(1,3);
                cout
            <<"After A=A.Sub(1,3) : ";
                Print(Iterator);
                B
            =A.ToArray();
                A
            =A+B;
                cout
            <<"After B=A.ToArray(); A=A+B : ";
                Print(Iterator);
                cout
            <<"last index of 2 is "<<A.LastIndexOf(2)<<endl;
                _getch();
            }

            結果如下:

            1
            After A.Add(2) : 1 2
            A contains 1
            A doesn't contain 3
            index of 1 is 0
            index of 3 is -1
            After A.Insert(0,0) : 0 1 2
            After A.Insert(1,3) : 0 3 1 2
            After A.RemoveAt(1) : 0 1 2
            After B=A.Right(2); A.Insert(1, B) : 0 1 2 1 2
            After A=A.Sub(1,3) : 1 2 1
            After B=A.ToArray(); A=A+B : 1 2 1 1 2 1
            last index of 2 is 4

             

             

            C#在用delegate的確很爽,無奈C++沒得爽,本來很天真地想實現一下功能

            template<typename _Type>
            class List : public IList<_Type>
            {
              template
            <typename T>
              typedef 
            bool Predicate(T& Object);

              
            bool Exists(Predicate<_Type> Match)
              {
                
            if (Match(*this)) return true;
                
            else return false;
              }
            };

             網上搜了下,才發現C++沒有這個特性,delegate暫時湊不出來,于是折騰了個難看的辦法,沒想到還是不行,至今不知道為啥

            template<typename _Type>
            class List : public IList<_Type>
            {
              
            bool Exists ((bool(*Predicate)(List<_Type>& Object)) Match)
              {
                
            if (Match(*this)) return true;
                
            else return false;
              }
            };

             

            歡迎大家來噴哈~

            posted on 2009-09-23 11:20 Lyt 閱讀(1960) 評論(5)  編輯 收藏 引用 所屬分類: 數據結構

            FeedBack:
            # re: Collection::List
            2009-09-23 14:56 | OwnWaterloo
            如果以GP而非OOP的方式構建集合,你會發現C++的template即使沒有delegate也會很爽。C#沒得這么爽。   回復  更多評論
              
            # re: Collection::List
            2009-09-23 18:30 | 陳梓瀚(vczh)
            @OwnWaterloo
            我的VL++2.0推掉重寫,最近再寫3.0,實踐了一下同時GP+OOP,最后發現可行,而且我也實現了delegate,將linq搬過來了。  回復  更多評論
              
            # re: Collection::List
            2009-09-23 18:35 | OwnWaterloo
            @陳梓瀚(vczh)
            支持推倒……  回復  更多評論
              
            # re: Collection::List
            2009-09-24 12:15 | 陳梓瀚(vczh)
            @OwnWaterloo
            原來同是宅人……  回復  更多評論
              
            # re: Collection::List[未登錄]
            2009-09-28 11:21 | a
            不用GP, 很多情況下都可以忽略Iterator,很多人用it無非是rbtree。我就喜歡直接用int遍歷hash(directory), vector, list之類的。。  回復  更多評論
              
            久久久久久人妻无码| 伊人热人久久中文字幕| 色偷偷偷久久伊人大杳蕉| 99久久精品国产麻豆| 久久精品国产亚洲AV不卡| 精品久久久中文字幕人妻| 91精品国产综合久久四虎久久无码一级 | 99久久久精品免费观看国产| 婷婷综合久久狠狠色99h| 久久久久亚洲av成人网人人软件 | 久久精品国产亚洲av瑜伽| 97久久国产综合精品女不卡| 99久久国产综合精品网成人影院| 精品久久久久久久久免费影院 | 久久综合九色欧美综合狠狠| 久久久亚洲欧洲日产国码二区| 久久综合一区二区无码| 久久本道伊人久久| 色88久久久久高潮综合影院| 亚洲精品tv久久久久| 国产三级观看久久| 久久精品国产亚洲网站| 久久久久免费看成人影片| 久久亚洲国产精品成人AV秋霞| 国产精品免费久久久久久久久| 狠色狠色狠狠色综合久久| 色婷婷综合久久久久中文| 人人妻久久人人澡人人爽人人精品 | 久久亚洲日韩看片无码| 欧美伊人久久大香线蕉综合69| 久久亚洲国产中v天仙www| 狠狠色婷婷久久一区二区三区| 精品熟女少妇AV免费久久| 国产成人综合久久精品红| 日产久久强奸免费的看| 久久久久亚洲AV无码去区首| 2021国产成人精品久久| 狠狠人妻久久久久久综合| 久久精品国产99国产精品| 国产精品丝袜久久久久久不卡| 9191精品国产免费久久|