stl_bids.h
上传用户:tigerk9
上传日期:2020-03-10
资源大小:237k
文件大小:4k
源码类别:

Telnet客户端

开发平台:

Visual C++

  1. // This is the STL wrapper for classlib/arrays.h from Borland's web site
  2. // It has been modified to be compatible with vc++ (Paul Branann 5/7/98)
  3. #ifndef STL_ARRAY_AS_VECTOR
  4. #define STL_ARRAY_AS_VECTOR
  5. #ifdef _MSC_VER
  6. #pragma warning(disable: 4786)
  7. #endif
  8. // #include <vector.h>
  9. // #include <algo.h>
  10. #include <vector>
  11. #include <algorithm>
  12. using namespace std;
  13. template <class T>
  14. class TArrayAsVector : public vector<T> {
  15. private:
  16. const unsigned int growable;
  17. const size_type lowerbound;
  18. public:
  19. TArrayAsVector(size_type upper,
  20. size_type lower = 0,
  21. int delta = 0) :
  22. vector<T>( ),
  23. growable(delta),
  24. lowerbound(lower)
  25. { reserve(upper-lower + 1);}
  26. ~TArrayAsVector( )
  27. { // This call is unnecessary?  (Paul Brannan 5/7/98)
  28. // vector<T>::~vector( );
  29. }
  30. int Add(const T& item)
  31. { if(!growable && size( ) == capacity( ))
  32. return 0;
  33. else
  34. insert(end( ), item);
  35. return 1; }
  36. int AddAt(const T& item, size_type index)
  37. { if(!growable &&
  38. ((size( ) == capacity( )) ||
  39. (ZeroBase(index > capacity( )) )))
  40. return 0;
  41. if(ZeroBase(index) > capacity( )) // out of bounds
  42. { insert(end( ), 
  43. ZeroBase(index) - size( ), T( ));
  44. insert(end( ), item); }
  45. else
  46. { insert(begin( ) + ZeroBase(index), item); }
  47. return 1;
  48. }
  49. size_type ArraySize( )
  50. { return capacity( ); }
  51. size_type BoundBase(size_type location) const
  52. { if(location == UINT_MAX)
  53. return INT_MAX;
  54. else
  55. return location + lowerbound; }
  56. void Detach(size_type index)
  57. { erase(begin( ) + ZeroBase(index)); }
  58. void Detach(const T& item)
  59. { Destroy(Find(item)); }
  60. void Destroy(size_type index)
  61. { erase(begin( ) + ZeroBase(index)); }
  62. void Destroy(const T& item)
  63. { Destroy(Find(item)); }
  64. size_type Find(const T& item) const
  65. { const_iterator location = find(begin( ), 
  66. end( ), item);
  67. if(location != end( ))
  68. return BoundBase(size_type(location - 
  69. begin( )));
  70. else
  71. return INT_MAX; }
  72. size_type GetItemsInContainer( )
  73. { return size( ); }
  74. void Grow(size_type index)
  75. { if( index < lowerbound )
  76. Reallocate(ArraySize( ) + (index - 
  77. lowerbound));
  78. else if( index >= BoundBase(size( )))
  79. Reallocate(ZeroBase(index) ); }
  80. int HasMember(const T& item)
  81. { if(Find(item) != INT_MAX)
  82. return 1;
  83. else
  84. return 0; }
  85. int IsEmpty( )
  86. { return empty( ); }
  87. int IsFull( )
  88. { if(growable)
  89. return 0;
  90. if(size( ) == capacity( ))
  91. return 1;
  92. else
  93. return 0; }
  94. size_type LowerBound( )
  95. { return lowerbound; }
  96. T& operator[] (size_type index)
  97. { return vector<T>::
  98. operator[](ZeroBase(index)); }
  99. const T& operator[] (size_type index) const
  100. { return vector<T>::
  101. operator[](ZeroBase(index)); }
  102. void Flush( )
  103. {
  104. // changed this to get it to work with VC++
  105. // (Paul Brannan 5/7/98)
  106. #ifdef _MSC_VER
  107. _Destroy(begin(), end());
  108. _Last = _First;
  109. #else
  110. destroy(begin( ), end( ));
  111. #ifdef __CYGWIN__
  112. _M_finish = _M_start;
  113. #else
  114. finish = start;
  115. #endif
  116. #endif
  117. }
  118. void Reallocate(size_type sz, 
  119. size_type offset = 0)
  120. { if(offset)
  121. insert(begin( ), offset, T( ));
  122. reserve(sz);
  123. erase(end( ) - offset, end( )); }
  124. void RemoveEntry(size_type index)
  125. { Detach(index); }
  126. void SetData(size_type index, const T& item)
  127. { (*this)[index] = item; }
  128. size_type UpperBound( )
  129. { return BoundBase(capacity( )) - 1; }
  130. size_type ZeroBase(size_type index) const
  131. { return index - lowerbound; }
  132. // The assignment operator is not inherited (Paul Brannan 5/25/98)
  133. TArrayAsVector& operator=(const TArrayAsVector& v) {
  134. vector<T>::operator=(v);
  135. // should growable and lowerbound be copied as well?
  136. return *this;
  137. }
  138. };
  139. #endif