Example #1
0
 /**
  * Get the integer offset of the problem integer
  *
  * @return Integer|NULL Returns null if the argument isn't set
  */
 public function getArgOffset()
 {
     try {
         return \r8\ary\calcOffset($this->getTraceByOffset(0)->getArgs(), $this->arg, \r8\ary\OFFSET_RESTRICT);
     } catch (\r8\Exception\Index $err) {
         return NULL;
     }
 }
Example #2
0
 public function testCalcOffset()
 {
     try {
         \r8\ary\calcOffset(array(), 2, "invalid offset value");
         $this->fail('An expected exception has not been raised.');
     } catch (\r8\Exception\Index $err) {
         $this->assertSame("List is empty", $err->getMessage());
     }
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -5, \r8\ary\OFFSET_NONE));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), -2, \r8\ary\OFFSET_NONE));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), -1, \r8\ary\OFFSET_NONE));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), 0, \r8\ary\OFFSET_NONE));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), 3, \r8\ary\OFFSET_NONE));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 4, \r8\ary\OFFSET_NONE));
     try {
         \r8\ary\calcOffset(array(), 2, \r8\ary\OFFSET_NONE);
         $this->fail('An expected exception has not been raised.');
     } catch (\r8\Exception\Index $err) {
         $this->assertSame("List is empty", $err->getMessage());
     }
     try {
         \r8\ary\calcOffset(range(1, 5), 5, \r8\ary\OFFSET_NONE);
         $this->fail('An expected exception has not been raised.');
     } catch (\r8\Exception\Index $err) {
         $this->assertSame("Offset is out of bounds", $err->getMessage());
     }
     try {
         \r8\ary\calcOffset(range(1, 5), -6, \r8\ary\OFFSET_NONE);
         $this->fail('An expected exception has not been raised.');
     } catch (\r8\Exception\Index $err) {
         $this->assertSame("Offset is out of bounds", $err->getMessage());
     }
     $this->assertEquals(1, \r8\ary\calcOffset(range(1, 5), -14, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(2, \r8\ary\calcOffset(range(1, 5), -8, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -5, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), -2, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), -1, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), 0, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), 3, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 4, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), 8, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), 15, \r8\ary\OFFSET_WRAP));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -14, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -8, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -5, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), -2, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), -1, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), 0, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), 3, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 4, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 8, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 15, \r8\ary\OFFSET_RESTRICT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -2, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), -1, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(0, \r8\ary\calcOffset(range(1, 5), 0, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(3, \r8\ary\calcOffset(range(1, 5), 3, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 4, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 8, \r8\ary\OFFSET_LIMIT));
     $this->assertEquals(4, \r8\ary\calcOffset(range(1, 5), 15, \r8\ary\OFFSET_LIMIT));
 }
Example #3
0
/**
 * Sets the internal array pointer to a specific offset
 *
 * @param Array $array The array to operate on
 * @param Integer $offset The offset to seek to
 * @param Integer $wrapFlag How to handle offsets outside the array range
 * @return Mixed Returns the value at the given offset. NULL will be returned
 *      if the given array is empty
 */
function seek(array &$array, $offset, $wrapFlag = FALSE)
{
    $count = count($array) - 1;
    if ($count == -1) {
        return NULL;
    }
    $offset = \r8\ary\calcOffset($array, $offset, $wrapFlag);
    // escape from the easy out
    if ($offset == 0) {
        return reset($array);
    }
    if ($offset == $count) {
        return end($array);
    }
    // Get the position of the current pointer
    $pointer = \r8\ary\pointer($array);
    // If we are already at our destination...
    if ($pointer == $offset) {
        return current($array);
    }
    // If the point we are seeking to is closer to the beginning than it is
    // to the end or to the current pointer position, seek from the start
    if ($offset < abs($pointer - $offset) && $offset < abs($count - $offset)) {
        reset($array);
        $pointer = 0;
    } else {
        if (abs($count - $offset) < abs($pointer - $offset)) {
            end($array);
            $pointer = $count;
        }
    }
    // If we are seeking backward
    if ($pointer > $offset) {
        // seek to the before final point
        for ($pointer--; $pointer >= $offset; $pointer--) {
            prev($array);
        }
    } else {
        // seek to the final point
        for ($pointer++; $pointer <= $offset; $pointer++) {
            next($array);
        }
    }
    return current($array);
}