PCL_TT_Convertor.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2005-3-9
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __PCL_TT_CONVERTOR__2005_03_09__H__
  11. #define __PCL_TT_CONVERTOR__2005_03_09__H__
  12. #ifdef _MSC_VER
  13. #pragma warning (disable : 4786) // identifier was truncated to '255' characters in the browser information
  14. #endif
  15. #include <deque>
  16. #include <assert.h>
  17. //=============================================================================
  18. /**
  19. * Map table.
  20. */
  21. template<class T1, class T2>
  22. class PCL_TT_Convertor
  23. {
  24. public:
  25. /// T1 ==> T2
  26. T2 First_to_Second (const T1& t1, const T2& t2Default) const
  27. {
  28. int i = GetIndexT1(t1) ;
  29. return (i==-1) ? t2Default : m_t2Tab[i] ;
  30. }
  31. /// T2 ==> T1
  32. T1 Second_to_First (const T2& t2, const T1& t1Default) const
  33. {
  34. int i = GetIndexT2(t2) ;
  35. return (i==-1) ? t1Default : m_t1Tab[i] ;
  36. }
  37. /// Get element count.
  38. int GetElementCount() const
  39. {
  40. if (m_t1Tab.size() == m_t2Tab.size())
  41. return (int)m_t1Tab.size();
  42. assert(false) ;
  43. return 0 ;
  44. }
  45. /// Get 0-based index in array by t1, return -1 if not found.
  46. int GetIndexT1 (const T1& t1) const
  47. {
  48. for (size_t i=0 ; i < m_t1Tab.size() ; i++)
  49. {
  50. if (m_t1Tab[i] == t1)
  51. return (int)i ;
  52. }
  53. return -1 ;
  54. }
  55. /// Get 0-based index in array by t2, return -1 if not found.
  56. int GetIndexT2 (const T2& t2) const
  57. {
  58. for (size_t i=0 ; i < m_t2Tab.size() ; i++)
  59. {
  60. if (m_t2Tab[i] == t2)
  61. return (int)i ;
  62. }
  63. return -1 ;
  64. }
  65. /// Get first element.
  66. const T1& GetT1 (int n) const {assert(IsValidIndex(n)); return m_t1Tab[n];}
  67. T1& GetT1 (int n) {assert(IsValidIndex(n)); return m_t1Tab[n];}
  68. /// Get second element.
  69. const T2& GetT2 (int n) const {assert(IsValidIndex(n)); return m_t2Tab[n];}
  70. T2& GetT2 (int n) {assert(IsValidIndex(n)); return m_t2Tab[n];}
  71. /// Add a new element into table.
  72. void AddElement (const T1& t1, const T2& t2)
  73. {
  74. m_t1Tab.push_back (t1) ;
  75. m_t2Tab.push_back (t2) ;
  76. }
  77. /// Remove an element from table by 0-based index.
  78. void RemoveElement (int nIndex)
  79. {
  80. if (IsValidIndex(nIndex))
  81. {
  82. m_t1Tab.erase(m_t1Tab.begin() + nIndex) ;
  83. m_t2Tab.erase(m_t2Tab.begin() + nIndex) ;
  84. }
  85. else
  86. {assert(false);}
  87. }
  88. /// Remove an element from table.
  89. void RemoveElement (const T1& t1, const T2& t2)
  90. {
  91. for (int i=0 ; i < GetElementCount() ; i++)
  92. if ((m_t1Tab[i] == t1) && (m_t2Tab[i] == t2))
  93. {
  94. RemoveElement(i) ;
  95. return ; // must return immediately
  96. }
  97. }
  98. /// Clear all elements.
  99. void Clear()
  100. {
  101. m_t1Tab.clear() ;
  102. m_t2Tab.clear() ;
  103. }
  104. /// Pop a element from front of list.
  105. void PopFront()
  106. {
  107. if (GetElementCount())
  108. {
  109. m_t1Tab.pop_front() ;
  110. m_t2Tab.pop_front() ;
  111. }
  112. }
  113. private:
  114. bool IsValidIndex (int nIndex) const
  115. {
  116. return (nIndex >= 0) && (nIndex < GetElementCount()) ;
  117. }
  118. private:
  119. std::deque<T1> m_t1Tab ;
  120. std::deque<T2> m_t2Tab ;
  121. };
  122. #endif