1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*********************************************************************
* Copyright 2018, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
#include "zincludes.h"
/*Forward*/
static int buildodom(int rank, NCZOdometer** odomp);
void
nczodom_reset(NCZOdometer* odom)
{
int r;
for(r=0;r<odom->rank;r++)
odom->index[r] = odom->start[r];
}
NCZOdometer*
nczodom_new(int rank, const size64_t* start, const size64_t* stop, const size64_t* stride, const size64_t* len)
{
int i;
NCZOdometer* odom = NULL;
if(buildodom(rank,&odom)) return NULL;
odom->properties.stride1 = 1; /* assume */
odom->properties.start0 = 1; /* assume */
for(i=0;i<rank;i++) {
odom->start[i] = (size64_t)start[i];
odom->stop[i] = (size64_t)stop[i];
odom->stride[i] = (size64_t)stride[i];
odom->len[i] = (size64_t)len[i];
if(odom->start[i] != 0) odom->properties.start0 = 0;
if(odom->stride[i] != 1) odom->properties.stride1 = 0;
}
nczodom_reset(odom);
for(i=0;i<rank;i++)
assert(stop[i] >= start[i] && stride[i] > 0 && (len[i]+1) >= stop[i]);
return odom;
}
NCZOdometer*
nczodom_fromslices(int rank, const NCZSlice* slices)
{
size_t i;
NCZOdometer* odom = NULL;
if(buildodom(rank,&odom)) return NULL;
odom->properties.stride1 = 1; /* assume */
odom->properties.start0 = 1; /* assume */
for(i=0;i<rank;i++) {
odom->start[i] = slices[i].start;
odom->stop[i] = slices[i].stop;
odom->stride[i] = slices[i].stride;
odom->len[i] = slices[i].len;
if(odom->start[i] != 0) odom->properties.start0 = 0;
if(odom->stride[i] != 1) odom->properties.stride1 = 0;
}
nczodom_reset(odom);
for(i=0;i<rank;i++) {
assert(slices[i].stop >= slices[i].start && slices[i].stride > 0);
assert((slices[i].stop - slices[i].start) <= slices[i].len);
}
return odom;
}
void
nczodom_free(NCZOdometer* odom)
{
if(odom == NULL) return;
nullfree(odom->start);
nullfree(odom->stop);
nullfree(odom->stride);
nullfree(odom->len);
nullfree(odom->index);
nullfree(odom);
}
int
nczodom_more(const NCZOdometer* odom)
{
return (odom->index[0] < odom->stop[0]);
}
void
nczodom_next(NCZOdometer* odom)
{
int i;
int rank;
rank = odom->rank;
for(i=rank-1;i>=0;i--) {
odom->index[i] += odom->stride[i];
if(odom->index[i] < odom->stop[i]) break;
if(i == 0) goto done; /* leave the 0th entry if it overflows */
odom->index[i] = odom->start[i]; /* reset this position */
}
done:
return;
}
/* Get the value of the odometer */
size64_t*
nczodom_indices(const NCZOdometer* odom)
{
return odom->index;
}
size64_t
nczodom_offset(const NCZOdometer* odom)
{
int i;
size64_t offset;
int rank = odom->rank;
offset = 0;
for(i=0;i<rank;i++) {
#if 1
offset *= odom->len[i];
#else
offset *= odom->stop[i];
#endif
offset += odom->index[i];
}
return offset;
}
static int
buildodom(int rank, NCZOdometer** odomp)
{
int stat = NC_NOERR;
NCZOdometer* odom = NULL;
if(odomp) {
if((odom = calloc(1,sizeof(NCZOdometer))) == NULL)
goto done;
odom->rank = rank;
if((odom->start=malloc(sizeof(size64_t)*rank))==NULL) goto nomem;
if((odom->stop=malloc(sizeof(size64_t)*rank))==NULL) goto nomem;
if((odom->stride=malloc(sizeof(size64_t)*rank))==NULL) goto nomem;
if((odom->len=malloc(sizeof(size64_t)*rank))==NULL) goto nomem;
if((odom->index=malloc(sizeof(size64_t)*rank))==NULL) goto nomem;
*odomp = odom; odom = NULL;
}
done:
nczodom_free(odom);
return stat;
nomem:
stat = NC_ENOMEM;
goto done;
}
/* Compute the total avail in last position */
size64_t
nczodom_avail(const NCZOdometer* odom)
{
size64_t avail;
/* The best we can do is compute the count for the rightmost index */
avail = (odom->stop[odom->rank-1] - odom->start[odom->rank-1]);
return avail;
}
/*
Incr the odometer by nczodom_avail() values.
Calling nczodom_next at that point should properly increment
rest of the odometer.
*/
void
nczodom_skipavail(NCZOdometer* odom)
{
if(odom->rank > 0)
odom->index[odom->rank-1] = odom->stop[odom->rank-1];
}
#if 0
size64_t
nczodom_laststride(const NCZOdometer* odom)
{
assert(odom != NULL && odom->rank > 0);
return odom->stride[odom->rank-1];
}
size64_t
nczodom_lastlen(const NCZOdometer* odom)
{
assert(odom != NULL && odom->rank > 0);
return odom->len[odom->rank-1];
}
#endif
|