/**
     *   Calculate next value using materialized input chunk
     *
     *   Private function used when the input chunk's contents
     *  have been materialized. As we scan the cells in the input
     *  chunk, this method computes the window aggregate(s) for each
     *  non-empty cell in the input.
     */
    void MaterializedWindowChunkIterator::calculateNextValue()
    {
        Coordinates const& currPos = getPosition();
        Coordinates windowStart(_nDims);
        Coordinates windowEnd(_nDims);

        //
        //  We need to check that we're not stepping out over the limit of the
        // array's dimensional boundaries when the chunk is at the array edge.
        for (size_t i = 0; i < _nDims; i++)
        {
            windowStart[i] = std::max(currPos[i] - _chunk._array._window[i]._boundaries.first, _chunk._array._dimensions[i].getStartMin());
            windowEnd[i] = std::min(currPos[i] + _chunk._array._window[i]._boundaries.second, _chunk._array._dimensions[i].getEndMax());
        }

        uint64_t windowStartPos = _chunk.coord2pos(windowStart);
        uint64_t windowEndPos = _chunk.coord2pos(windowEnd);

        Value state;
        state.setNull(0);
        Coordinates probePos(_nDims);

        //
        //  The _inputMap contains an entry for every non-NULL cell in the input.
        // So set markers at the start and the end of the window.
        map<uint64_t, Value>::const_iterator windowIteratorCurr = _inputMap.lower_bound(windowStartPos);
        map<uint64_t, Value>::const_iterator windowIteratorEnd = _inputMap.upper_bound(windowEndPos);

        while(windowIteratorCurr != windowIteratorEnd)
        {
            uint64_t pos = windowIteratorCurr->first;
            _chunk.pos2coord(pos,probePos);

            //
            //  Sanity check. We should never go beyond the end of
            // the window as defined by the value of windowEndPos.
            SCIDB_ASSERT(( windowStartPos <= windowEndPos ));

            //
            //  Check to see if this cell is outside the window's box.
            for(size_t i=0; i<_nDims; i++)
            {
                if (probePos[i]<windowStart[i] || probePos[i]>windowEnd[i])
                {
                    //
                    //  We're now out of the window box. So calculate
                    // next probe position, reset windowIteratorCurr, and bounce
                    // along.
                    //
                    //  NOTE: This code is optimized for the 2D case.
                    //        I could calculate, depending on the
                    //        dimension that passed the disjunction
                    //        above, precisely by how much I should
                    //        step the probe. But to do so would
                    //        complicate this logic, and probably
                    //        won't help performance much.
                    SCIDB_ASSERT ((_nDims == _chunk._array._dimensions.size()));
                    SCIDB_ASSERT ((_nDims > 0 ));

                    do {
                        windowStartPos += _chunk.getStep();
                    } while ( windowStartPos <= pos );
                    windowIteratorCurr = _chunk._inputMap.lower_bound(windowStartPos);

                    goto nextIter;
                }
            }

            _aggregate->accumulateIfNeeded(state, windowIteratorCurr->second);
            windowIteratorCurr++;

            nextIter:;
        }
        _aggregate->finalResult(_nextValue, state);
    }
bool sender::isInWindow(int frameNum){
	if (windowEnd()<windowposition)
		return (frameNum>=0&&frameNum<=windowEnd()) || (frameNum>=windowposition&&frameNum<=bufsize);
	else	return frameNum>=windowposition && frameNum<=windowEnd();
}