\ array                      MINFORTH Plus 1.5                        MM-200824
\ -----------------------------------------------------------------------------
\                            array voc creater
\
\               Copyright (C) 2021 manfred.mahlow@forth-ev.de
\
\ This program is free software; you can redistribute it and/or modify it under
\ the terms of the GNU General Public License as published by the Free Software
\ Foundation; either version 3 of the License, or (at your option) any later
\ version, see http://www.gnu.org/licenses .
\ -----------------------------------------------------------------------------
\
\ include array   Defines an array data voc inside a data voc.
\                 See int-array.mf how to use it.
\

  get-current        \ current is the container data voc for the new array voc

  require buffer.mf

  set-current        \ restore current ( the container voc )

  @voc ?u/i drop     \ throw an error if current is not a data voc

  voc array   @voc array definitions

  \ @voc = new array data voc , @voc >cvoc = data voc of the array elements

  warning off

  struct begin  buffer u/i +  struct end

  warning on

  @voc >cvoc item
  : of ( i oid -- oid.i )
  \ From array data object oid return the oid of array item i. i={0,size-1}
    >r @voc >cvoc u/i * r> buffer of ;

  : init ( u oid -- )
  \ Initialize the array data object oid for u array items and erase all items.
  \ An array can be reinitialized but not with a different size. 
    >r @voc >cvoc u/i * r@ buffer init r> buffer erase ;

  : size ( oid -- u )
  \ Return the number of array items of the array data object oid.
    buffer size @voc >cvoc u/i / ;

  : ? ( oid -- )
  \ Display all array items of the array data object oid.
    cr ." size = " dup @voc size .
    dup @voc size 0 do i cr dup 3 .r ." : " over @voc of ? loop drop ;

\ ------------------------------------------------------------------------------
\ Last Revision: MM-230115 released for mfp1.5-4.0.2
\                MM-211212 dat voc check in line 23 changed
\                MM-200918 MFP 4.0.1

\ Example see int-array.mf


