Changeset 860


Ignore:
Timestamp:
Jun 22, 2003, 2:50:35 AM (18 years ago)
Author:
orlov
Message:
  • Improved eq functions.
Location:
to-imperative/trunk/runtime
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/runtime/rf_expr.hh

    r840 r860  
    139139  /// Get expression length (in terms)
    140140  inline uintptr_t get_len () const ;
     141private:
    141142  ///
    142143  /// Get expression flags
    143144  inline uintptr_t get_flags () const ;
     145public:
    144146  ///
    145147  /// Dump expression contents (not recursive)
     
    159161  inline uint32_t hash () const ;
    160162  ///
    161   /// Compare with subexpression of other expression
     163  /// Compare with subexpression of other expression.  Attention: doesn't check
     164  /// that our expression is not longer then the rest of the other.
    162165  inline bool eq (Expr const& _expr, uintptr_t _index) ;
    163166  ///
     
    171174  ///
    172175  /// Compare our first term with some one of another expression as flat
    173   /// symbols.
    174   inline bool Expr::sym_eq (Expr const& _expr, uintptr_t _index) const ;
     176  /// symbols. Should be called only with valid index argument.
     177  inline bool sym_eq (Expr const& _expr, uintptr_t _index) const ;
    175178  ///
    176179  /// Equality check operator
     180  inline bool operator == (Expr& _expr) ;
     181  ///
     182  /// Inequality check operator
     183  inline bool operator != (Expr& _expr) ;
     184  ///
     185  /// Same methods for const expressions.
    177186  inline bool operator == (Expr const& _expr) const ;
    178   ///
    179   /// Inequality check operator
    180187  inline bool operator != (Expr const& _expr) const ;
    181188  ///
     
    252259  /// beside expression margins.
    253260  INLINE void deref_childs () const ;
    254 public:
    255261  static INLINE void deref_childs (
    256262    Term* _first, Term* _last, MemoryChunk* _mem
  • to-imperative/trunk/runtime/rf_expr.ih

    r855 r860  
    203203inline bool Expr::symbol_at (uintptr_t _index) const
    204204{
    205   return (first + _index)->get_type() > type_ref;
     205  return (first + _index)->is_sym();
    206206}
    207207
     
    263263}
    264264
     265#if 0
    265266inline void Expr::ref_childs () const
    266267{
     
    269270  }
    270271}
     272#endif
    271273
    272274inline bool Expr::writeln (FILE* _fp) const
     
    359361inline bool Expr::sym_eq (Expr const& _expr, uintptr_t _index) const
    360362{
    361   //
    362   // FIXME: Do not forget to do object comparison for compound symbols
    363   return
    364     first->get_type() == (_expr.first + _index)->get_type() &&
    365     first->uint_data2 == (_expr.first + _index)->uint_data2;
    366 }
    367 
    368 inline bool Expr::flat_eq (Expr const& _expr, uintptr_t _index) const
     363  return *first == *(_expr.first + _index);
     364}
     365
     366inline bool Expr::eq (Expr const& _expr, uintptr_t _index) const
    369367{
    370368  Term *f = _expr.first + _index;
    371   if (first == f && last == f + (last - first)) {
     369  if (first == f) {
    372370    #if DEBUG
    373371    identical++;
     
    375373    return true;
    376374  }
    377   return Term::flat_eq(first, f, last - first);
    378 }
    379 
    380 inline bool Expr::flat_eq (Expr const& _expr, uintptr_t _index)
     375  return Term::eq(first, f, get_len());
     376}
     377
     378inline bool Expr::eq (Expr const& _expr, uintptr_t _index)
    381379{
    382380  Term *f = _expr.first + _index;
    383   Term *l = f + (last - first);
    384   if (first == f && last == l) {
     381  if (first == f) {
    385382    #if DEBUG
    386383    identical++;
     
    388385    return true;
    389386  }
    390   bool res = Term::flat_eq(first, f, last - first);
    391   //
    392   // I suppouse that in typical situation unification of flat expressions will
    393   // slow down the computation.  But more statistics are needed.
    394 #if 0
     387  bool res = Term::eq(first, f, get_len());
    395388  if (res) {
    396389    #if DEBUG
    397390    unifications++;
    398391    #endif
    399     //
    400     // Destroy old expression
     392    uintptr_t new_flags = flags | _expr.flags;
    401393    drop();
    402     //
    403     // Build a copy in place
    404     new(this) Expr(f, l, _expr.mem_chunk, flags & _expr.flags);
    405   }
    406 #endif
     394    new(this) Expr(f, f + get_len(), _expr.mem_chunk, new_flags);
     395  }
    407396  return res;
    408397}
    409398
    410 inline bool Expr::eq (Expr const& _expr, uintptr_t _index) const
    411 {
    412   Term *f = _expr.first + _index;
    413   Term *l = f + (last - first);
    414   if (first == f && last == l) {
    415     #if DEBUG
    416     identical++;
    417     #endif
    418     return true;
    419   }
    420   if (is_flat() || _expr.is_flat())
    421     return Term::flat_eq(first, f, last - first);
    422   return Term::eq(first, last, f, l);
    423 }
    424 
    425 inline bool Expr::eq (Expr const& _expr, uintptr_t _index)
    426 {
    427   Term *f = _expr.first + _index;
    428   Term *l = f + (last - first);
    429   if (first == f && last == l) {
    430     #if DEBUG
    431     identical++;
    432     #endif
    433     return true;
    434   }
    435   bool res;
    436   if (is_flat() || _expr.is_flat())
    437     res = Term::flat_eq(first, f, last - first);
    438   else
    439     res = Term::eq(first, last, f, l);
    440 #if 0
    441   if (res) {
    442     #if DEBUG
    443     unifications++;
    444     #endif
    445     //
    446     // Destroy old expression
    447     drop();
    448     //
    449     // Build a copy in place
    450     new(this) Expr(f, l, _expr.mem_chunk, flags & _expr.flags);
    451   }
    452 #endif
    453   return res;
    454 }
    455 
    456399inline bool Expr::operator == (Expr const& _expr) const
     400{
     401  if (get_len() != _expr.get_len()) return false;
     402  return eq(_expr, 0);
     403}
     404
     405inline bool Expr::operator != (Expr const& _expr) const
     406{
     407  if (get_len() != _expr.get_len()) return true;
     408  return !eq(_expr, 0);
     409}
     410
     411inline bool Expr::operator == (Expr& _expr)
    457412{
    458413  if (first == _expr.first && last == _expr.last) {
     
    463418  } else {
    464419    bool res = Term::eq(first, last, _expr.first, _expr.last);
    465 #if 0
    466420    if (res) {
    467421      #if DEBUG
     
    469423      #endif
    470424      if (mem_chunk->get_ref_count() >= _expr.mem_chunk->get_ref_count()) {
    471         (Expr&)_expr = self;
     425        flags |= _expr.flags;
     426        _expr = self;
    472427      } else {
    473         (Expr&)self = _expr;
     428        _expr.flags |= flags;
     429        self = _expr;
    474430      }
    475431    }
    476 #endif
    477432    return res;
    478433  }
    479 //  return Term::eq(first, last, _expr.first, _expr.last);
    480 }
    481 
    482 inline bool Expr::operator != (Expr const& _expr) const
    483 {
    484   if (first == _expr.first && last == _expr.last) {
    485     #if DEBUG
    486     identical++;
    487     #endif
    488     return false;
    489   } else {
    490     bool res = Term::eq(first, last, _expr.first, _expr.last);
    491     if (res) {
    492       #if DEBUG
    493       unifications++;
    494       #endif
    495       if (mem_chunk->get_ref_count() >= _expr.mem_chunk->get_ref_count()) {
    496         (Expr&)_expr = self;
    497       } else {
    498         (Expr&)self = _expr;
    499       }
    500     }
    501     return res;
    502   }
    503 //  return !Term::eq(first, last, _expr.first, _expr.last);
     434}
     435
     436inline bool Expr::operator != (Expr& _expr)
     437{
     438  return !(self == _expr);
    504439}
    505440
  • to-imperative/trunk/runtime/rf_term.cc

    r835 r860  
    1111
    1212#include "rf_term.ih"
    13 #include "rf_char.ih"
    1413
    1514namespace rfrt {
  • to-imperative/trunk/runtime/rf_term.hh

    r836 r860  
    142142  inline Term& operator = (Term const& _t) ;
    143143
    144   typedef void (Term::*ctor_func_t)(Term const& _t) ;
    145   typedef void (Term::*dtor_func_t)() ;
    146   typedef bool (Term::*eq_func_t)(Term const& _t) const ;
    147   typedef pxx::WString (Term::*to_string_func_t)() const ;
    148   typedef size_t (Term::*get_char_len_func_t)() const ;
    149   typedef Term* (Term::*to_chars_func_t)(Term* _p) const ;
    150 
    151144  static void ref_ctor (Term* _to, Term const* _from) ;
    152145  static void ref_dtor (Term* _term) ;
     
    171164    Term const* _f1, Term const* _l1, Term const* _f2, Term const* _l2
    172165  ) ;
     166  static inline bool eq (
     167    Term const* _p1, Term const* _p2, uintptr_t _len
     168  ) ;
    173169  ///
    174170  /// Check whether two flat term sequences are equal
    175171  static inline bool flat_eq (
    176     Term const* _f1, Term const* _f2, uintptr_t _len
     172    Term const* _p1, Term const* _p2, uintptr_t _len
    177173  ) ;
    178174  ///
  • to-imperative/trunk/runtime/rf_term.ih

    r839 r860  
    4444inline Term::~Term ()
    4545{
    46 #if 0
    47   if (is_ref()) {
    48 //    Expr e(this);
    49 //    e.get_mem_chunk()->dec_ref_count();
    50     MemoryChunk* m = get_mem_chunk();
    51     //
    52     // Decrement reference counter and destroy an object if it is zero
    53     if (m->dec_ref_count() == 0) {
    54       //
    55       // Walk through and decrement reference counters on childs
    56 //      bool f = (data1 & REF_BIT) == 0 ? true : false;
    57       Expr::deref_childs(
    58         get_first(), (data1 & FLAT_BIT) != 0 ? get_last() : get_first(), m
    59       );
    60       //
    61       // Deallocate expression holder in memory
    62       MemoryChunk::destroy_instance(m);
    63     }
    64   }
    65 #else
    66 //  (this->*dtor_funcs[get_class()])();
    6746  void (*pf)(Term*) = short_type_funcs[get_type()]->dtor;
    6847  if (pf != null) (*pf)(this);
    69 #endif
    7048}
    7149
    7250inline Term::Term (Term const& _t)
    7351{
    74 //  (this->*ctor_funcs[_t.get_class()])(_t);
    7552  void (*pf)(Term*, Term const*) = short_type_funcs[_t.get_type()]->ctor;
    7653  if (pf != null) (*pf)(this, &_t);
     
    154131inline bool Term::operator == (Term const& _t) const
    155132{
    156 #if 0
    157   term_class_t cl = get_class();
    158   //
    159   // If both terms have the same type...
    160   if (cl == _t.get_class()){
    161     //
    162     // ...compare them
    163     return (this->*eq_funcs[cl])(_t);
    164   } else {
    165     return false;
    166   }
    167 #endif
    168   //
    169   // If symbol types are equal...
    170   if (get_type() == _t.get_type()) {
    171     if (uint_data2 == _t.uint_data2) return true;
    172     else {
    173       bool (*pf)(Term const*, Term const*) = short_type_funcs[get_type()]->eq;
    174       if (pf != null) return (*pf)(this, &_t);
    175     }
    176   }
     133  if (data1 != _t.data1) return false;
     134  if (uint_data2 == _t.uint_data2) return true;
     135  bool (*pf)(Term const*, Term const*) = short_type_funcs[get_type()]->eq;
     136  if (pf && (*pf)(this, &_t)) return true;
    177137  return false;
    178138}
     
    200160}
    201161
     162inline bool Term::eq (
     163  Term const* _p1, Term const* _p2, uintptr_t _len
     164)
     165{
     166  for (; _len--; _p1++, _p2++)
     167  {
     168    if (*_p1 != *_p2) return false;
     169  }
     170  return true;
     171}
     172
    202173inline bool Term::flat_eq (
    203   Term const* _f1, Term const* _f2, uintptr_t _len
     174  Term const* _p1, Term const* _p2, uintptr_t _len
    204175)
    205176{
    206   for (; _len--; _f1++, _f2++) {
    207     //
    208     // FIXME: Do not forget to do object comparison for compound symbols
    209     if (_f1->get_type() != _f2->get_type() || _f1->uint_data2 != _f2->uint_data2)
     177  for (; _len--; _p1++, _p2++)
     178  {
     179    if (_p1->data1 != _p2->data1 || _p1->uint_data2 != _p2->uint_data2)
    210180      return false;
    211181  }
     
    234204}
    235205
    236 
    237206inline Term::operator pxx::WString () const
    238207{
Note: See TracChangeset for help on using the changeset viewer.