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

Document ID: Generated on Mon May 14 08:06:18 EDT 2012 from SVN revision 114750
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)