⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stl_tree.h

📁 STL完整源码,实现STL文件的读写和三维体的重建及其分析
💻 H
📖 第 1 页 / 共 3 页
字号:
/* * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Silicon Graphics makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. * * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation.  Hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose.  It is provided "as is" without express or implied warranty. * * *//* NOTE: This is an internal header file, included by other STL headers. *   You should not attempt to use it directly. */#ifndef __SGI_STL_INTERNAL_TREE_H#define __SGI_STL_INTERNAL_TREE_H/*Red-black tree class, designed for use in implementing STLassociative containers (set, multiset, map, and multimap). Theinsertion and deletion algorithms are based on those in Cormen,Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),except that(1) the header cell is maintained with links not only to the rootbut also to the leftmost node of the tree, to enable constant timebegin(), and to the rightmost node of the tree, to enable linear timeperformance when used with the generic set algorithms (set_union,etc.);(2) when a node being deleted has two children its successor node isrelinked into its place, rather than copied, so that the onlyiterators invalidated are those referring to the deleted node.*/#include <stl_algobase.h>#include <stl_alloc.h>#include <stl_construct.h>#include <stl_function.h>__STL_BEGIN_NAMESPACE typedef bool __rb_tree_color_type;const __rb_tree_color_type __rb_tree_red = false;const __rb_tree_color_type __rb_tree_black = true;struct __rb_tree_node_base{  typedef __rb_tree_color_type color_type;  typedef __rb_tree_node_base* base_ptr;  color_type color;   base_ptr parent;  base_ptr left;  base_ptr right;  static base_ptr minimum(base_ptr x)  {    while (x->left != 0) x = x->left;    return x;  }  static base_ptr maximum(base_ptr x)  {    while (x->right != 0) x = x->right;    return x;  }};template <class Value>struct __rb_tree_node : public __rb_tree_node_base{  typedef __rb_tree_node<Value>* link_type;  Value value_field;};struct __rb_tree_base_iterator{  typedef __rb_tree_node_base::base_ptr base_ptr;  typedef bidirectional_iterator_tag iterator_category;  typedef ptrdiff_t difference_type;  base_ptr node;  void increment()  {    if (node->right != 0) {      node = node->right;      while (node->left != 0)        node = node->left;    }    else {      base_ptr y = node->parent;      while (node == y->right) {        node = y;        y = y->parent;      }      if (node->right != y)        node = y;    }  }  void decrement()  {    if (node->color == __rb_tree_red &&        node->parent->parent == node)      node = node->right;    else if (node->left != 0) {      base_ptr y = node->left;      while (y->right != 0)        y = y->right;      node = y;    }    else {      base_ptr y = node->parent;      while (node == y->left) {        node = y;        y = y->parent;      }      node = y;    }  }};template <class Value, class Ref, class Ptr>struct __rb_tree_iterator : public __rb_tree_base_iterator{  typedef Value value_type;  typedef Ref reference;  typedef Ptr pointer;  typedef __rb_tree_iterator<Value, Value&, Value*>             iterator;  typedef __rb_tree_iterator<Value, const Value&, const Value*> const_iterator;  typedef __rb_tree_iterator<Value, Ref, Ptr>                   self;  typedef __rb_tree_node<Value>* link_type;  __rb_tree_iterator() {}  __rb_tree_iterator(link_type x) { node = x; }  __rb_tree_iterator(const iterator& it) { node = it.node; }  reference operator*() const { return link_type(node)->value_field; }#ifndef __SGI_STL_NO_ARROW_OPERATOR  pointer operator->() const { return &(operator*()); }#endif /* __SGI_STL_NO_ARROW_OPERATOR */  self& operator++() { increment(); return *this; }  self operator++(int) {    self tmp = *this;    increment();    return tmp;  }      self& operator--() { decrement(); return *this; }  self operator--(int) {    self tmp = *this;    decrement();    return tmp;  }};inline bool operator==(const __rb_tree_base_iterator& x,                       const __rb_tree_base_iterator& y) {  return x.node == y.node;}inline bool operator!=(const __rb_tree_base_iterator& x,                       const __rb_tree_base_iterator& y) {  return x.node != y.node;}#ifndef __STL_CLASS_PARTIAL_SPECIALIZATIONinline bidirectional_iterator_tagiterator_category(const __rb_tree_base_iterator&) {  return bidirectional_iterator_tag();}inline __rb_tree_base_iterator::difference_type*distance_type(const __rb_tree_base_iterator&) {  return (__rb_tree_base_iterator::difference_type*) 0;}template <class Value, class Ref, class Ptr>inline Value* value_type(const __rb_tree_iterator<Value, Ref, Ptr>&) {  return (Value*) 0;}#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */inline void __rb_tree_rotate_left(__rb_tree_node_base* x, __rb_tree_node_base*& root){  __rb_tree_node_base* y = x->right;  x->right = y->left;  if (y->left !=0)    y->left->parent = x;  y->parent = x->parent;  if (x == root)    root = y;  else if (x == x->parent->left)    x->parent->left = y;  else    x->parent->right = y;  y->left = x;  x->parent = y;}inline void __rb_tree_rotate_right(__rb_tree_node_base* x, __rb_tree_node_base*& root){  __rb_tree_node_base* y = x->left;  x->left = y->right;  if (y->right != 0)    y->right->parent = x;  y->parent = x->parent;  if (x == root)    root = y;  else if (x == x->parent->right)    x->parent->right = y;  else    x->parent->left = y;  y->right = x;  x->parent = y;}inline void __rb_tree_rebalance(__rb_tree_node_base* x, __rb_tree_node_base*& root){  x->color = __rb_tree_red;  while (x != root && x->parent->color == __rb_tree_red) {    if (x->parent == x->parent->parent->left) {      __rb_tree_node_base* y = x->parent->parent->right;      if (y && y->color == __rb_tree_red) {        x->parent->color = __rb_tree_black;        y->color = __rb_tree_black;        x->parent->parent->color = __rb_tree_red;        x = x->parent->parent;      }      else {        if (x == x->parent->right) {          x = x->parent;          __rb_tree_rotate_left(x, root);        }        x->parent->color = __rb_tree_black;        x->parent->parent->color = __rb_tree_red;        __rb_tree_rotate_right(x->parent->parent, root);      }    }    else {      __rb_tree_node_base* y = x->parent->parent->left;      if (y && y->color == __rb_tree_red) {        x->parent->color = __rb_tree_black;        y->color = __rb_tree_black;        x->parent->parent->color = __rb_tree_red;        x = x->parent->parent;      }      else {        if (x == x->parent->left) {          x = x->parent;          __rb_tree_rotate_right(x, root);        }        x->parent->color = __rb_tree_black;        x->parent->parent->color = __rb_tree_red;        __rb_tree_rotate_left(x->parent->parent, root);      }    }  }  root->color = __rb_tree_black;}inline __rb_tree_node_base*__rb_tree_rebalance_for_erase(__rb_tree_node_base* z,                              __rb_tree_node_base*& root,                              __rb_tree_node_base*& leftmost,                              __rb_tree_node_base*& rightmost){  __rb_tree_node_base* y = z;  __rb_tree_node_base* x = 0;  __rb_tree_node_base* x_parent = 0;  if (y->left == 0)             // z has at most one non-null child. y == z.    x = y->right;               // x might be null.  else    if (y->right == 0)          // z has exactly one non-null child.  y == z.      x = y->left;              // x is not null.    else {                      // z has two non-null children.  Set y to      y = y->right;             //   z's successor.  x might be null.      while (y->left != 0)        y = y->left;      x = y->right;    }  if (y != z) {                 // relink y in place of z.  y is z's successor    z->left->parent = y;     y->left = z->left;    if (y != z->right) {      x_parent = y->parent;      if (x) x->parent = y->parent;      y->parent->left = x;      // y must be a left child      y->right = z->right;      z->right->parent = y;    }    else      x_parent = y;      if (root == z)      root = y;    else if (z->parent->left == z)      z->parent->left = y;    else       z->parent->right = y;    y->parent = z->parent;    __STD::swap(y->color, z->color);    y = z;    // y now points to node to be actually deleted  }  else {                        // y == z    x_parent = y->parent;    if (x) x->parent = y->parent;       if (root == z)      root = x;    else       if (z->parent->left == z)        z->parent->left = x;      else        z->parent->right = x;    if (leftmost == z)       if (z->right == 0)        // z->left must be null also        leftmost = z->parent;    // makes leftmost == header if z == root      else        leftmost = __rb_tree_node_base::minimum(x);    if (rightmost == z)        if (z->left == 0)         // z->right must be null also        rightmost = z->parent;      // makes rightmost == header if z == root      else                      // x == z->left        rightmost = __rb_tree_node_base::maximum(x);  }  if (y->color != __rb_tree_red) {     while (x != root && (x == 0 || x->color == __rb_tree_black))      if (x == x_parent->left) {        __rb_tree_node_base* w = x_parent->right;        if (w->color == __rb_tree_red) {          w->color = __rb_tree_black;          x_parent->color = __rb_tree_red;          __rb_tree_rotate_left(x_parent, root);          w = x_parent->right;        }

⌨️ 快捷键说明

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