24 #ifndef OdVector_H_INCLUDED
25 #define OdVector_H_INCLUDED
34 template <
class T,
class A = OdObjectsAllocator<T>,
class Mm = OdrxMemoryManager>
class OdVector;
53 template <
class T,
class A,
class Mm>
class OdVector
465 #define VEC_SIZE_TYPE typename OdVector<T, A, Mm>::size_type
466 #define VEC_ITERATOR typename OdVector<T, A, Mm>::iterator
467 #define VEC_CONST_ITERATOR typename OdVector<T, A, Mm>::const_iterator
470 template<
class T,
class A,
class Mm>
472 : m_pData(
NULL), m_physicalLength(physicalLength), m_logicalLength(0)
473 , m_growLength(growLength)
475 if(m_growLength == 0)
480 if (m_physicalLength)
482 m_pData = allocate(m_physicalLength);
486 template <
class T,
class A,
class Mm>
488 : m_pData(
NULL), m_physicalLength(0), m_logicalLength(0), m_growLength(-200)
492 template <
class T,
class A,
class Mm>
494 : m_pData(
NULL), m_physicalLength(vec.m_physicalLength)
495 , m_logicalLength(vec.m_logicalLength), m_growLength(vec.m_growLength)
497 if(m_physicalLength > 0)
499 m_pData = allocate(m_physicalLength);
501 A::copy(m_pData, vec.m_pData, m_logicalLength);
505 template <
class T,
class A,
class Mm>
511 template <
class T,
class A,
class Mm>
516 if(m_physicalLength < vec.m_logicalLength)
520 m_pData = allocate(vec.m_logicalLength);
521 m_physicalLength = vec.m_logicalLength;
524 m_logicalLength = vec.m_logicalLength;
526 A::copy(m_pData, vec.m_pData, m_logicalLength);
531 template <
class T,
class A,
class Mm>
539 T* pData = ((numByte >= physicalLength)
540 ?
reinterpret_cast<T*
>(Mm::Alloc(numByte))
549 template <
class T,
class A,
class Mm>
554 A::destroy(m_pData, m_logicalLength);
558 m_physicalLength = 0;
562 template <
class T,
class A,
class Mm>
565 T* pOldData = m_pData;
568 if(!isForcePhysicalLength)
572 newPhysicalLength = ((newPhysicalLength + m_growLength - 1)/m_growLength) * m_growLength;
576 newPhysicalLength = m_logicalLength + (-m_growLength)*m_logicalLength/100;
578 if(newPhysicalLength < physicalLength)
579 newPhysicalLength = physicalLength;
583 if(isUseRealloc && A::useRealloc() && m_logicalLength > 0 && m_pData !=
NULL)
585 m_pData =
reinterpret_cast<T*
>(Mm::Realloc(pOldData, newPhysicalLength*
sizeof(T), m_physicalLength*
sizeof(T)));
590 m_physicalLength = newPhysicalLength;
592 if(physicalLength < m_logicalLength)
593 m_logicalLength = physicalLength;
597 T* pNewData = allocate(newPhysicalLength);
600 A::constructn(pNewData, pOldData, newLogicalLength);
605 m_physicalLength = newPhysicalLength;
606 m_logicalLength = newLogicalLength;
610 template <
class T,
class A,
class Mm>
614 return (
index < m_logicalLength);
617 template <
class T,
class A,
class Mm>
627 template <
class T,
class A,
class Mm>
634 template <
class T,
class A,
class Mm>
640 template <
class T,
class A,
class Mm>
646 template <
class T,
class A,
class Mm>
652 template <
class T,
class A,
class Mm>
658 template <
class T,
class A,
class Mm>
661 return (!isEmpty() ? m_pData :
NULL);
664 template <
class T,
class A,
class Mm>
667 return (!isEmpty() ? m_pData :
NULL);
670 template <
class T,
class A,
class Mm>
673 return (!isEmpty() ? m_pData + m_logicalLength :
NULL);
676 template <
class T,
class A,
class Mm>
679 return (!isEmpty() ? m_pData + m_logicalLength :
NULL);
682 template <
class T,
class A,
class Mm>
690 if(afterLast > first)
693 const VEC_SIZE_TYPE newLogicalLength = oldLogicalLength + numElem;
695 if(newLogicalLength > m_physicalLength)
696 reallocate(newLogicalLength, first < begin() || first >= end());
698 A::constructn(m_pData + oldLogicalLength, first, numElem);
700 m_logicalLength = newLogicalLength;
702 T* pData = m_pData +
index;
704 if(
index != oldLogicalLength)
705 A::move(pData + numElem, pData, oldLogicalLength -
index);
707 A::copy(pData, first, numElem);
711 riseError(eInvalidInput);
714 template <
class T,
class A,
class Mm>
718 const VEC_SIZE_TYPE newLogicalLength = oldLogicalLength + numElem;
721 if(newLogicalLength > m_physicalLength)
722 reallocate(newLogicalLength, &
value < begin() || &
value >= end());
724 A::constructn(m_pData + oldLogicalLength, numElem,
value);
726 m_logicalLength = newLogicalLength;
728 T* pData = m_pData +
index;
730 if(
index != oldLogicalLength)
731 A::move(pData + numElem, pData, oldLogicalLength -
index);
734 pData[numElem] =
value;
736 return (begin_non_const() +
index);
739 template <
class T,
class A,
class Mm>
746 return (begin_non_const() +
index);
749 template <
class T,
class A,
class Mm>
755 if(
index == oldLogicalLength)
756 resize(newLogicalLength,
value);
757 else if(
index < oldLogicalLength)
759 if(newLogicalLength > m_physicalLength)
760 reallocate(newLogicalLength, &
value < begin() || &
value >= end());
762 A::construct(m_pData + oldLogicalLength);
766 T* pData = m_pData +
index;
768 A::move(pData + 1, pData, oldLogicalLength -
index);
773 riseError(eInvalidIndex);
778 template <
class T,
class A,
class Mm>
785 if(
index < newLogicalLength)
787 T* pData = m_pData +
index;
788 A::move(pData, pData + 1, newLogicalLength -
index);
791 resize(newLogicalLength);
795 template <
class T,
class A,
class Mm>
798 if(!isValid(startIndex) || startIndex > endIndex)
799 riseError(eInvalidIndex);
809 A::move(pData + startIndex, pData + endIndex, oldLogicalLength - endIndex);
810 A::destroy(pData + oldLogicalLength - numElem, numElem);
812 m_logicalLength -= numElem;
816 template <
class T,
class A,
class Mm>
828 template <
class T,
class A,
class Mm>
832 const int lengthDiff = logicalLength - oldLogicalLength;
836 if(logicalLength > m_physicalLength)
837 reallocate(logicalLength, &
value < begin() || &
value >= end());
839 A::constructn(m_pData + oldLogicalLength, lengthDiff,
value);
841 else if(lengthDiff < 0)
842 A::destroy(m_pData + logicalLength, -lengthDiff);
844 m_logicalLength = logicalLength;
847 template <
class T,
class A,
class Mm>
851 const int lengthDiff = logicalLength - oldLogicalLength;
855 if(logicalLength > m_physicalLength)
856 reallocate(logicalLength,
true);
857 A::constructn(m_pData + oldLogicalLength, lengthDiff);
859 else if(lengthDiff < 0)
860 A::destroy(m_pData + logicalLength, -lengthDiff);
862 m_logicalLength = logicalLength;
865 template <
class T,
class A,
class Mm>
868 return m_logicalLength;
871 template <
class T,
class A,
class Mm>
874 return (m_logicalLength == 0);
877 template <
class T,
class A,
class Mm>
880 return m_physicalLength;
883 template <
class T,
class A,
class Mm>
886 if(m_physicalLength < reserveLength)
887 setPhysicalLength(reserveLength);
890 template <
class T,
class A,
class Mm>
893 erase(begin_non_const(), end_non_const());
895 insert(begin_non_const(), first, afterLast);
898 template <
class T,
class A,
class Mm>
903 if(first != afterLast)
906 return (begin_non_const() +
index);
909 template <
class T,
class A,
class Mm>
916 return (begin_non_const() +
index);
919 template <
class T,
class A,
class Mm>
922 erase(begin_non_const(), end_non_const());
925 template <
class T,
class A,
class Mm>
928 insertAt(m_logicalLength,
value);
931 template <
class T,
class A,
class Mm>
939 template <
class T,
class A,
class Mm>
942 return m_logicalLength;
945 template <
class T,
class A,
class Mm>
948 return (m_logicalLength == 0);
951 template <
class T,
class A,
class Mm>
954 return m_logicalLength;
957 template <
class T,
class A,
class Mm>
960 return m_physicalLength;
963 template <
class T,
class A,
class Mm>
969 template <
class T,
class A,
class Mm>
975 template <
class T,
class A,
class Mm>
981 template <
class T,
class A,
class Mm>
988 template <
class T,
class A,
class Mm>
992 return m_pData[
index];
995 template <
class T,
class A,
class Mm>
999 return m_pData[
index];
1002 template <
class T,
class A,
class Mm>
1006 return m_pData[
index];
1009 template <
class T,
class A,
class Mm>
1013 return m_pData[
index];
1016 template <
class T,
class A,
class Mm>
1026 template <
class T,
class A,
class Mm>
1030 return m_pData[
index];
1033 template <
class T,
class A,
class Mm>
1039 template <
class T,
class A,
class Mm>
1045 template <
class T,
class A,
class Mm>
1048 return m_pData[m_logicalLength - 1];
1051 template <
class T,
class A,
class Mm>
1054 return m_pData[m_logicalLength - 1];
1057 template <
class T,
class A,
class Mm>
1060 insertAt(m_logicalLength,
value);
1061 return (m_logicalLength - 1);
1064 template <
class T,
class A,
class Mm>
1068 return (begin_non_const() +
index);
1071 template <
class T,
class A,
class Mm>
1074 insert(end_non_const(), vec.
begin(), vec.
end());
1078 template <
class T,
class A,
class Mm>
1084 template <
class T,
class A,
class Mm>
1087 return removeAt(m_logicalLength - 1);
1090 template <
class T,
class A,
class Mm>
1093 if(m_logicalLength == vec.m_logicalLength)
1097 if(m_pData[i] != vec.m_pData[i])
1105 template <
class T,
class A,
class Mm>
1114 template <
class T,
class A,
class Mm>
1119 assertValid(startIndex);
1123 if(m_pData[i] ==
value)
1133 template <
class T,
class A,
class Mm>
1136 resize(logicalLength);
1140 template <
class T,
class A,
class Mm>
1143 if(physicalLength == 0)
1148 m_physicalLength = 0;
1150 else if(physicalLength != m_physicalLength)
1151 reallocate(physicalLength,
true,
true);
1153 if(m_physicalLength < m_logicalLength)
1154 m_logicalLength = m_physicalLength;
1159 template <
class T,
class A,
class Mm>
1163 m_growLength = growLength;
1170 template <
class T,
class A,
class Mm>
1194 template <
class T,
class A,
class Mm>
1197 if(!isValid(firstIndex) || !isValid(secondIndex))
1198 riseError(eInvalidIndex);
1200 if(firstIndex != secondIndex)
1202 const T
value = m_pData[firstIndex];
1203 m_pData[firstIndex] = m_pData[secondIndex];
1204 m_pData[secondIndex] =
value;
1225 #endif // OdVector_H_INCLUDED