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

📄 cell.h

📁 A Library of Efficient Data Types and Algorithms,封装了常用的ADT及其相关算法的软件包
💻 H
字号:
/*******************************************************************************++  LEDA 4.5  +++  cell.h+++  Copyright (c) 1995-2004+  by Algorithmic Solutions Software GmbH+  All rights reserved.+ *******************************************************************************/// $Revision: 1.3 $  $Date: 2004/02/06 11:18:23 $#ifndef CELL_H
#define CELL_H

#include <LEDA/event.h>

LEDA_BEGIN_NAMESPACE

template <class T, class visitor> class cell; 

#define CELL_VISITOR_TYPE(TYPE) \
  struct TYPE \
  { void operator()(self& c, const_reference x) \
    { TYPE##_event.occur(param2<self&,const_reference>(c,x)); } \
    \
    void operator()(self& c0, const_self& c1) \
    { TYPE##_event.occur(param2<self&,const_self&>(c0,c1)); } \
    \
    void operator()(const_self& c0, const_self& c1) \
    { TYPE##_event.occur(param2<const_self&,const_self&>(c0,c1)); } \
    \
    void operator()(self& c) \
    { TYPE##_event.occur(param1<self&>(c)); } \
    \
    void operator()(const_self& c) \
    { TYPE##_event.occur(param1<const_self&>(c)); } \
    \
    void operator()(int i, const_self& c) \
    { TYPE##_event.occur(param2<const_self&,int>(c,i)); } \
    \
    void operator()(int i, self& c) \
    { TYPE##_event.occur(param2<self&,int>(c,i)); } \
  }

template <class T>
struct cell_visitor
{
  typedef cell<T, cell_visitor<T> >       self;
  typedef const cell<T, cell_visitor<T> > const_self;
  
  typedef T        value_type;
  typedef T&       reference;
  typedef const T& const_reference;
    
  static event construct_event;
  static event copy_event;
  static event destruct_event;
  static event assign_event;
  
  static event increment_event;
  static event decrement_event;

  static event cmp_le_event;
  static event cmp_leq_event;
  static event cmp_gr_event;
  static event cmp_geq_event;
  static event cmp_eq_event;
  static event cmp_neq_event;


  CELL_VISITOR_TYPE(construct);
  CELL_VISITOR_TYPE(copy);
  CELL_VISITOR_TYPE(destruct);
  CELL_VISITOR_TYPE(assign);
  
  CELL_VISITOR_TYPE(increment);
  CELL_VISITOR_TYPE(decrement);
  
  CELL_VISITOR_TYPE(cmp_le);
  CELL_VISITOR_TYPE(cmp_leq);
  CELL_VISITOR_TYPE(cmp_gr);
  CELL_VISITOR_TYPE(cmp_geq);
  CELL_VISITOR_TYPE(cmp_eq);
  CELL_VISITOR_TYPE(cmp_neq);

};

template <class T> event cell_visitor<T>::construct_event;
template <class T> event cell_visitor<T>::copy_event;
template <class T> event cell_visitor<T>::destruct_event;
template <class T> event cell_visitor<T>::assign_event;

template <class T> event cell_visitor<T>::increment_event;
template <class T> event cell_visitor<T>::decrement_event;

template <class T> event cell_visitor<T>::cmp_le_event;
template <class T> event cell_visitor<T>::cmp_leq_event;
template <class T> event cell_visitor<T>::cmp_gr_event;
template <class T> event cell_visitor<T>::cmp_geq_event;
template <class T> event cell_visitor<T>::cmp_eq_event;
template <class T> event cell_visitor<T>::cmp_neq_event;


template <class T, class visitor = cell_visitor<T> >
class cell : public visitor
{   
  public:
  
  typedef cell<T,visitor>       self;
  typedef const cell<T,visitor> const_self;

  typedef T        value_type;
  typedef T&       reference;
  typedef T*       pointer; 
  typedef const T& const_reference;

  protected:
  
  value_type val;
  
  public:
  
  const_reference data() const { return val; }

  cell(const_reference x = T()) : val(x) 
  { typename visitor::construct V; V(*this,x); }    
  
	cell(const_self& c) : val(c.val)
  { typename visitor::copy V; V(*this,c); }
   
  self& operator=(const_self& c)
  { typename visitor::assign V; V(*this,c); 
    val = c.val;
    return *this; 
  }  
  
  self& operator=(const_reference x) 
  { typename visitor::assign V; V(*this,x); 
    val = x; 
    return *this; 
  }    

 ~cell() { typename visitor::destruct V; V(*this); }
 
  // --------------------------------------
  // compare operations
  // --------------------------------------
  
  bool operator<(const_self& c) const
  { typename visitor::cmp_le V; V(*this,c);
    return val < c.val; 
  }
  
  bool operator>(const_self& c) const
  { typename visitor::cmp_gr V; V(*this,c);
    return val > c.val; 
  }
  
  bool operator<=(const_self& c) const
  { typename visitor::cmp_leq V; V(*this,c);
    return val <= c.val; 
  }
    
  bool operator>=(const_self& c) const
  { typename visitor::cmp_geq V; V(*this,c);
    return val >= c.val; 
  }
      
  bool operator==(const_self& c) const
  { typename visitor::cmp_eq V; V(*this,c);
    return val == c.val; 
  }  
  
  bool operator!=(const_self& c) const
  { typename visitor::cmp_neq V; V(*this,c);
    return val != c.val; 
  }  

  // --------------------------------------
  // arithmetic operations
  // --------------------------------------
   
  self& operator++() 
  { val++;
    typename visitor::increment V; V(*this);        
    return *this;
  }
  
  self& operator--() 
  { val--;
    typename visitor::decrement V; V(*this);
    return *this;
  }

  value_type operator++(int) 
  { value_type tmp = val++;    
    typename visitor::increment V; V(*this,tmp);        
    return tmp;
  }
        
  value_type operator--(int) 
  { value_type tmp = val--;    
    typename visitor::decrement V; V(*this,tmp);        
    return tmp;
  }
      
  // --------------------------------------
  // stream operations
  // --------------------------------------
  
  friend std::ostream& operator<<(std::ostream& os, const_self& c) 
  { os << c.val;
    return os;
  }
  
  operator reference() { return val;  }
};


template <class T, class V>
bool operator<(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_le Vis; Vis(c,val);
  return c.data() < val; 
}

template <class T, class V>
bool operator>(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_gr Vis; Vis(c,val);
  return c.data() > val; 
}

template <class T, class V>
bool operator<(T val, const cell<T,V>& c) 
{ typename cell<T,V>::cmp_gr Vis; Vis(c,val);
  return val < c.data(); 
}

template <class T, class V>
bool operator>(T val, const cell<T,V>& c) 
{ typename cell<T,V>::cmp_le Vis; Vis(c,val);  
  return val > c.data(); 
}

template <class T, class V>
bool operator<=(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_leq Vis; Vis(c,val);  
  return c.data() <= val; 
}

template <class T, class V>
bool operator>=(T val, const cell<T,V>& c) 
{ typename cell<T,V>::cmp_leq Vis; Vis(c,val);
  return val >= c.data(); 
}

template <class T, class V>
bool operator<=(T val, const cell<T,V>& c) 
{ typename cell<T,V>::cmp_geq Vis; Vis(c,val);
  return val <= c.data(); 
}

template <class T, class V>
bool operator>=(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_geq Vis; Vis(c,val);
  return c.data() >= val; 
}

template <class T, class V>
bool operator==(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_eq Vis; Vis(c,val);
  return c.data() == val; 
}

template <class T, class V>
bool operator==(T val, const cell<T,V>& c) 
{ return operator==(c,val); }
  
template <class T, class V>
bool operator!=(const cell<T,V>& c, T val) 
{ typename cell<T,V>::cmp_neq Vis; Vis(c,val);
  return c.data() != val; 
}

template <class T, class V>
bool operator!=(T val, const cell<T,V>& c) 
{ return operator!=(c,val); }


LEDA_END_NAMESPACE

#endif

⌨️ 快捷键说明

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