
#define _GNU_SOURCE 1
#include <search.h>

struct twalk_with_twalk_r_closure
@{
  void (*action) (const void *, VISIT, int);
  int depth;
@};

static void
twalk_with_twalk_r_action (const void *nodep, VISIT which, void *closure0)
@{
  struct twalk_with_twalk_r_closure *closure = closure0;

  switch (which)
    @{
    case leaf:
      closure->action (nodep, which, closure->depth);
      break;
    case preorder:
      closure->action (nodep, which, closure->depth);
      ++closure->depth;
      break;
    case postorder:
      /* @r{The preorder action incremented the depth.} */
      closure->action (nodep, which, closure->depth - 1);
      break;
    case endorder:
      --closure->depth;
      closure->action (nodep, which, closure->depth);
      break;
    @}
@}

void
twalk (const void *root, void (*action) (const void *, VISIT, int))
@{
  struct twalk_with_twalk_r_closure closure = @{ action, 0 @};
  twalk_r (root, twalk_with_twalk_r_action, &closure);
@}
