VR-Forces 4.0.4 Class Documentation
include/ngutil/orderedKeyList.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002 ** Copyright (c) 2004 MAK Technologies, Inc.
00003 ** All rights reserved.
00004 *******************************************************************************/
00005 /*******************************************************************************
00006 ** $RCSfile: orderedKeyList.h,v $ $Revision: 1.3 $ $State: Exp $
00007 *******************************************************************************/
00008 
00011 
00012 #ifndef ORDEREDKEYLIST_H_
00013 #define ORDEREDKEYLIST_H_
00014 
00018 
00019 #include <QtCore/QList>
00020 #include "ngutil/entityMapperKey.h"
00021 
00022 template <typename T>
00023 class DtOrderedKeyList : public QList<T>
00024 {
00025 public:
00026    DtOrderedKeyList();
00027    DtOrderedKeyList(const DtOrderedKeyList&);
00028 
00029    DtOrderedKeyList& operator=(const DtOrderedKeyList&);
00030 
00031    virtual ~DtOrderedKeyList();
00032 
00033    void clear();
00034    void copyList(const DtOrderedKeyList&);
00035 
00037    void addEntry(T, bool replace = false);
00038    T removeEntry(const DtEntityMapperKey& key);
00039    T find(const DtEntityMapperKey& key) const;
00040 };
00041 
00042 template <typename T>
00043 DtOrderedKeyList<T>::DtOrderedKeyList()
00044 {
00045 }
00046 
00047 template <typename T>
00048 DtOrderedKeyList<T>::DtOrderedKeyList(const DtOrderedKeyList<T>& orig)
00049 {
00050    clear();
00051    copyList(orig);
00052 }
00053 
00054 template <typename T>
00055 DtOrderedKeyList<T>& DtOrderedKeyList<T>::operator=(const DtOrderedKeyList<T>& rhs)
00056 {
00057    if (this != &rhs)
00058    {
00059       clear();
00060       copyList(rhs);
00061    }
00062 
00063    return *this;
00064 }
00065  
00066 template <typename T>
00067 DtOrderedKeyList<T>::~DtOrderedKeyList()
00068 {
00069    clear();
00070 }
00071 
00072 template <typename T>
00073 void DtOrderedKeyList<T>::clear()
00074 {
00075    typename QList<T>::iterator it = this->begin();
00076 
00077    while (it != this->end())
00078    {
00079       delete *it;
00080       ++it;
00081    }
00082 
00083    QList<T>::clear();
00084 }
00085 
00086 template <typename T>
00087 void DtOrderedKeyList<T>::copyList(const DtOrderedKeyList<T>& rhs)
00088 {
00089    typename QList<T>::const_iterator it = rhs.begin();
00090 
00091    while (it != rhs.end())
00092    {
00093       addEntry((*it)->clone());
00094       ++it;
00095    }
00096 }
00097 
00098 template <typename T>
00099 void DtOrderedKeyList<T>::addEntry(T entry, bool replace)
00100 {
00101    if (this->count() == 0)
00102    {
00103       append(entry);
00104 
00105       return;
00106    }
00107 
00109    T item = this->last();
00110    DtEntityMapperKey sdKey = *entry;
00111 
00112    if (*item < sdKey)
00113    {
00114       append(entry);
00115       return;
00116    }
00117 
00118    item = this->first();
00119 
00120    if (*item > sdKey)
00121    {
00122       insert(0, entry);
00123       return;
00124    }
00125 
00126    QString strKey = entry->string().c_str();
00127 
00128    unsigned int iLower = 0;
00129    unsigned int iUpper = this->count() - 1;
00130 
00131    while ((iUpper - iLower) != 1)
00132    {
00133       int iPos = (iUpper + iLower) / 2;
00134 
00135       if (sdKey < *this->at(iPos))
00136       {
00137          iUpper = iPos;
00138       }
00139       else if (sdKey > *this->at(iPos))
00140       {
00141          iLower = iPos;
00142       }
00143       else if ((sdKey == *this->at(iPos)) && replace)
00144       {
00145          *this->at(iPos) = *entry;
00146 
00147          return;
00148       }
00149    }
00150 
00151    insert(iLower + 1, entry);
00152 }
00153 
00154 template <typename T>
00155 T DtOrderedKeyList<T>::find(const DtEntityMapperKey& key) const
00156 {
00157    typename QList<T>::const_iterator iter = this->begin();
00158 
00159    while (iter != this->end())
00160    {
00161       if (**iter == key)
00162       {
00163          return *iter;
00164       }
00165 
00166       ++iter;
00167    }
00168 
00169    return T();
00170 }
00171 
00172 template <typename T>
00173 T DtOrderedKeyList<T>::removeEntry(const DtEntityMapperKey& key)
00174 {
00175    typename QList<T>::iterator iter = this->begin();
00176 
00177    while (iter != this->end())
00178    {
00179       if (**iter == key)
00180       {
00181          T item = *iter;
00182 
00183          erase(iter);
00184 
00185          return item;
00186       }
00187 
00188       ++iter;
00189    }
00190 
00191    return NULL;
00192 }
00193 
00194 #endif //ORDEREDKEYLIST_H_

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)