📄 lists.h
字号:
/* * lists.h * * List Container Classes * * Portable Windows Library * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): ______________________________________. * * $Log: lists.h,v $ * Revision 1.19 2000/04/14 07:19:32 craigs * Fixed problem with assert when dequeueing from an empty queue * * Revision 1.18 1999/08/22 12:13:43 robertj * Fixed warning when using inlines on older GNU compiler * * Revision 1.17 1999/03/09 02:59:50 robertj * Changed comments to doc++ compatible documentation. * * Revision 1.16 1999/02/16 08:12:00 robertj * MSVC 6.0 compatibility changes. * * Revision 1.15 1998/09/23 06:20:49 robertj * Added open source copyright license. * * Revision 1.14 1997/06/08 04:49:12 robertj * Fixed non-template class descendent order. * * Revision 1.13 1997/04/27 05:50:10 robertj * DLL support. * * Revision 1.12 1997/02/14 13:53:59 robertj * Major rewrite of sorted list to use sentinel record instead of NULL pointers. * * Revision 1.11 1996/07/15 10:32:50 robertj * Fixed bug in sorted list (crash on remove). * * Revision 1.10 1996/05/26 03:25:13 robertj * Compatibility to GNU 2.7.x * * Revision 1.9 1996/01/23 13:13:32 robertj * Fixed bug in sorted list GetObjectsIndex not checking if is same object * * Revision 1.8 1995/08/24 12:35:00 robertj * Added assert for list index out of bounds. * * Revision 1.7 1995/06/17 11:12:43 robertj * Documentation update. * * Revision 1.6 1995/03/14 12:41:41 robertj * Updated documentation to use HTML codes. * * Revision 1.5 1995/02/22 10:50:30 robertj * Changes required for compiling release (optimised) version. * * Revision 1.4 1995/02/05 00:48:05 robertj * Fixed template version. * * Revision 1.3 1995/01/15 04:49:23 robertj * Fixed errors in template version. * * Revision 1.2 1994/12/21 11:53:12 robertj * Documentation and variable normalisation. * * Revision 1.1 1994/12/12 09:59:35 robertj * Initial revision * */#ifdef __GNUC__#pragma interface#endif///////////////////////////////////////////////////////////////////////////////// PList container class/**This class is a collection of objects which are descendents of the #PObject# class. It is implemeted as a doubly linked list. The implementation of a list allows very fast inserting and deleting of objects in the collection, but has severe penalties for random access. All object access should be done sequentially to avoid these speed penalties. The class remembers the last accessed element. This state information is used to optimise access by the "virtual array" model of collections. If access via ordinal index is made sequentially there is little overhead. The PAbstractList class would very rarely be descended from directly by the user. The #PDECLARE_LIST# and #PLIST# macros would normally be used to create descendent classes. They will instantiate the template based on #PList# or directly declare and define the class (using inline functions) if templates are not being used. The #PList# class or #PDECLARE_LIST# macro will define the correctly typed operators for subscript access (#operator[]#). */class PAbstractList : public PCollection{ PCONTAINERINFO(PAbstractList, PCollection); public: /**@name Construction */ //@{ /**Create a new, empty, list. Note that by default, objects placed into the list will be deleted when removed or when all references to the list are destroyed. */ PINLINE PAbstractList(); //@} // Overrides from class PObject /**Get the relative rank of the two lists. The following algorithm is employed for the comparison:\begin{description} \item[#EqualTo#] if the two lists are identical in length and each objects values, not pointer, are equal. \item[#LessThan#] if the instances object value at an ordinal position is less than the corresponding objects value in the #obj# parameters list. This is also returned if all objects are equal and the instances list length is less than the #obj# parameters list length. \item[#GreaterThan#] if the instances object value at an ordinal position is greater than the corresponding objects value in the #obj# parameters list. This is also returned if all objects are equal and the instances list length is greater than the #obj# parameters list length.\end{description} @return comparison of the two objects, #EqualTo# for same, #LessThan# for #obj# logically less than the object and #GreaterThan# for #obj# logically greater than the object. */ virtual Comparison Compare(const PObject & obj) const; /**@name Overrides from class PContainer */ //@{ /**This function is meaningless for lists. The size of the collection is determined by the addition and removal of objects. The size cannot be set in any other way. @return Always TRUE. */ virtual BOOL SetSize( PINDEX newSize /// New size for the list, this is ignored. ); //@} /**@name Overrides from class PCollection */ //@{ /**Append a new object to the collection. This places a new link at the "tail" of the list. @return index of the newly added object. */ virtual PINDEX Append( PObject * obj /// New object to place into the collection. ); /**Insert a new object immediately before the specified object. If the object to insert before is not in the collection then the equivalent of the #Append()# function is performed. Note that the object values are compared for the search of the #before# parameter, not the pointers. So the objects in the collection must correctly implement the #PObject::Compare()# function. @return index of the newly inserted object. */ virtual PINDEX Insert( const PObject & before, /// Object value to insert before. PObject * obj /// New object to place into the collection. ); /**Insert a new object at the specified ordinal index. If the index is greater than the number of objects in the collection then the equivalent of the #Append()# function is performed. @return index of the newly inserted object. */ virtual PINDEX InsertAt( PINDEX index, /// Index position in collection to place the object. PObject * obj /// New object to place into the collection. ); /**Remove the object from the collection. If the AllowDeleteObjects option is set then the object is also deleted. @return TRUE if the object was in the collection. */ virtual BOOL Remove( const PObject * obj /// Existing object to remove from the collection. ); /**Remove the object at the specified ordinal index from the collection. If the AllowDeleteObjects option is set then the object is also deleted. Note if the index is beyond the size of the collection then the function will assert. @return pointer to the object being removed, or NULL if it was deleted. */ virtual PObject * RemoveAt( PINDEX index /// Index position in collection to place the object. ); /**Set the object at the specified ordinal position to the new value. This will overwrite the existing entry. If the AllowDeleteObjects option is set then the old object is also deleted. The object accessed in this way is remembered by the class and further access will be fast. Access to elements one either side of that saved element, and the head and tail of the list, will always be fast. Note if the index is beyond the size of the collection then the function will assert. @return TRUE if the object was successfully added. */ virtual BOOL SetAt( PINDEX index, /// Index position in collection to set. PObject * val /// New value to place into the collection. ); /**Get the object at the specified ordinal position. If the index was greater than the size of the collection then NULL is returned. The object accessed in this way is remembered by the class and further access will be fast. Access to elements one either side of that saved element, and the head and tail of the list, will always be fast. @return pointer to object at the specified index. */ virtual PObject * GetAt( PINDEX index // Index position in the collection of the object. ) const; /**Search the collection for the specific instance of the object. The object pointers are compared, not the values. A simple linear search from "head" of the list is performed. @return ordinal index position of the object, or P_MAX_INDEX. */ virtual PINDEX GetObjectsIndex( const PObject * obj /// Object to find. ) const; /**Search the collection for the specified value of the object. The object values are compared, not the pointers. So the objects in the collection must correctly implement the #PObject::Compare()# function. A simple linear search from "head" of the list is performed. @return ordinal index position of the object, or P_MAX_INDEX. */ virtual PINDEX GetValuesIndex( const PObject & obj /// Object to find value of. ) const; //@} protected: /**Get the object at the specified ordinal position. If the index was greater than the size of the collection then this asserts. The object accessed in this way is remembered by the class and further access will be fast. Access to elements one either side of that saved element, and the head and tail of the list, will always be fast. @return reference to object at the specified index. */ PINLINE PObject & GetReferenceAt( PINDEX index /// Ordinal index of the list element to set as current. ) const; /**Move the internal "cursor" to the index position specified. This function will optimise the sequential move taking into account the previous current position and the position at the head and tail of the list. Whichever of these three points is closes is used as the starting point for a sequential move to the required index. @return TRUE if the index could be set as the current element. */ BOOL SetCurrent( PINDEX index /// Ordinal index of the list element to set as current. ) const; class Element { public: Element(PObject * theData); Element * prev; Element * next; PObject * data; }; class Info { public: Info() { head = tail = lastElement = NULL; } Element * head; Element * tail; Element * lastElement; PINDEX lastIndex; } * info;};#ifdef PHAS_TEMPLATES/**This template class maps the PAbstractList to a specific object type. The functions in this class primarily do all the appropriate casting of types. Note that if templates are not used the #PDECLARE_LIST# macro will simulate the template instantiation. */template <class T> class PList : public PAbstractList{ PCLASSINFO(PList, PAbstractList); public: /**@name Construction */ //@{ /**Create a new, empty, list. Note that by default, objects placed into the list will be deleted when removed or when all references to the list are destroyed. */ PList() : PAbstractList() { } //@} /**@name Overrides from class PObject */ //@{ /**Make a complete duplicate of the list. Note that all objects in the array are also cloned, so this will make a complete copy of the list. */ virtual PObject * Clone() const { return PNEW PList(0, this); } //@} /**@name New functions for class */ //@{ /**Retrieve a reference to the object in the list. If there was not an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -