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

📄 vertleyb.txt

📁 用C++编的小程序。
💻 TXT
字号:
RTL: The Relational Template Library
by Arkadiy Vertleyb and Dmitriy Arapov

Listing 1:

template <class T, class U>
struct derive_from : public T, public U
{
  ...
};
struct empty_type
{};
template <class List> struct derive_from_all
{
  typedef derive_from<
    typename List::head,
    typename derive_from_all<typename List::tail>::result> result;
};
template <> struct derive_from_all<null_type>
{
  typedef empty_type result;
};
template<class FieldList>
  struct tuple : public derive_from_all<FieldList>::result
{
  typedef FieldList field_list;
  template<class Col>
  typename Col::type& operator[](Col&){
    return static_cast<Col&>(*this).value;
  }
  template<class Col>
  const typename Col::type& operator[](Col&) const {
    return static_cast<const Col&>(*this).value;
  }
  ...
};

Listing 2

template <class List, class Func> class for_each_type;
template <class List_, class Func_> class for_each_type_
{
  static void run_helper_(
    Func_ f, void(Func_::*)(typename List_::head*))
  {
    f((typename List_::head*)0);
    for_each_type<typename List_::tail, Func_>::run(f);
  }
  static void run_helper_(
    Func_ f, bool(Func_::*)(typename List_::head*))
  {
    if (f((typename List_::head*)0))
      for_each_type<typename List_::tail, Func_>::run(f);
  }
  static void run(Func_ f)
  {
    run_helper_(f, &Func_::operator());
  }
  friend class for_each_type<List_, Func_>;
};
template <class List, class Func> class for_each_type
{
  struct stop
  {
    static void run(Func) {}
  };
  typedef select<
    boost::is_same<List, null_type>::value, stop,
    for_each_type_<List, Func> >::result result_;
public:
  static void run(Func f)
  {
    result_::run(f);
  }
};
template <class List, class Func>
void run_for_each(Func f, List* = 0)
{
  for_each_type<List, Func>::run(f);
}


Listing 3:

template
<
  class Tuple,
  class SortList = typename Tuple::field_list,
  class Strategy = sorted_strategy,
  bool IsDistinct = false
>
class table_iterator {...};

template
<
  class Tuple,
  class SortList = typename Tuple::field_list,
  class Strategy = sorted_strategy,
  bool IsDistinct = false
>class table
{
public:
  typedef typename Tuple::field_list field_list;
  typedef SortList sort_list;
  typedef Tuple value_type;
  typedef table_iterator<Tuple,SortList,Strategy,IsDistinct>
    const_iterator;
  enum {is_distinct = IsDistinct};
  const_iterator begin() const {...}
  const_iterator end() const {...}
  ... // range methods
private:
  ... // implementation details
};


Listing 4:

template <class Table, class Pred>class selection_t;
template <class Table, class Pred>
class selection_iterator
{
public:
  typedef std::bidirectional_iterator_tag iterator_category;
  typedef typename selection_t<Table,Pred>::value_type
    value_type;
  typedef typename selection_t<Table,Pred>::arg::const_iterator
    arg_iterator;
  typedef selection_t<Table,Pred> relation_type;
  ...
  selection_iterator(arg_iterator itr, const relation_type* rel)
    : itr_(itr), relation_(rel) {}
  selection_iterator operator++()
  {
    ++itr_;
    move_to_next();
    return *this;
  }
  selection_iterator::value_type operator*() const
  {
    return *itr_;
  }
private:
  void move_to_next()
  {
    while (itr_ != relation_->arg_.end()
      && !(relation_->pred_)(*itr_))
    ++itr_;
  }
private:
  arg_iterator itr_;
  const relation_type* relation_;
};
template <class Table, class Pred>
class selection_t
{
  friend class selection_iterator<Table,Pred>;
public:
  typedef typename Table::field_list field_list;
  typedef typename Table::sort_list sort_list;
  typedef typename Table::value_type value_type;
  typedef selection_iterator<Table,Pred> const_iterator;
  typedef Table arg;
  enum {is_distinct = arg::is_distinct};

  selection_t(const arg& t, const Pred P) : arg_(t), pred_(p) {}
  const_iterator begin() const
  {
    const_iterator i(arg_.begin(), this);
    i.move_to_next();
    return i;
  }
  const_iterator end() const
  {
    return const_iterator(arg_.end(), this);
  }
  ...
private:
  const arg* arg_;
  Pred pred_;
};
template <class Arg, class Pred>
selection_t<Arg, Pred> selection(const Arg& t, Pred p)
{
  return selection_t<Arg, Pred>(t, p);
}


Listing 5:

COLUMN(ssn, long);
COLUMN(name, std::string);
COLUMN(year, int);
typedef
  list<ssn,
  list<name,
  list<year> > > fields;
typedef tuple<fields> tuple_type;
table<tuple_type> staff;
template <class C> class less_equal
{
public:
  less_equal(typename C::const_reference x) : x_(x) {}
  template <class Tuple>
    bool operator()(const Tuple& tp) const
  {
    return FIELD(tp, C) <= x_;
  }
private:
  typename C::const_reference x_;
};
 ...
print(selection(staff, less_equal<year>(1965)));

Listing 6:

template <class Table1, class Table2>
  class cross_product_iterator {...};
template <class Table1, class Table2> class cross_product_t
{
public:
  typedef typename concat<
    typename Table1::field_list,
    typename Table2::field_list>::result field_list;
  typedef tuple<field_list> value_type;
  typedef select<
    Table1::is_distinct,
    concat<
      typename Table1::sort_list,
      typename Table2::sort_list>::result,
    typename Table1::sort_list
    >::result sort_list;
  ...
};

Listing 7:

class selection_t
{
public:
 ...
template <class SubTuple>
  range_t<const_iterator> equal_range(const SubTuple& sub) const
{
  ...
  range_t<typename arg::const_iterator>
    r(arg_.equal_range(sub));
  selection_t<range_t<typename arg::const_iterator>,Pred>
    s(r,pred_);
  range_t<const_iterator> temp(
    const_iterator(s.begin().base().base(),this),
    const_iterator(s.end().base().base(),this)
  );
  return temp;
}
template<class SubTuple>
  const_iterator lower_bound(const SubTuple& sub) const {...}
template<class SubTuple>
  const_iterator upper_bound(const SubTuple& sub) const {...}
 ...
};


Listing 8:

template<class Table, class SubTuple>
  range_t<typename Table::const_iterator>
  selection_eq(const Table& t, const SubTuple& s)
{
  return t.equal_range(s);
}
template<class Table, class SubTuple>
  range_t<typename Table::const_iterator>
  selection_lt(const Table& t, const SubTuple& s)
{
  return range(t.begin(), t.lower_bound(s));
}





4


⌨️ 快捷键说明

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