base_lis.h

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

H
382
字号
inline CoolBase_List_Node*& CoolBase_List_Node::next_node () {
  return this->next;
}


class CoolBase_List {                           // Define the CoolBase_List class
public:
  typedef CoolBase_List_Node* IterState;                // Curpos state for iterator

  CoolBase_List() {};                           // A Nil CoolBase_List
  virtual ~CoolBase_List();                             // Destructor is virtual
  
  inline void reset();                          // Reset current position
  inline Boolean next();                        // Increment current position
  Boolean prev();                               // Decrement current position
  
  Boolean operator==(const CoolBase_List& l) const;     // Equality test
  inline Boolean operator!=(const CoolBase_List& l) const; // Inequality test
  
  void tail(CoolBase_List& l, int n = 1);               // Sets l to the nth tail/cdr
  void last(CoolBase_List& l, int n = 1);               // Sets l to the n last nodes
  void but_last(CoolBase_List& l, int n = 1);   // Sets l to all but n last
  
  void clear();                                 // Removes all nodes 
  inline Boolean is_empty();                    // Any nodes in List?
  int length();                                 // Returns node count in List
  int position();                               // Returns current position
  
  Boolean search(const CoolBase_List& l);               // SubList search
  
  // returns true if THIS CoolBase_List contains the specified subList
  // and sets CoolBase_List l to a subList in THIS List starting at the 1st occurence
  // of CoolBase_List s
  Boolean sublist(CoolBase_List& l, const CoolBase_List& s);
  
  void copy(const CoolBase_List& l);                    // Copy l into *this
  void reverse();                               // Reverses order of elements
  Boolean prepend(const CoolBase_List& l);              // Prepends l to start of List
  Boolean append(const CoolBase_List& l);               // Appends  l to end of List
  
  Boolean set_tail(const CoolBase_List& l, int n = 1); // rplacd a l
  Boolean remove_duplicates();                    // Removes duplicate elements
  
  void set_intersection(const CoolBase_List& l);        // Intersection of two Lists
  void set_union(const CoolBase_List& l);               // Union of two Lists
  void set_difference(const CoolBase_List& l);  // Difference of two Lists
  void set_xor(const CoolBase_List& l);         // XOR of two Lists
  
  Boolean next_intersection(const CoolBase_List& l); // Current position intersect
  Boolean next_union(const CoolBase_List& l);   // Current position union
  Boolean next_difference(const CoolBase_List& l);      // Current position difference
  Boolean next_xor(const CoolBase_List& l);             // Current position XOR
  
  void describe(ostream& os);                   // Describes structure of List
  
  friend ostream& operator<<(ostream& os, const CoolBase_List& l); // Output 
  /*##inline*/ friend ostream& operator<<(ostream& os, const CoolBase_List* l); 

protected:
  CoolBase_List_Node* node_ptr;                 // Pointer to first node
  CoolBase_List_Node* curpos;                   // Maintains current position
  CoolBase_List_Node* prevpos;                  // Performance hack to previous
  Boolean traversal;                            // Traversal flag for curpos
  
  virtual CoolBase_List* new_list(CoolBase_List_Node*); // Creates a new CoolBase_List from node
  virtual CoolBase_List_Node* insert_before_node(const void* v, CoolBase_List_Node* next_np);
  virtual CoolBase_List_Node* insert_after_node(const void* v, CoolBase_List_Node* prev_np) const;
  
  virtual Boolean compare_data(const void*, const void*) const;
  virtual void output_data(ostream&, const CoolBase_List_Node*) const;
  
  CoolBase_List_Node* copy_nodes() const;               // Returns copy of nodes
  inline void reference(CoolBase_List_Node*);   // Increments reference count
  inline void dereference(CoolBase_List_Node*); // Decrements reference count
  // And if zero, deletes node
  void CoolBase_List::free_nodes(CoolBase_List_Node* np); // Deletes nodes
  
  CoolBase_List* operator=(const CoolBase_List& l);     // Assignment List1 = List2;
  CoolBase_List_Node* operator[](int n);                // X = l[n];
  
  int position(const void* x);                  // Returns 0-relative index 
  virtual Boolean do_find(CoolBase_List_Node* np, const void* x, // Used by find
                          CoolBase_List_Node*& cp, CoolBase_List_Node*& pp) const;
  // returns true if THIS CoolBase_List contains element x and 
  // sets CoolBase_List l to a subList in THIS List starting at the first occurence of x
  Boolean member(CoolBase_List& l, const void* x);
  
  Boolean push(const void* x);                  // Adds X to head of THIS List
  Boolean push_new(const void* x);              // Push(X) if not in THIS List
  Boolean push_end(const void* x);              // Adds X at end of THIS List
  Boolean push_end_new(const void* x);          // push_end(x) if not in List
  
  CoolBase_List_Node* pop();                            // Removes/returns head node
  CoolBase_List_Node* remove();                 // Removes item at curpos
  Boolean remove(const void* x);                // Removes first occurence
  
  Boolean replace(const void*, const void*);    // Replace first
  Boolean replace_all(const void*,const void*); // Replace all
  
  void sort(Predicate f);                       // Sort List using predicate
  void merge(const CoolBase_List& l, Predicate f);      // Merge List w/ sort predicate
  
  Boolean insert_before(const void* new_item);  // Insert item before curpos
  Boolean insert_after(const void* new_item);   // Insert item after curpos
  
  Boolean insert_before(const void*,const void*); // Insert item before
  Boolean insert_after(const void*, const void*); // Insert item after
  
  void value_error (const char*);               // Raise exception
  void get_error (const char*, int);            // Raise exception
  void before_error (const char*);              // Raise exception
  void after_error (const char*);               // Raise exception
  void bracket_error (const char*, int);        // Raise exception
  void pop_error (const char*);                 // Raise exception
  void remove_error (const char*);              // Raise exception
  void va_arg_error (const char*, int);         // Raise exception
};


// reference () -- Increments the reference count of a node pointer
// Input:          The node pointer to be referenced.
// Output:         None.

inline void CoolBase_List::reference(CoolBase_List_Node* np) {
  if (np != NULL) np->ref_count++;
}


// dereference() -- Decrements the reference count of the node pointer and
//                  deallocates storage if no longer referenced.
// Input:           The node pointer to be dereferenced.
// Output:          None.

inline void CoolBase_List::dereference(CoolBase_List_Node* np) {
  if (np != NULL && --(np->ref_count) <= 0)
    this->free_nodes(np);
}


// reset() -- sets current position to NULL
// Input:     None.
// Output:    None.

inline void CoolBase_List::reset() {
  this->curpos = NULL;
  this->prevpos = NULL;
  this->traversal = TRUE;
}


// next() -- Increment current position. If NULL, set it to first.
// Input:    None.
// Output:   TRUE or FALSE.

inline Boolean CoolBase_List::next() {
  register CoolBase_List_Node* cp = this->curpos;
  if (cp == NULL)                               // Current position valid?
    cp = this->node_ptr;                        // Set curpos to head node
  else
    (cp = cp->next);                            // Advance to next position
  return ((this->curpos = cp) != NULL);         // Return status
}


// operator!=() -- Returns TRUE if data in THIS CoolBase_List and the not the same
// Input:          A CoolBase_List reference.
// Output:         TRUE or FALSE.

inline Boolean CoolBase_List::operator!=(const CoolBase_List& l) const {
  return !this->operator==(l);
}


// is_empty() -- Indicates node presence in CoolBase_List
// Input:        None.
// Output:       TRUE or FALSE.

inline Boolean CoolBase_List::is_empty() {
  return this->node_ptr == NULL;
}

// operator<<() -- Overload output operator for CoolBase_List objects
// Input:          An output stream reference and CoolBase_List pointer.
// Output:         An output stream reference.

inline ostream& operator<<(ostream& os, const CoolBase_List* l) {
  return operator<<(os, *l);
}

#endif                                          // End of BASE_LISTH

⌨️ 快捷键说明

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