2011-04-18 02:56:10 +08:00
|
|
|
/*********************************************************************
|
2018-12-07 05:21:03 +08:00
|
|
|
* Copyright 2018, UCAR/Unidata
|
2011-04-18 02:56:10 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
2017-03-09 08:01:10 +08:00
|
|
|
#include "dapincludes.h"
|
2011-04-18 02:56:10 +08:00
|
|
|
#include "dapodom.h"
|
|
|
|
|
|
|
|
/**********************************************/
|
|
|
|
/* Define methods for a dimension dapodometer*/
|
|
|
|
|
2019-09-18 10:27:43 +08:00
|
|
|
/* Build an odometer covering slices startslice up to, but not including, stopslice */
|
2011-04-18 02:56:10 +08:00
|
|
|
Dapodometer*
|
2013-04-24 04:18:16 +08:00
|
|
|
dapodom_fromsegment(DCEsegment* segment, size_t startindex, size_t stopindex)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
2012-08-01 04:34:13 +08:00
|
|
|
Dapodometer* odom;
|
|
|
|
|
2013-04-24 04:18:16 +08:00
|
|
|
assert(stopindex > startindex);
|
|
|
|
assert((stopindex - startindex) <= NC_MAX_VAR_DIMS);
|
2012-08-01 04:34:13 +08:00
|
|
|
odom = (Dapodometer*)calloc(1,sizeof(Dapodometer));
|
2011-04-18 02:56:10 +08:00
|
|
|
MEMCHECK(odom,NULL);
|
2013-04-24 04:18:16 +08:00
|
|
|
odom->rank = (stopindex - startindex);
|
2011-04-18 02:56:10 +08:00
|
|
|
for(i=0;i<odom->rank;i++) {
|
2013-04-24 04:18:16 +08:00
|
|
|
odom->start[i] = segment->slices[i+startindex].first;
|
|
|
|
odom->stride[i] = segment->slices[i+startindex].stride;
|
|
|
|
odom->stop[i] = (segment->slices[i+startindex].last + 1);
|
2013-02-26 12:31:06 +08:00
|
|
|
/* should the above line be instead?
|
|
|
|
odom->stop[i] = odom->start[i] + (odom->count[i]*odom->stride[i]);
|
|
|
|
*/
|
2013-04-24 04:18:16 +08:00
|
|
|
#if 0
|
|
|
|
odom->count[i] = segment->slices[i+startindex].count;
|
|
|
|
#endif
|
|
|
|
odom->declsize[i] = segment->slices[i+startindex].declsize;
|
2012-08-01 04:34:13 +08:00
|
|
|
odom->index[i] = odom->start[i];
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
|
|
|
return odom;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dapodometer*
|
2012-08-01 04:34:13 +08:00
|
|
|
dapodom_new(size_t rank,
|
|
|
|
const size_t* start, const size_t* count,
|
|
|
|
const ptrdiff_t* stride, const size_t* size)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Dapodometer* odom = (Dapodometer*)calloc(1,sizeof(Dapodometer));
|
|
|
|
MEMCHECK(odom,NULL);
|
|
|
|
odom->rank = rank;
|
|
|
|
assert(odom->rank <= NC_MAX_VAR_DIMS);
|
|
|
|
for(i=0;i<odom->rank;i++) {
|
2013-04-24 04:18:16 +08:00
|
|
|
size_t istart,icount,istop,ideclsize;
|
|
|
|
ptrdiff_t istride;
|
|
|
|
istart = (start != NULL ? start[i] : 0);
|
|
|
|
icount = (count != NULL ? count[i] : (size != NULL ? size[i] : 1));
|
|
|
|
istride = (size_t)(stride != NULL ? stride[i] : 1);
|
|
|
|
istop = istart + icount*istride;
|
|
|
|
ideclsize = (size != NULL ? size[i]: (istop - istart));
|
|
|
|
odom->start[i] = istart;
|
|
|
|
odom->stop[i] = istop;
|
|
|
|
odom->stride[i] = istride;
|
|
|
|
odom->declsize[i] = ideclsize;
|
2012-08-01 04:34:13 +08:00
|
|
|
odom->index[i] = odom->start[i];
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
|
|
|
return odom;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-01 04:34:13 +08:00
|
|
|
dapodom_free(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
if(odom) free(odom);
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
#if 0
|
2011-04-18 02:56:10 +08:00
|
|
|
char*
|
2012-08-01 04:34:13 +08:00
|
|
|
dapodom_print(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static char line[1024];
|
|
|
|
char tmp[64];
|
|
|
|
line[0] = '\0';
|
|
|
|
if(odom->rank == 0) {
|
2017-11-24 01:55:24 +08:00
|
|
|
strlcat(line,"[]",sizeof(line));
|
2011-04-18 02:56:10 +08:00
|
|
|
} else for(i=0;i<odom->rank;i++) {
|
|
|
|
sprintf(tmp,"[%lu/%lu:%lu:%lu]",
|
2012-08-01 04:34:13 +08:00
|
|
|
(size_t)odom->index[i],
|
|
|
|
(size_t)odom->start[i],
|
|
|
|
(size_t)odom->stride[i],
|
|
|
|
(size_t)odom->length[i]);
|
2017-11-24 01:55:24 +08:00
|
|
|
strlcat(line,tmp,sizeof(line));
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|
2011-04-18 02:56:10 +08:00
|
|
|
|
|
|
|
int
|
2012-08-01 04:34:13 +08:00
|
|
|
dapodom_more(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
2012-08-01 04:34:13 +08:00
|
|
|
return (odom->index[0] < odom->stop[0]);
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert current dapodometer settings to a single integer count*/
|
2012-08-01 04:34:13 +08:00
|
|
|
off_t
|
|
|
|
dapodom_count(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
2012-08-01 04:34:13 +08:00
|
|
|
off_t offset = 0;
|
2011-04-18 02:56:10 +08:00
|
|
|
for(i=0;i<odom->rank;i++) {
|
2012-08-01 04:34:13 +08:00
|
|
|
offset *= odom->declsize[i];
|
2011-04-18 02:56:10 +08:00
|
|
|
offset += odom->index[i];
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-01 04:34:13 +08:00
|
|
|
dapodom_next(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i; /* do not make unsigned */
|
|
|
|
if(odom->rank == 0) return 0;
|
2012-08-01 04:34:13 +08:00
|
|
|
for(i=odom->rank-1;i>=0;i--) {
|
|
|
|
odom->index[i] += odom->stride[i];
|
|
|
|
if(odom->index[i] < odom->stop[i]) break;
|
2011-04-18 02:56:10 +08:00
|
|
|
if(i == 0) return 0; /* leave the 0th entry if it overflows*/
|
2012-08-01 04:34:13 +08:00
|
|
|
odom->index[i] = odom->start[i]; /* reset this position*/
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**************************************************/
|
2012-08-01 04:34:13 +08:00
|
|
|
size_t
|
|
|
|
dapodom_varmcount(Dapodometer* odom, const ptrdiff_t* steps, const size_t* declsizes)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t offset = 0;
|
|
|
|
for(i=0;i<odom->rank;i++) {
|
|
|
|
size_t tmp;
|
|
|
|
tmp = odom->index[i];
|
2012-08-01 04:34:13 +08:00
|
|
|
tmp = tmp - odom->start[i];
|
|
|
|
tmp = tmp / odom->stride[i];
|
2011-04-18 02:56:10 +08:00
|
|
|
tmp = tmp * steps[i];
|
|
|
|
offset += tmp;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
/*
|
|
|
|
Given a dapodometer, compute the total
|
|
|
|
number of elements in its space.
|
|
|
|
*/
|
2011-04-18 02:56:10 +08:00
|
|
|
|
2012-08-01 04:34:13 +08:00
|
|
|
#if 0
|
|
|
|
off_t
|
|
|
|
dapodom_space(Dapodometer* odom)
|
2011-04-18 02:56:10 +08:00
|
|
|
{
|
2012-08-01 04:34:13 +08:00
|
|
|
size_t i;
|
|
|
|
off_t count = 1;
|
2011-04-18 02:56:10 +08:00
|
|
|
for(i=0;i<odom->rank;i++) {
|
2012-08-01 04:34:13 +08:00
|
|
|
count *= odom->size[i];
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
return count;
|
2011-04-18 02:56:10 +08:00
|
|
|
}
|
2012-08-01 04:34:13 +08:00
|
|
|
#endif
|