HanoiTower: application example for references

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/ref.R

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

1
2
3
4
5
6
7
8
  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<e4>gel

See Also

ref, Recall

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    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)

ref documentation built on May 2, 2019, 6:08 p.m.

Related to HanoiTower in ref...