Back to index

im-sdk  12.3.91
IMList.hh
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 #ifndef _imlist_hh
00043 #define _imlist_hh
00044 
00045 #include <stdlib.h>
00046 
00047 template <class T>
00048 class IMList { // append an item to list
00049  public:
00050   IMList();
00051   ~IMList() {
00052     delete list;
00053   }
00054   void addItem(T* );
00055   void addItemByKey(T*, int);
00056   T& getItem(unsigned int) const;
00057   T& getItemByKey(int) const;
00058   T& getItemMakeTop(unsigned int);
00059   T& getFirstItem(void);
00060   T& getNextItem(void);
00061   unsigned int getIndex(T*) const;
00062   int getSize() const {return nSize;}
00063   void remove(T*);
00064   void destroy() ;
00065 
00066   // don't re-order index of the list when an item is removed
00067   void disable_reordering();
00068  protected:
00069   typedef struct _Tnode {
00070     T* obj;
00071     int key;
00072     unsigned int index;
00073     struct _Tnode *next;
00074   } Tnode;
00075   Tnode *list;
00076   Tnode *next_node;
00077   Tnode null_obj;
00078   int nSize;
00079   unsigned int nIndex;
00080   int reorder;
00081  private:
00082   void *getNodeByKey(int) const;
00083 };
00084 
00085 template <class T>
00086 IMList<T>::IMList() {
00087   nSize = nIndex = 0;
00088   list = (Tnode *)0;
00089   next_node = (Tnode *)0;
00090   null_obj.obj = (T*)0;
00091   null_obj.key = 0;
00092   null_obj.index = 0;
00093   null_obj.next = 0;
00094   reorder = 1;
00095 }
00096 
00097 template <class T>
00098 void IMList<T>::disable_reordering() {
00099   reorder = 0;
00100 }
00101 
00102 template <class T>
00103 void IMList<T>::addItem(T* item) {
00104   Tnode *node = new Tnode;
00105   node->obj  = item;
00106   nIndex++;
00107   node->index = nIndex;
00108   node->next = list;
00109   list = node;
00110   nSize++;
00111 }
00112 
00113 template <class T>
00114 void IMList<T>::addItemByKey(T* item, int key) {
00115   Tnode *node = (Tnode*)getNodeByKey(key);
00116   if (!node) {
00117     node = new Tnode;
00118     node->obj  = item;
00119     node->key  = key;
00120     nIndex++;
00121     node->index = nIndex;
00122     node->next = list;
00123     list = node;
00124     nSize++;
00125   } else {
00126     node->obj = item;
00127   }
00128 }
00129 
00130 template <class T>
00131 void IMList<T>::remove(T* item) {
00132   Tnode **prev, *tnode;
00133   int index=1;
00134   for (prev = &list; tnode = *prev;) {
00135     if (tnode->obj == item) {
00136       *prev = tnode->next;
00137       if (next_node == tnode) next_node = tnode->next;      
00138       delete tnode;
00139       nSize--;
00140       if (reorder == 0) break;
00141     } else {
00142       prev = &tnode->next;
00143       if (reorder == 1) tnode->index=index++;
00144     }
00145   }
00146   if (reorder == 1) nIndex=nSize;
00147   return;
00148 }
00149 
00150 template <class T>
00151 T& IMList<T>::getItemMakeTop(unsigned int index) {
00152   Tnode * tnode = (Tnode *)list;
00153   Tnode * tnode_prev = NULL;
00154 
00155   if (index == 0 || index > nIndex)
00156     return *(null_obj.obj);
00157 
00158   if (tnode == 0 && nSize == 0 && reorder == 0)  // not error
00159     return *(null_obj.obj);
00160 
00161   while (tnode) {
00162     if (tnode->index == index) {
00163       if (NULL != tnode_prev) {
00164        tnode_prev->next = tnode->next;
00165        tnode->next = list;
00166        list = tnode;
00167       }
00168       return *(tnode->obj);
00169     }
00170     tnode_prev = tnode;
00171     tnode = tnode->next;
00172   }
00173   if (tnode == 0)    // should not be an error
00174     return *(null_obj.obj);
00175 
00176   return *(tnode->obj);
00177 }
00178 
00179 template <class T>
00180 T& IMList<T>::getFirstItem(void) {
00181   Tnode * tnode = (Tnode *)list;
00182   if(tnode==0){
00183      return *(null_obj.obj);
00184   }
00185   next_node=tnode->next;
00186   return *(tnode->obj);
00187 }
00188 
00189 template <class T>
00190 T& IMList<T>::getNextItem(void) {
00191   Tnode * tnode = (Tnode *)next_node;
00192   if(tnode==0){
00193      return *(null_obj.obj);
00194   }
00195   next_node=tnode->next;
00196   return *(tnode->obj);
00197 }
00198 
00199 template <class T>
00200 unsigned int IMList<T>::getIndex(T *item) const {
00201   Tnode *tnode = (Tnode *)list;
00202 
00203   if (item == 0) return 0;
00204   while (tnode) {
00205     if (tnode->obj == item) return tnode->index;
00206     tnode = tnode->next;
00207   }
00208   return 0;
00209 }
00210 
00211 template <class T>
00212 T& IMList<T>::getItem(unsigned int index) const {
00213   Tnode * tnode = (Tnode *)list;
00214 
00215   if (index == 0 || index > nIndex)
00216     return *(null_obj.obj);
00217 
00218   if (tnode == 0 && nSize == 0 && reorder == 0)  // not error
00219     return *(null_obj.obj);
00220 
00221   while (tnode) {
00222     if (tnode->index == index) return *(tnode->obj);
00223     tnode = tnode->next;
00224   }
00225   if (tnode == 0)    // should not be an error
00226     return *(null_obj.obj);
00227 
00228   return *(tnode->obj);
00229 }
00230 
00231 template <class T>
00232 T& IMList<T>::getItemByKey(int key) const {
00233   Tnode *node = (Tnode*)getNodeByKey(key);
00234   if (!node) return *(null_obj.obj);
00235   return *(node->obj);
00236 }
00237 
00238 template <class T>
00239 void* IMList<T>::getNodeByKey(int key) const {
00240   Tnode *tnode = (Tnode *)list;
00241 
00242   while (tnode) {
00243     if (tnode->key == key) return (void*)tnode;
00244     tnode = tnode->next;
00245   }
00246   return (void*)0;
00247 }
00248 
00249 template <class T>
00250 void IMList<T>::destroy() {
00251   Tnode *tnode = (Tnode *)list;
00252   Tnode *del_node = (Tnode *)0;
00253 
00254   while (tnode) {
00255     del_node = tnode;
00256     tnode = tnode->next;
00257     //delete del_node->obj;
00258     delete del_node;
00259   }
00260   list = (Tnode *)0;
00261   nSize = 0;
00262   nIndex = 0;
00263 }
00264   
00265 #endif