HanoiTower {ref}R Documentation

application example for references

Description

This is an example for using references in S (R/S+) with package ref. HanoiTower implements a recursive algorithm solving the Hanoi tower problem. It is implemented such that the recursion can be done either by passing the HanoiTower by reference or by value to the workhorse function move.HanoiTower. Furthermore you can choose whether recursion should use Recall or should directly call move.HanoiTower. As the HanoiTower object is not too big, it can be extended by some garbage MBytes, that will demonstrate the advantage of passing references instead of values. The deeper we recurse, the more memory we waist by passing values (and the more memory we save by passing references). Functions move.HanoiTower and print.HanoiTower are internal (not intended to be called by the user directly).

Usage

  HanoiTower(n = 5
  , parameter.mode = c("reference", "value")[1]
  , recursion.mode = c("recall", "direct")[1]
  , garbage = 0
  , print = FALSE
  , plot = TRUE
  , sleep = 0
  )

Arguments

n number of slices
parameter.mode one of "reference" or "value" deciding how to pass the HanoiTower object
recursion.mode one of "recall" or "direct" deciding how to call recursively
garbage no. of bytes to add to the HanoiTower size
print TRUE print the HanoiTower changes
plot FALSE not to plot the HanoiTower changes
sleep no. of seconds to wait between HanoiTower changes for better monitoring of progress

Details

The Hanoi Tower problem can be described as follows: you have n slices of increasing size placed on one of three locations a,b,c such that the biggest slice is at the bottom, the next biggest slice on top of it and so forth with the smallest slice as the top of the tower. Your task is to move all slices from one stick to the other, but you are only allowed to move one slice at a time and you may never put a bigger slice on top of a smaller one. The recursive solution is: to move n slices from a to c you just need to do three steps: move n-1 slices to b, move the biggest slice to c and move n-1 slices from b to c. If n equals 1, just move from a to c.

Value

invisible()

Author(s)

Jens Oehlschlägel

See Also

ref, Recall

Examples

    HanoiTower(n=2)

 ## Not run: 
    # small memory examples
    HanoiTowerDemoBytes <- 0
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "reference"
    , recursion.mode  = "direct"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "reference"
    , recursion.mode  = "recall"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "value"
    , recursion.mode  = "direct"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "value"
    , recursion.mode  = "recall"
    , garbage = HanoiTowerDemoBytes
    )
    rm(HanoiTowerDemoBytes)

    # big memory examples
    HanoiTowerDemoBytes <- 100000
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "reference"
    , recursion.mode  = "direct"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "reference"
    , recursion.mode  = "recall"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "value"
    , recursion.mode  = "direct"
    , garbage = HanoiTowerDemoBytes
    )
    if (is.R())
      gc()
    HanoiTower(
      parameter.mode  = "value"
    , recursion.mode  = "recall"
    , garbage = HanoiTowerDemoBytes
    )
    rm(HanoiTowerDemoBytes)
  ## End(Not run)

[Package ref version 0.95 Index]