Advertisement
zamotivator

Untitled

Jul 13th, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.45 KB | None | 0 0
  1.         void cut(CutBuilder& builder) const
  2.         {
  3.             if (builder.length() == 0) {
  4.                 /* source completed */
  5.                 return;
  6.             }
  7.  
  8.             /*
  9.              * Where source starts (relative to dimension begin)?
  10.              *
  11.              *          source
  12.              *          <==>
  13.              * INTERVAL [__|__]
  14.              *             ^
  15.              *             |
  16.              *           source
  17.              */
  18.             position_t source = builder.logical() % _interval;
  19.  
  20.             /*
  21.              * How many position absent in MAIN (not overlap) area?
  22.              *
  23.              *                          skipMain
  24.              *                          <==>
  25.              * PREFIX[___] MAIN & SUFFIX[__|__]
  26.              *                             ^
  27.              *                             |
  28.              *                           source
  29.              */
  30.             position_t skipMain;
  31.  
  32.             if (source < _prefix) {
  33.                 /*
  34.                  * Source starts inside PREFIX.
  35.                  *
  36.                  * How many position absent in MAIN (not overlap) area?
  37.                  *          skipPrefix
  38.                  *          <==>
  39.                  * PREFIX[__|__] MAIN[___] SUFFIX [___]
  40.                  *          ^
  41.                  *          |
  42.                  *        source
  43.                  */
  44.  
  45.                 position_t skipPrefix = _prefix - source;
  46.  
  47.                 if (skipPrefix > builder.length()) {
  48.                     /* source shorter than PREFIX */
  49.                     builder.skipSource(builder.length());
  50.                     /* source completed */
  51.                     return;
  52.                 } else {
  53.                     /* skip PREFIX */
  54.                     builder.skipSource(skipPrefix);
  55.                 }
  56.  
  57.                 /*
  58.                  * Now we exactly on begin of MAIN, and skipMain equal zero.
  59.                  *
  60.                  * PREFIX[___] MAIN[|__] SUFFIX[___]
  61.                  *                  ^
  62.                  *                  |
  63.                  *                source
  64.                  */
  65.                 skipMain = 0;
  66.             } else {
  67.                 /*
  68.                  * Source starts in MAIN or SUFFIX.
  69.                  *
  70.                  * PREFIX[___] MAIN & SUFFIX [__|__]
  71.                  *                              ^
  72.                  *                              |
  73.                  *                            source
  74.                  */
  75.                 skipMain = source - _prefix;
  76.             }
  77.  
  78.             if (skipMain < _main) {
  79.                 /*
  80.                  * Source starts in MAIN area.
  81.                  *
  82.                  * How many position should we copy?
  83.                  *                       copyMain
  84.                  *                       <==>
  85.                  * PREFIX[___] MAIN[____|____] SUFFIX[___]
  86.                  *                      ^
  87.                  *                      |
  88.                  *                    source
  89.                  */
  90.                 position_t copyMain = _main - skipMain;
  91.  
  92.                 /* Move result segment to source position */
  93.                 builder.skipResultToSource();
  94.  
  95.                 if (_nested) {
  96.                     /* We have nested dimensions (not fully included) */
  97.                     while(builder.length() > 0 && copyMain > 0) {
  98.                         /* Process one line from nested dimension */
  99.                         _nested->cut(builder);
  100.                         /* Go to next line */
  101.                         copyMain -= _nested->_interval;
  102.                     }
  103.                 } else {
  104.                     /*
  105.                       We do not have nested dimensions
  106.                       (or their fully included).
  107.                     */
  108.                     builder.copy(min(copyMain, builder.length()));
  109.                 }
  110.                 /* MAIN area completed! */
  111.             }
  112.  
  113.             /*
  114.              * Source now in SUFFIX.
  115.              *
  116.              * How many positions left in suffix?
  117.              *                                <==>
  118.              * PREFIX[___] MAIN[___] SUFFIX[__|__]
  119.              *                                ^
  120.              *                                |
  121.              *                              source
  122.              */
  123.             builder.skipSource(min(_interval - source,
  124.                                    builder.length()));
  125.         }
  126.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement