vector.h

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 319 行 · 第 1/2 页

H
319
字号
  Boolean insert_before (const Type&);  // before curpos
  Boolean insert_after (const Type&);   // after curpos
  Boolean insert_before (const Type&, size_t index); // before index
  Boolean insert_after (const Type&, size_t index);  // after index

  // shuffle current and last elmt instead of shifting elmts to preserve order
  Type shuffle_remove ();                       // Remove and shuffle last elmt up
  Boolean shuffle_remove (const Type&, int dir = +1); // to fill hole.
  Boolean shuffle_insert_before (const Type&); // before curpos
  Boolean shuffle_insert_after (const Type&);  // after curpos
  Boolean shuffle_insert_before (const Type&, size_t index); // before index
  Boolean shuffle_insert_after (const Type&, size_t index);  // after index

protected:
  Type* data;                                   // Pointer to allocated storage
  static Boolean (*compare_s)(const Type&, const Type&);        // Pointer operator== function
  void grow (size_t min_size);                  // Make the CoolVector bigger

private:
  friend Boolean CoolVector_is_data_equal (const Type& t1, const Type& t2);
};

// Type& value () -- Return value at current position.
// Input:            None
// Output:           Type reference to value at current position

template<class Type> 
inline Type& CoolVector<Type>::value () {
#if ERROR_CHECKING
  if (this->curpos == INVALID_POS)                      // If INVALID current position
    this->value_error (#Type);                  // Raise exception
#endif
  return (this->data[this->curpos]);
}


// Type& operator[] () -- Access an individual element from a vector.
//                        Range checking is not performed.
// Input:                 Integer index of element to access
// Output:                Type reference of element

template<class Type> 
inline Type& CoolVector<Type>::operator[] (size_t n) const {
  return this->data[n];
}


// get() -- Returns the element of THIS.
// Input:   A positive integer index.
// Output:  A Type reference of data in the nth element of THIS.

template <class Type> 
inline Type& CoolVector<Type>::get(size_t n) {
#if ERROR_CHECKING  
  if (n >= this->number_elements) // If index out of range
    this->bracket_error (#Type, n);             // Raise exception
#endif
  this->curpos = n;                             // Update current position
  return this->data[n];
}

// put() -- Replaces the data slot of the CoolVector element and if
//          successful, returns the  new data item.
// Input:   A Type reference and a positive integer index.
// Output:  TRUE if nth element exists, FALSE otherwise.

template <class Type> 
inline Boolean CoolVector<Type>::put(const Type& x, size_t n) {
  if (n >= this->number_elements)               // False if index out of range
    return FALSE;
  else {
    this->curpos = n;                           // Update current position
    this->data[n] = x;                          // Store data
    return TRUE;
  }
}

  
// Boolean operator!= -- Test for inequality of the data of two vectors
// Input:                Reference to CoolVector object
// Output:               TRUE/FALSE

template<class Type> 
inline Boolean CoolVector<Type>::operator!= (const CoolVector<Type>& v) const {
  return (!operator== (v));
}


// size_t position () -- Return current position.
// Input:              None
// Output:             Integer representing current position

template<class Type> 
inline size_t CoolVector<Type>::position () const {
  return this->CoolBaseVector::position ();
}


// size_t position () -- Find first occurrence of element in a CoolVector,
//                     from start or end of vector respectively if dir = +1, -1.
// Input:              Element value searching for, and search direction
// Output:             Integer representing index or -1 if not found

template<class Type> 
inline size_t CoolVector<Type>::position (const Type& value, int dir) {
  size_t start = 0;                     // start from beginning
  if (dir == -1) start = this->number_elements - 1; // or end of array
  if (this->find(value, start, dir))            // search least or most 
    return this->curpos;                        // recent first
  else 
    return INVALID_POS();
}

// size_t set_length () -- Set the number of elements in a CoolVector object.
//                       If there is not enough storage allocated, set to
//                       maximum size for storage.
// Input:                Length value
// Output:               Integer representing number of elements

template<class Type> 
inline size_t CoolVector<Type>::set_length (size_t n) {
  this->CoolBaseVector::set_length (n, "Type"); // Call base class with type
  return this->length();                        // Return length
}


// void set_alloc_size () -- Set the default allocation size growth rate.
// Input:                    Growth size in number of elements
// Output:                   None

template<class Type> 
inline void CoolVector<Type>::set_alloc_size (size_t n) {
  this->CoolBaseVector::set_alloc_size (n, "Type");     // Call base class with size
}


// void set_growth_ratio () -- Set the growth percentage for the CoolVector object
// Input:                      Float ratio
// Output:                     None

template<class Type> 
inline void CoolVector<Type>::set_growth_ratio (float ratio) {
  this->CoolBaseVector::set_growth_ratio (ratio, "Type"); // Call base class with size
}

// ostream& operator<< () -- Overload the output operator for CoolVector pointer
// Input:                    Ostream reference and CoolVector pointer
// Output:                   Ostream reference

template<class Type>
inline ostream& operator<< (ostream& os, const CoolVector<Type>* v) {
  return operator<< (os, *v);
}

#include <cool/Vector.C>   // required for most template implementations

#endif                                          // End ifdef of VECTORH

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?