% resizer - sequence with overridden reported element size
%
% resizer :: Size:[[1,E]], seq([_->A]) -> seq([Size->A]).
classdef resizer < seq
	properties (GetAccess=private, SetAccess=private)
		source
		newsize
		check
	end
	methods
		function o=resizer(newsize,source,check)
			if ~size_match(elsize(source),newsize),
				error('seq.resizer: size mismatch');
			end
			if nargin<3, check=0; end
			o.source=source; 
			o.newsize=newsize;
			o.check=check;
		end

		function x=head(a), x=head(a.source); if a.check, size_check(a.newsize,size(x)); end; end
		function d=next(d), d.source=next(d.source); if isempty(d.source), d=nil; end; end
		function s=elsize(a), s=a.newsize; end
		function s=tostring(a), s=tostring(a.source); end
	end
end

function b=size_match(s1,s2)
	b=all((s1==s2) | isnan(s1) | isnan(s2));
end

function b=size_check(spec,size)
	if ~all((spec==size) | isnan(spec))
		error('seq.resizer: element size does not match specification');
	end
end
