|
Boost : |
From: Richard Hogaboom (richard.hogaboom_at_[hidden])
Date: 2019-10-23 20:20:39
I've written a library at GitHub/rahogaboom/daa that does dynamic array
allocation in ptr to ptr ... style that is very simple to use(only two
routines), header file only, can allocate an unlimited number of
dimensions with subscripts for each dimension starting at an arbitrary
number e.g. 0, 1, -5 etc, can allocate anything you can take sizeof()
and allocates all in one contiguous block(stack, heap) for easy
freeing. I'm aware of the Boost.MultiArray library, however, I think
daa has some advantages. Specifically, a. each dimension has it's own
starting subscript b. it's relative simplicity(2 routines) c. it's
longevity - 30 years and two articles d. also can be used in a C only
environment. Following is the points noted in the GitHub README.md:
|Library:
   daa.hpp
Dynamic Array Allocator - C++ header file only library
   - ptr to ptr to ... style arbitrary N dimensional array allocator
of anything you can take sizeof()
   - arbitrary starting subscript(0, 1, -10, ...) for each separate
dimension - Note: for each array
     dimension the starting subscript and dimension size determine the
valid subscripts for that
     dimension - any index outside that range is invalid and will give
unpredictable results
   - allocates anything of any type(double, int, long, struct, ...)
   - one contiguous block allocation of memory for all ptr's and
data(locality of
     reference and easy free())
   - array or slices of array can be passed to subroutines to any
depth, modified there,
     and be seen at all levels
   - simple library, two routines(das()(calculate space needed) and
daa()(allocate ptr to ptr ... array))
   - no library dependencies(except standard header files)
   - initialization pointer argument set to instance of initialized
type or NULL for no initialization
   - implemented by a recursive routine(ptr_init()) which calls two
other different recursive
     routines(off() and doff())
   - see article in Embedded Systems Programming, Dec. 2000, "Flexible
Dynamic Array Allocation"(included)
   - see article in The C Users Journal, Nov. 1990. "A Flexible
Dynamic Array Allocator"(included)
   - 18 verification tests that show code usage examples, see daa_test.cpp
   - see cut/paste Examples section and API(das(), daa()) section below
   - a daa-compile.tar file is provided with a non-header
compiled(clang, clang++, gcc, g++) version
   - for an excellent reference on this type of array(ptr to ptr to
...) access see Numerical Recipes
     in C, Press, Flannery, Teukolsky, and Vettering, Cambridge
University Press, 1992, pg. 20-23.
     Ch. 1 Preliminaries -> 1.2 Some C Conventions for Scientific
Computing -> Matrices and
     Two-Dimensional Arrays. From the above reference read:
         'Here is our bottom line: We avoid the fixed-size
two-dimensional arrays of C as
          being unsuitable data structures for representing matrices
in scientific computing. We
          adopt instead the convention âpointer to array of pointers,â
with the array elements
          pointing to the first element in the rows of each matrix.'
   - this file is the entire documentation needed to use the library
   - Author: Richard Hogaboom, richard.hogaboom_at_[hidden]|
|Â Â Â Â Â Usage:
   - das() - find size necessary
     malloc()(or whatever memory allocation routine) - allocate memory
     daa() - populate allocated memory
   - allocate auto stack or static area storage e.g. unsigned char
mem[SIZE]
     das() - determine if array will fit in mem[]
     daa() - populate allocated memory
|
|Example:|
|Â Â Â Â Â Â Â Â Â Â Â int err_code = 0;
           int asize = 0;
           char *mem_ptr;
           unsigned int d[4] = {3, 5, 4, 2}; /* dimensions */
           int st[4] = {-1, -5, 10, 0}; /* starting subscripts */
           double ****array = NULL; /* array pointer */
           asize = das(sizeof(double), 4, d, &err_code);
           mem_ptr = (char *)malloc(asize);
           array = (double ****) daa(sizeof(double), 4, d, st,
&err_code, mem_ptr, NULL);|
|Â Â Â Â Â Â Â Â Â Â Â double d = array[-1][-5][10][0];|
-- Richard A Hogaboom 16 Alprilla Farm Road Hopkinton, MA 01748-1922 richard.hogaboom_at_[hidden] www.linkedin.com/in/richardhogaboom/ www.github.com/rahogaboom M508-330-3775
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk