HanoiTower                package:ref                R Documentation

_a_p_p_l_i_c_a_t_i_o_n _e_x_a_m_p_l_e _f_o_r _r_e_f_e_r_e_n_c_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     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).

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

       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 

_D_e_t_a_i_l_s:

     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.

_V_a_l_u_e:

     invisible()

_A_u_t_h_o_r(_s):

     Jens Oehlschlgel

_S_e_e _A_l_s_o:

     'ref', 'Recall'

_E_x_a_m_p_l_e_s:

         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)

