logo

const class

sys::Range

sys::Obj
  sys::Range
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   11 Dec 05  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** Range represents a contiguous range of integers from start to
  11  ** end.  Ranges may be represented as literals in Fan source code as
  12  ** "start..end" for an inclusive end or "start...end" for an exlusive
  13  ** range.
  14  **
  15  const final class Range
  16  {
  17  
  18  //////////////////////////////////////////////////////////////////////////
  19  // Constructor
  20  //////////////////////////////////////////////////////////////////////////
  21  
  22    **
  23    ** Convenience for make(start, end, false).
  24    **
  25    new makeInclusive(Int start, Int end)
  26  
  27    **
  28    ** Convenience for make(start, end, true).
  29    **
  30    new makeExclusive(Int start, Int end)
  31  
  32    **
  33    ** Constructor with start, end, and exclusive flag (all must be non-null).
  34    **
  35    new make(Int start, Int end, Bool exclusive)
  36  
  37  //////////////////////////////////////////////////////////////////////////
  38  // Obj Overrides
  39  //////////////////////////////////////////////////////////////////////////
  40  
  41    **
  42    ** Return true if same start, end, and exclusive.
  43    **
  44    override Bool equals(Obj obj)
  45  
  46    **
  47    ** Return start ^ end.
  48    **
  49    override Int hash()
  50  
  51    **
  52    ** If inclusive return "start..end", if exclusive return "start...end".
  53    **
  54    override Str toStr()
  55  
  56  //////////////////////////////////////////////////////////////////////////
  57  // Methods
  58  //////////////////////////////////////////////////////////////////////////
  59  
  60    **
  61    ** Return start index.
  62    **
  63    ** Example:
  64    **   (1..3).start -> 1
  65    **
  66    Int start()
  67  
  68    **
  69    ** Return end index.
  70    **
  71    ** Example:
  72    **   (1..3).end -> 3
  73    **
  74    Int end()
  75  
  76    **
  77    ** Is the end index inclusive.
  78    **
  79    ** Example:
  80    **   (1..3).inclusive -> true
  81    **   (1...3).inclusive -> false
  82    **
  83    Bool inclusive()
  84  
  85    **
  86    ** Is the end index exclusive.
  87    **
  88    ** Example:
  89    **   (1..3).exclusive -> false
  90    **   (1...3).exclusive -> true
  91    **
  92    Bool exclusive()
  93  
  94    **
  95    ** Return if this range contains the specified integer.
  96    **
  97    ** Example:
  98    **   (1..3).contains(2)  ->  true
  99    **   (1..3).contains(4)  ->  false
 100    **
 101    Bool contains(Int i)
 102  
 103    **
 104    ** Call the specified function for each integer in the range.
 105    **
 106    ** Example:
 107    **   ('a'..'z').each |Int i| { echo(i) }
 108    **
 109    Void each(|Int i| c)
 110  
 111  }