File indexing completed on 2024-05-12 05:39:54

0001 #ifndef INSERTIONSORTEDLIST_H
0002 #define INSERTIONSORTEDLIST_H
0003 
0004 #include <QHash>
0005 #include <QList>
0006 
0007 // template<typename Key,typename Value> class InsertionSortedList;
0008 #include <core_global.h>
0009 template <class Key, class Value>
0010 class CORE_EXPORT InsertionSortedMap
0011 {
0012 public:
0013     InsertionSortedMap();
0014 
0015     void append(const Key& key, const Value& value);
0016     void insert(int pos, const Key& key, const Value& value);
0017     void prepend(const Key& key, const Value& value);
0018 
0019     const Value getValue(const Key& key) const;
0020     const Value getValueFromIndex(int index) const;
0021 
0022     int size();
0023 
0024 private:
0025     QList<Key> m_keys;
0026     QHash<Key, Value> m_data;
0027 };
0028 
0029 template <class Key, class Value>
0030 InsertionSortedMap<Key, Value>::InsertionSortedMap()
0031 {
0032 }
0033 
0034 template <class Key, class Value>
0035 void InsertionSortedMap<Key, Value>::append(const Key& key, const Value& value)
0036 {
0037     m_keys.append(key);
0038     m_data.insert(key, value);
0039 }
0040 
0041 template <class Key, class Value>
0042 void InsertionSortedMap<Key, Value>::insert(int pos, const Key& key, const Value& value)
0043 {
0044     m_keys.insert(pos, key);
0045     m_data.insert(key, value);
0046 }
0047 
0048 template <class Key, class Value>
0049 void InsertionSortedMap<Key, Value>::prepend(const Key& key, const Value& value)
0050 {
0051     m_keys.prepend(key);
0052     m_data.insert(key, value);
0053 }
0054 
0055 template <class Key, class Value>
0056 const Value InsertionSortedMap<Key, Value>::getValue(const Key& key) const
0057 {
0058     return m_data.value(key);
0059 }
0060 
0061 template <class Key, class Value>
0062 const Value InsertionSortedMap<Key, Value>::getValueFromIndex(int index) const
0063 {
0064     Q_ASSERT(index >= 0);
0065     Q_ASSERT(index < m_keys.size());
0066 
0067     return m_data.value(m_keys.at(index));
0068 }
0069 
0070 template <class Key, class Value>
0071 int InsertionSortedMap<Key, Value>::size()
0072 {
0073     return m_keys.size();
0074 }
0075 #endif // INSERTIONSORTEDLIST_H