time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (3839B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2018 The Stdlib Authors.
      6 
      7 Licensed under the Apache License, Version 2.0 (the "License");
      8 you may not use this file except in compliance with the License.
      9 You may obtain a copy of the License at
     10 
     11    http://www.apache.org/licenses/LICENSE-2.0
     12 
     13 Unless required by applicable law or agreed to in writing, software
     14 distributed under the License is distributed on an "AS IS" BASIS,
     15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 See the License for the specific language governing permissions and
     17 limitations under the License.
     18 
     19 -->
     20 
     21 # isRowMajorContiguous
     22 
     23 > Determine if an array is row-major contiguous.
     24 
     25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
     26 
     27 <section class="intro">
     28 
     29 An array is **contiguous** if the memory address of each array element is adjacent to the memory address of the next array element.
     30 
     31 </section>
     32 
     33 <!-- /.intro -->
     34 
     35 <!-- Package usage documentation. -->
     36 
     37 <section class="usage">
     38 
     39 ## Usage
     40 
     41 ```javascript
     42 var isRowMajorContiguous = require( '@stdlib/ndarray/base/assert/is-row-major-contiguous' );
     43 ```
     44 
     45 #### isRowMajorContiguous( shape, strides, offset )
     46 
     47 Returns a `boolean` indicating if an array is row-major contiguous.
     48 
     49 ```javascript
     50 var shape = [ 2, 2 ];
     51 var strides = [ 2, 1 ];
     52 var offset = 25;
     53 
     54 var bool = isRowMajorContiguous( shape, strides, offset );
     55 // returns true
     56 
     57 shape = [ 10 ];
     58 strides = [ 3 ]; // every third memory element
     59 offset = 0;
     60 
     61 bool = isRowMajorContiguous( shape, strides, offset );
     62 // returns false
     63 ```
     64 
     65 </section>
     66 
     67 <!-- /.usage -->
     68 
     69 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     70 
     71 <section class="notes">
     72 
     73 </section>
     74 
     75 <!-- /.notes -->
     76 
     77 <!-- Package usage examples. -->
     78 
     79 <section class="examples">
     80 
     81 ## Examples
     82 
     83 <!-- eslint no-undef: "error" -->
     84 
     85 ```javascript
     86 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
     87 var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
     88 var strides2offset = require( '@stdlib/ndarray/base/strides2offset' );
     89 var randu = require( '@stdlib/random/base/randu' );
     90 var isRowMajorContiguous = require( '@stdlib/ndarray/base/assert/is-row-major-contiguous' );
     91 
     92 var strides;
     93 var offset;
     94 var shape;
     95 var bool;
     96 var i;
     97 var j;
     98 
     99 shape = [ 0, 0, 0 ];
    100 
    101 for ( i = 0; i < 100; i++ ) {
    102     // Generate a random array shape:
    103     shape[ 0 ] = discreteUniform( 1, 10 );
    104     shape[ 1 ] = discreteUniform( 1, 10 );
    105     shape[ 2 ] = discreteUniform( 1, 10 );
    106 
    107     // Generate strides:
    108     if ( randu() < 0.5 ) {
    109         strides = shape2strides( shape, 'row-major' );
    110     } else {
    111         strides = shape2strides( shape, 'column-major' );
    112     }
    113     j = discreteUniform( 0, shape.length-1 );
    114     strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1;
    115 
    116     strides[ 0 ] *= discreteUniform( 1, 2 ); // if scaled by 1, then single segment
    117 
    118     // Compute the index offset:
    119     offset = strides2offset( shape, strides ) + 25; // include a view offset
    120 
    121     // Determine if the array is row-major contiguous:
    122     bool = isRowMajorContiguous( shape, strides, offset );
    123     console.log( 'Shape: %s. Strides: %s. Offset: %d. Contiguous: %s.', shape.join( 'x' ), strides.join( ',' ), offset, bool );
    124 }
    125 ```
    126 
    127 </section>
    128 
    129 <!-- /.examples -->
    130 
    131 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    132 
    133 <section class="references">
    134 
    135 </section>
    136 
    137 <!-- /.references -->
    138 
    139 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    140 
    141 <section class="links">
    142 
    143 </section>
    144 
    145 <!-- /.links -->