Synopsis - Synopsis/PTree/operations.hh

File: Synopsis/PTree/operations.hh

      1//
      2// Copyright (C) 2004 Stefan Seefeld
      3// All rights reserved.
      4// Licensed to the public under the terms of the GNU LGPL (>= 2),
      5// see the file COPYING for details.
      6//
      7#ifndef Synopsis_PTree_operations_hh_
      8#define Synopsis_PTree_operations_hh_
      9
     10#include <Synopsis/PTree/Node.hh>
     11#include <cassert>
     12
     13namespace Synopsis
     14{
     15namespace PTree
     16{
     17bool operator == (const Node &p, char c);
     18inline bool operator != (const Node &p, char c) { return !operator == (p, c);}
     19bool operator == (const Node &p, const char *str);
     20inline bool operator != (const Node &p, const char *str) { return !operator == (p, str);}
     21bool operator == (const Node &p, const Node &q);
     22inline bool operator != (const Node &p, const Node &q) { return !operator == (p, q);}
     23bool equal(const Node &p, const char *str, size_t len);
     24bool equal(const Node *p, const Node *q);
     25bool equiv(const Node *p, const Node *q);
     26
     27//. Return the last cons cell.
     28const Node *last(const Node *);
     29//. Return the last cons cell.
     30Node *last(Node *);
     31inline const Node *first(const Node *p) { return p ? p->car() : 0;}
     32inline Node *first(Node *p) { return p ? p->car() : 0;}
     33inline const Node *rest(const Node *p) { return p ? p->cdr() : 0;}
     34inline Node *rest(Node *p) { return p ? p->cdr() : 0;}
     35inline const Node *nth(const Node *p, size_t n)
     36{
     37  while(p && n-- > 0) p = p->cdr();
     38  return p ? p->car() : 0;
     39}
     40inline Node *nth(Node *p, size_t n)
     41{
     42  while(p && n-- > 0) p = p->cdr();
     43  return p ? p->car() : 0;
     44}
     45inline const Node *tail(const Node *p, size_t k)
     46{
     47  while(p && k-- > 0) p = p->cdr();
     48  return p;
     49}
     50inline Node *tail(Node *p, size_t k)
     51{
     52  while(p && k-- > 0) p = p->cdr();
     53  return p;
     54}
     55
     56const Node *second(const Node *);
     57Node *second(Node *);
     58const Node *third(const Node *);
     59Node *third(Node *);
     60int length(const Node *);
     61
     62inline const Node *cadr(const Node *p) { return p->cdr()->car();}
     63inline Node *cadr(Node *p) { return p->cdr()->car();}
     64inline const Node *cddr(const Node *p) { return p->cdr()->cdr();}
     65inline Node *cddr(Node *p) { return p->cdr()->cdr();}
     66//. compute Caa..ar
     67const Node *ca_ar(const Node *);
     68Node *ca_ar(Node *);
     69
     70Node *cons(Node *, Node *);
     71List *list();
     72List *list(Node *);
     73List *list(Node *, Node *);
     74List *list(Node *, Node *, Node *);
     75List *list(Node *, Node *, Node *, Node *);
     76List *list(Node *, Node *, Node *, Node *, Node *);
     77List *list(Node *, Node *, Node *, Node *, Node *, Node *);
     78List *list(Node *, Node *, Node *, Node *, Node *, Node *,
     79	   Node *);
     80List *list(Node *, Node *, Node *, Node *, Node *, Node *,
     81	   Node *, Node *);
     82Node *copy(Node *);
     83Node *append(Node *, Node *);
     84Node *replace_all(Node *, Node *, Node *);
     85Node *subst(Node *, Node *, Node *);
     86Node *subst(Node *, Node *, Node *, Node *, Node *);
     87Node *subst(Node *, Node *, Node *, Node *,
     88	    Node *, Node *, Node *);
     89Node *shallow_subst(Node *, Node *, Node *);
     90Node *shallow_subst(Node *, Node *, Node *, Node *, Node *);
     91Node *shallow_subst(Node *, Node *, Node *, Node *,
     92		    Node *, Node *, Node *);
     93Node *shallow_subst(Node *, Node *, Node *, Node *,
     94		    Node *, Node *, Node *, Node *, Node *);
     95Node *subst_sublist(Node *, Node *, Node *);
     96
     97/* they cause side-effect */
     98Node *nconc(Node *, Node *);
     99Node *nconc(Node *, Node *, Node *);
    100template <typename N> N *nconc(N *p, Node *q)
    101{
    102  assert(p);
    103  last(p)->set_cdr(q);
    104  return p;
    105}
    106
    107Node *snoc(Node *, Node *);
    108template <typename N> N *snoc(N *p, Node *q)
    109{
    110  return nconc(p, cons(q, 0));
    111}
    112
    113}
    114}
    115
    116#endif

Generated on Thu Apr 16 16:28:02 2009 by
synopsis (version devel)