Changeset 463 for trunk/Common/alax.info/roatlcollections.h
- Timestamp:
- May 25, 2015, 10:37:24 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Common/alax.info/roatlcollections.h
r78 r463 1 1 //////////////////////////////////////////////////////////// 2 // Copyright (C) Roman Ryltsov, 2006-201 22 // Copyright (C) Roman Ryltsov, 2006-2015 3 3 // Created by Roman Ryltsov roman@alax.info 4 //5 // $Id$6 4 // 7 5 // A permission to use the source code is granted as long as reference to … … 13 11 14 12 //////////////////////////////////////////////////////////// 13 // CRoIterativeTraitsBase 14 15 class CRoIterativeTraitsBase 16 { 17 public: 18 19 //////////////////////////////////////////////////////// 20 // CAddressT 21 22 template <typename CValue> 23 class CAddressT 24 { 25 public: 26 CValue m_Value; 27 28 public: 29 // CAddressT 30 const CValue* operator & () const 31 { 32 return reinterpret_cast<const CValue*>(this); 33 } 34 CValue* operator & () 35 { 36 return reinterpret_cast<CValue*>(this); 37 } 38 }; 39 40 public: 41 // CRoIterativeTraitsBase 42 }; 43 44 //////////////////////////////////////////////////////////// 15 45 // CRoIterativeArrayTraitsT 16 46 17 template <typename _Array> 18 class CRoIterativeArrayTraitsT 47 template <typename CArray> 48 class CRoIterativeArrayTraitsT : 49 public CRoIterativeTraitsBase 19 50 { 20 51 protected: … … 29 60 { 30 61 public: 31 const _Array* m_pArray;62 const CArray* m_pArray; 32 63 KEY m_nIndex; 33 64 34 65 public: 35 66 // CIterator 36 CIterator(const _Array* pArray) throw() :67 CIterator(const CArray* pArray) throw() : 37 68 m_pArray(pArray), 38 69 m_nIndex(0) … … 60 91 { 61 92 public: 62 const _Array* m_pArray;93 const CArray* m_pArray; 63 94 KEY m_nIndex; 64 95 65 96 public: 66 97 // CReverseIterator 67 CReverseIterator(const _Array* pArray) throw() :98 CReverseIterator(const CArray* pArray) throw() : 68 99 m_pArray(pArray), 69 100 m_nIndex(pArray->GetCount() - 1) … … 85 116 }; 86 117 118 //////////////////////////////////////////////////////// 119 // CRangeIterator 120 121 class CRangeIterator 122 { 123 public: 124 const CArray& m_Array; 125 SIZE_T m_nIndex; 126 127 public: 128 // CRangeIterator 129 ATL_FORCEINLINE CRangeIterator(const CArray* pArray, SIZE_T nIndex) : 130 m_Array(*pArray), 131 m_nIndex(nIndex) 132 { 133 _A(pArray); 134 } 135 ATL_FORCEINLINE BOOL operator == (const CRangeIterator& Value) const 136 { 137 _A(&m_Array == &Value.m_Array); 138 return m_nIndex == Value.m_nIndex; 139 } 140 ATL_FORCEINLINE BOOL operator != (const CRangeIterator& Value) const 141 { 142 return !(*this == Value); 143 } 144 ATL_FORCEINLINE CRangeIterator& operator ++ () 145 { 146 ++m_nIndex; 147 return *this; 148 } 149 ATL_FORCEINLINE operator const typename CArray::CCollectionElement* () const 150 { 151 const typename CArray::CCollectionElement& Element = m_Array.GetAt(m_nIndex); 152 CAddressT<typename CArray::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CArray::CCollectionElement>&>(Element); 153 return &ElementAddress; 154 } 155 ATL_FORCEINLINE operator typename CArray::CCollectionElement* () 156 { 157 typename CArray::CCollectionElement& Element = const_cast<CArray&>(m_Array).GetAt(m_nIndex); 158 CAddressT<typename CArray::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CArray::CCollectionElement>&>(Element); 159 return &ElementAddress; 160 } 161 ATL_FORCEINLINE static CRangeIterator begin(const CArray* pArray) 162 { 163 return CRangeIterator(pArray, 0); 164 } 165 ATL_FORCEINLINE static CRangeIterator end(const CArray* pArray) 166 { 167 return CRangeIterator(pArray, pArray->GetCount()); 168 } 169 }; 170 87 171 public: 88 172 // CRoIterativeArrayTraitsT … … 92 176 // CRoIterativeListTraitsT 93 177 94 template <typename _List> 95 class CRoIterativeListTraitsT 178 template <typename CList> 179 class CRoIterativeListTraitsT : 180 public CRoIterativeTraitsBase 96 181 { 97 182 protected: … … 106 191 { 107 192 public: 108 const _List* m_pList;193 const CList* m_pList; 109 194 KEY m_Position; 110 195 111 196 public: 112 197 // CIterator 113 CIterator(const _List* pList) throw() :198 CIterator(const CList* pList) throw() : 114 199 m_pList(pList), 115 200 m_Position(pList->GetHeadPosition()) … … 136 221 { 137 222 public: 138 const _List* m_pList;223 const CList* m_pList; 139 224 KEY m_Position; 140 225 141 226 public: 142 227 // CReverseIterator 143 CReverseIterator(const _List* pList) throw() :228 CReverseIterator(const CList* pList) throw() : 144 229 m_pList(pList), 145 230 m_Position(pList->GetTailPosition()) … … 159 244 } 160 245 }; 246 247 //////////////////////////////////////////////////////// 248 // CRangeIterator 249 250 class CRangeIterator 251 { 252 public: 253 const CList& m_List; 254 POSITION m_Position; 255 256 public: 257 // CRangeIterator 258 ATL_FORCEINLINE CRangeIterator(const CList* pList, POSITION Position) : 259 m_List(*pList), 260 m_Position(Position) 261 { 262 _A(pList); 263 } 264 ATL_FORCEINLINE BOOL operator == (const CRangeIterator& Value) const 265 { 266 _A(&m_List == &Value.m_List); 267 return m_Position == Value.m_Position; 268 } 269 ATL_FORCEINLINE BOOL operator != (const CRangeIterator& Value) const 270 { 271 return !(*this == Value); 272 } 273 ATL_FORCEINLINE CRangeIterator& operator ++ () 274 { 275 m_List.GetNext(m_Position); 276 return *this; 277 } 278 ATL_FORCEINLINE operator const typename CList::CCollectionElement* () const 279 { 280 _A(m_Position); 281 const typename CList::CCollectionElement& Element = m_List.GetAt(m_Position); 282 CAddressT<typename CList::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CList::CCollectionElement>&>(Element); 283 return &ElementAddress; 284 } 285 ATL_FORCEINLINE operator typename CList::CCollectionElement* () 286 { 287 _A(m_Position); 288 typename CList::CCollectionElement& Element = const_cast<CList&>(m_List).GetAt(m_Position); 289 CAddressT<typename CList::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CList::CCollectionElement>&>(Element); 290 return &ElementAddress; 291 } 292 ATL_FORCEINLINE static CRangeIterator begin(const CList* pList) 293 { 294 return CRangeIterator(pList, pList->GetHeadPosition()); 295 } 296 ATL_FORCEINLINE static CRangeIterator end(const CList* pList) 297 { 298 return CRangeIterator(pList, NULL); 299 } 300 }; 301 302 public: 303 // CRoIterativeListTraitsT 161 304 }; 162 305 … … 164 307 // CRoIterativeCollectionT 165 308 166 template <typename T, template <typename _Collection> class _IterativeTraitsT, typename _Element, typename _ElementTraits = CElementTraits<_Element> >309 template <typename T, template <typename _Collection> class CIterativeTraits, typename _Element, typename _ElementTraits = CElementTraits<_Element> > 167 310 class CRoIterativeCollectionT : 168 protected _IterativeTraitsT<T>311 protected CIterativeTraits<T> 169 312 { 170 313 protected: 171 typedef typename _IterativeTraitsT<T>::KEY KEY;314 typedef typename CIterativeTraits<T>::KEY KEY; 172 315 173 316 public: … … 277 420 return RemoveThatT<CReverseIterator>(pCompareElement); 278 421 } 422 BOOL RemoveFirst(typename _ElementTraits::INARGTYPE Element) 423 { 424 T* pT = static_cast<T*>(this); 425 KEY Key; 426 if(!pT->FindFirst(Element, &Key)) 427 return FALSE; 428 pT->RemoveAt(Key); 429 return TRUE; 430 } 431 BOOL RemoveLast(typename _ElementTraits::INARGTYPE Element) 432 { 433 T* pT = static_cast<T*>(this); 434 KEY Key; 435 if(!pT->FindLast(Element, &Key)) 436 return FALSE; 437 pT->RemoveAt(Key); 438 return TRUE; 439 } 279 440 //} 280 441 template <typename _Iterator> … … 390 551 } 391 552 // TODO: ForEachThat 553 554 // Range Iterator 555 ATL_FORCEINLINE typename CIterativeTraits<T>::CRangeIterator begin() const 556 { 557 return CRangeIterator::begin(static_cast<const T*>(this)); 558 } 559 ATL_FORCEINLINE typename CIterativeTraits<T>::CRangeIterator end() const 560 { 561 return CRangeIterator::end(static_cast<const T*>(this)); 562 } 392 563 }; 393 564 … … 400 571 public CRoIterativeCollectionT<CRoArrayT<_Element, _ElementTraits>, CRoIterativeArrayTraitsT, _Element, _ElementTraits> 401 572 { 573 public: 402 574 typedef CAtlArray<_Element, _ElementTraits> CBaseArray; 575 typedef _Element CCollectionElement; 403 576 404 577 private: … … 473 646 { 474 647 public: 648 typedef CRoListT<_Element, _ElementTraits> CRoList; 649 typedef _Element CCollectionElement; 650 651 public: 475 652 // CRoListT 476 653 BOOL FindPositionIndex(POSITION Position, SIZE_T* pnIndex = NULL) const throw() … … 711 888 template<typename _KeyElement, typename _Element, class _KeyElementTraits = CElementTraits<_KeyElement>, class _ElementTraits = CElementTraits<_Element> > 712 889 class CRoMapT : 713 public CAtlMap<_KeyElement, _Element, _KeyElementTraits, _ElementTraits> 890 public CAtlMap<_KeyElement, _Element, _KeyElementTraits, _ElementTraits>, 891 public CRoIterativeTraitsBase 714 892 { 715 893 public: 894 typedef CAtlMap<_KeyElement, _Element, _KeyElementTraits, _ElementTraits> CBaseMap; 895 typedef CRoMapT<_KeyElement, _Element, _KeyElementTraits, _ElementTraits> CMap; 896 typedef _Element CCollectionElement; 897 898 //////////////////////////////////////////////////////// 899 // CPositionRangeIterator 900 901 class CPositionRangeIterator 902 { 903 public: 904 const CMap& m_Map; 905 POSITION m_Position; 906 907 public: 908 // CPositionRangeIterator 909 ATL_FORCEINLINE CPositionRangeIterator(const CMap* pMap, POSITION Position) : 910 m_Map(*pMap), 911 m_Position(Position) 912 { 913 _A(pMap); 914 } 915 ATL_FORCEINLINE BOOL operator == (const CPositionRangeIterator& Position) const 916 { 917 _A(&m_Map == &Position.m_Map); 918 return m_Position == Position.m_Position; 919 } 920 ATL_FORCEINLINE BOOL operator != (const CPositionRangeIterator& Position) const 921 { 922 return !(*this == Position); 923 } 924 ATL_FORCEINLINE CPositionRangeIterator& operator ++ () 925 { 926 m_Map.GetNext(m_Position); 927 return *this; 928 } 929 ATL_FORCEINLINE operator const POSITION* () const 930 { 931 return &m_Position; 932 } 933 ATL_FORCEINLINE operator typename POSITION* () 934 { 935 return &m_Position; 936 } 937 ATL_FORCEINLINE static CPositionRangeIterator begin(const CMap* pMap) 938 { 939 return CPositionRangeIterator(pMap, pMap->GetStartPosition()); 940 } 941 ATL_FORCEINLINE static CPositionRangeIterator end(const CMap* pMap) 942 { 943 return CPositionRangeIterator(pMap, NULL); 944 } 945 }; 946 947 //////////////////////////////////////////////////////// 948 // CPositions 949 950 class CPositions 951 { 952 public: 953 const CMap* m_pMap; 954 955 public: 956 // CPositions 957 ATL_FORCEINLINE CPositions(const CMap* pMap) : 958 m_pMap(pMap) 959 { 960 _A(pMap); 961 } 962 963 // Range Iterator 964 ATL_FORCEINLINE CPositionRangeIterator begin() const 965 { 966 return CPositionRangeIterator::begin(m_pMap); 967 } 968 ATL_FORCEINLINE CPositionRangeIterator end() const 969 { 970 return CPositionRangeIterator::end(m_pMap); 971 } 972 }; 973 974 //////////////////////////////////////////////////////// 975 // CValueRangeIterator 976 977 class CValueRangeIterator 978 { 979 public: 980 const CMap& m_Map; 981 POSITION m_Position; 982 983 public: 984 // CValueRangeIterator 985 ATL_FORCEINLINE CValueRangeIterator(const CMap* pMap, POSITION Position) : 986 m_Map(*pMap), 987 m_Position(Position) 988 { 989 _A(pMap); 990 } 991 ATL_FORCEINLINE BOOL operator == (const CValueRangeIterator& Value) const 992 { 993 _A(&m_Map == &Value.m_Map); 994 return m_Position == Value.m_Position; 995 } 996 ATL_FORCEINLINE BOOL operator != (const CValueRangeIterator& Value) const 997 { 998 return !(*this == Value); 999 } 1000 ATL_FORCEINLINE CValueRangeIterator& operator ++ () 1001 { 1002 m_Map.GetNext(m_Position); 1003 return *this; 1004 } 1005 ATL_FORCEINLINE operator const typename CMap::CCollectionElement* () const 1006 { 1007 _A(m_Position); 1008 const typename CMap::CCollectionElement& Element = m_Map.GetValueAt(m_Position); 1009 CAddressT<typename CMap::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CMap::CCollectionElement>&>(Element); 1010 return &ElementAddress; 1011 } 1012 ATL_FORCEINLINE operator typename CMap::CCollectionElement* () 1013 { 1014 _A(m_Position); 1015 typename CMap::CCollectionElement& Element = const_cast<CMap&>(m_Map).GetValueAt(m_Position); 1016 CAddressT<typename CMap::CCollectionElement>& ElementAddress = reinterpret_cast<CAddressT<typename CMap::CCollectionElement>&>(Element); 1017 return &ElementAddress; 1018 } 1019 ATL_FORCEINLINE static CValueRangeIterator begin(const CMap* pMap) 1020 { 1021 return CValueRangeIterator(pMap, pMap->GetStartPosition()); 1022 } 1023 ATL_FORCEINLINE static CValueRangeIterator end(const CMap* pMap) 1024 { 1025 return CValueRangeIterator(pMap, NULL); 1026 } 1027 }; 1028 1029 //////////////////////////////////////////////////////// 1030 // CValues 1031 1032 class CValues 1033 { 1034 public: 1035 const CMap* m_pMap; 1036 1037 public: 1038 // CValues 1039 ATL_FORCEINLINE CValues(const CMap* pMap) : 1040 m_pMap(pMap) 1041 { 1042 _A(pMap); 1043 } 1044 1045 // Range Iterator 1046 ATL_FORCEINLINE CValueRangeIterator begin() const 1047 { 1048 return CValueRangeIterator::begin(m_pMap); 1049 } 1050 ATL_FORCEINLINE CValueRangeIterator end() const 1051 { 1052 return CValueRangeIterator::end(m_pMap); 1053 } 1054 }; 1055 1056 public: 716 1057 // CRoMapT 1058 CPositions GetPositions() const 1059 { 1060 return CPositions(this); 1061 } 1062 CValues GetValues() const 1063 { 1064 return CValues(this); 1065 } 717 1066 }; 718 1067
Note: See TracChangeset
for help on using the changeset viewer.