CreatorExample.cpp

Demonstration how to equip a multidimensional array with on-demand creation of data slices. This function will output the following information:

(3000 3001 3002 3003 3004 3005 3006 )(3007 3008 3009 3010 3011 3012 3013 )(3014 3015 3016 3017 3018 3019 3020 )
 Full 3D array, slices created on demand:
(0 1 2 3 4 5 6 )(7 8 9 10 11 12 13 )(14 15 16 17 18 19 20 )
(1000 1001 1002 1003 1004 1005 1006 )(1007 1008 1009 1010 1011 1012 1013 )(1014 1015 1016 1017 1018 1019 1020 )
(2000 2001 2002 2003 2004 2005 2006 )(2007 2008 2009 2010 2011 2012 2013 )(2014 2015 2016 2017 2018 2019 2020 )
(3000 3001 3002 3003 3004 3005 3006 )(3007 3008 3009 3010 3011 3012 3013 )(3014 3015 3016 3017 3018 3019 3020 )
(4000 4001 4002 4003 4004 4005 4006 )(4007 4008 4009 4010 4011 4012 4013 )(4014 4015 4016 4017 4018 4019 4020 )
#include <vector/MultiArray.hpp>
#include <vector>
#include <iostream>

using namespace Fiber;
using namespace std;

struct  SliceProvider : DataCreator<double>
{
        MultiIndex<3>    DataSpace;
        vector<double*>  SliceData;

public:
        // Constructor initialize the slice data to "unloaded" status (might open file)
        SliceProvider(const MultiIndex<3>&Size)
        : DataSpace(Size)
        {
                SliceData.resize( DataSpace.maxidx() ); 

                for(int i=0; i<SliceData.size(); i++)
                        SliceData[i] = 0;
        }

        /*
          Internal data creation routine. 
          A more advanced version might keep track of the used
          memory and remove unused slices from RAM.
          Here, we might also interface some I/O (might read/write file).
         */
        Iterator<double> create(index_t slice_index)
        {
        double *d = SliceData[ slice_index ]; 
        index_t length = DataSpace.subidx().size(); 

                if (!d)
                {
                        d = new double[ length ]; 
                        SliceData[ slice_index ] = d; 
                        for(int i=0; i<length; i++)
                        {
                                d[i] = 1000*slice_index + i;
                        }
                } 

                return Iterator<double>(length,d);
        }

        // Destructor cleans up all memory (might close file)
        ~SliceProvider()
        {
                for(int i=0; i < SliceData.size(); i++)
                        delete [] SliceData[i];
        }

        // Implementation of virtual slice creation function
        override CreativeIterator<double> slice(int Dim, index_t sliceID)
        {
                if (Dim==2)
                {
                        return CreativeIterator<double>( create(sliceID), this);
                }
                return CreativeIterator<double>(0,0, this);
        }

        // Implementation of one-dimensional data creation (not supported here)
        override double&getElement(index_t)
        {
        static  double nix = 0.0;
                return nix;
        }
};

int DataCreatorExample()
{
MultiIndex<3> ArraySize = MIndex(7,3,5); 

SliceProvider SP(ArraySize); 
MultiArray<3,double> M(ArraySize, &SP ); 

MultiArray<2,double> M2D = M.slice(3); 

        cout << M2D << endl; 

        cout << " Full 3D array, slices created on demand:" << endl; 

        cout << M << endl;

        return 1;
}