[Gridflow-cvs] [svn] commit: r5593 - in /trunk/src: classes1.cxx classes3.cxx formats.cxx gridflow.cxx gridflow.hxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Wed May 5 18:57:23 EDT 2010


Author: matju
Date: Wed May  5 18:57:21 2010
New Revision: 5593

Log:
passage de P<Dim> à Dim

Modified:
    trunk/src/classes1.cxx
    trunk/src/classes3.cxx
    trunk/src/formats.cxx
    trunk/src/gridflow.cxx
    trunk/src/gridflow.hxx

Modified: trunk/src/classes1.cxx
==============================================================================
--- trunk/src/classes1.cxx (original)
+++ trunk/src/classes1.cxx Wed May  5 18:57:21 2010
@@ -64,10 +64,10 @@
 
 Numop *op_add, *op_sub, *op_mul, *op_div, *op_mod, *op_shl, *op_and, *op_put;
 
-static void   expect_dim_dim_list    (P<Dim> d) {if (d->n!=1) RAISE("dimension list should be Dim[n], not %s",d->to_s());}
-//static void expect_min_one_dim     (P<Dim> d) {if (d->n<1 ) RAISE("minimum 1 dimension");}
-static void   expect_max_one_dim     (P<Dim> d) {if (d->n>1 ) RAISE("expecting Dim[] or Dim[n], got %s",d->to_s());}
-//static void expect_exactly_one_dim (P<Dim> d) {if (d->n!=1) RAISE("expecting Dim[n], got %s",d->to_s());}
+static void   expect_dim_dim_list    (const Dim &d) {if (d->n!=1) RAISE("dimension list should be Dim[n], not %s",d->to_s());}
+//static void expect_min_one_dim     (const Dim &d) {if (d->n<1 ) RAISE("minimum 1 dimension");}
+static void   expect_max_one_dim     (const Dim &d) {if (d->n>1 ) RAISE("expecting Dim[] or Dim[n], got %s",d->to_s());}
+//static void expect_exactly_one_dim (const Dim &d) {if (d->n!=1) RAISE("expecting Dim[n], got %s",d->to_s());}
 
 //****************************************************************
 \class GridCast : FObject {
@@ -90,32 +90,35 @@
 // out0 nt to be specified explicitly
 \class GridImport : FObject {
 	\attr NumberTypeE cast;
-	\attr P<Dim> dim; // size of grids to send
+	/*\attr*/ Dim dim; // size of grids to send
+	\attr bool per_message;
 	PtrGrid dim_grid;
 	\constructor (...) {
+		per_message = true;
 		dim_grid.constrain(expect_dim_dim_list);
 		this->cast = argc>=2 ? NumberTypeE_find(argv[1]) : int32_e;
 		if (argc>2) RAISE("too many arguments");
 		if (argc>0 && argv[0]!=gensym("per_message")) {
+			per_message=false;
 			dim_grid=new Grid(argv[0]);
 			dim = dim_grid->to_dim();
 			if (!dim->prod()) RAISE("target grid size must not be zero");
 		}
 	}
 	~GridImport() {}
-	\decl 0 reset() {int32 foo[1]={0}; if (out) while (out->dim) out->send(1,foo);}
+	\decl 0 reset() {int32 foo[1]={0}; if (out) while (out->sender) out->send(1,foo);}
 	\decl 0 symbol(t_symbol *x);
 	\decl 0 to_ascii(...);
 	\decl 0 bang() {_0_list(0,0);}
 	//\decl 0 list(...);
-	\decl 1 per_message() {dim=0; dim_grid=0;}
+	\decl 1 per_message() {per_message=true; dim_grid=0;}
 	\grin 0
 	\grin 1 int32
 	template <class T> void process (long n, T *data) {
 		if (in.size()<=0) in.resize(1);
 		if (!in[0]) in[0]=new GridInlet((FObject *)this,stromgol);
 		while (n) {
-			if (!out || !out->dim) out = new GridOutlet(this,0,dim?dim:in[0]->dim,cast);
+			if (!out || !out->sender) out = new GridOutlet(this,0,per_message?in[0]->dim:dim,cast);
 			long n2 = min((long)n,out->dim->prod()-out->dex);
 			out->send(n2,data);
 			n-=n2; data+=n2;
@@ -125,15 +128,16 @@
 
 GRID_INLET(0) {} GRID_FLOW {process(n,data);} GRID_END
 GRID_INPUT(1,dim_grid) {
-	P<Dim> d = dim_grid->to_dim();
+	Dim d = dim_grid->to_dim();
 	if (!d->prod()) RAISE("target grid size must not be zero");
 	dim = d;
+	per_message=false;
 } GRID_END
 
 \def 0 symbol(t_symbol *x) {
 	const char *name = x->s_name;
 	long n = strlen(name);
-	if (!dim) out=new GridOutlet(this,0,new Dim(n));
+	if (per_message) out=new GridOutlet(this,0,Dim(n));
 	process(n,(uint8 *)name);
 }
 \def 0 to_ascii(...) {
@@ -141,14 +145,14 @@
 	pd_oprint(os,argc,argv);
 	string s = os.str();
 	long n = s.length();
-	if (!dim) out=new GridOutlet(this,0,new Dim(n),cast);
+	if (per_message) out=new GridOutlet(this,0,Dim(n),cast);
 	process(n,(uint8 *)s.data());
 }
 \def 0 list(...) {//first two lines are there until grins become strictly initialized.
 	if (in.size()<=0) in.resize(1);
 	if (!in[0]) in[0]=new GridInlet((FObject *)this,stromgol);
 	in[0]->from_list(argc,argv,cast);
-	if (!argc && !dim) out = new GridOutlet(this,0,new Dim(0),cast);
+	if (!argc && per_message) out = new GridOutlet(this,0,Dim(0),cast);
 }
 
 \end class {install("#import",2,1); add_creator("@import"); stromgol = &GridImport_grid_0_hand;}
@@ -365,7 +369,7 @@
 	long cs; // chunksize used in put_at
 	\constructor (Grid *r=0) {
 		put_at.constrain(expect_max_one_dim);
-		this->r = r?r:new Grid(new Dim(),int32_e,true);
+		this->r = r?r:new Grid(Dim(),int32_e,true);
 		op = op_put;
 		wdex  = NEWBUF(int32,Dim::MAX_DIM); // temporary buffer, copy of put_at
 		fromb = NEWBUF(int32,Dim::MAX_DIM);
@@ -421,7 +425,7 @@
 	int nd = nb-nc+na-1;
 	COPY(v,in->dim->v,na-1);
 	COPY(v+na-1,r->dim->v+nc,nb-nc);
-	out=new GridOutlet(this,0,new Dim(nd,v),r->nt);
+	out=new GridOutlet(this,0,Dim(nd,v),r->nt);
 	if (nc>0) in->set_chunk(na-1);
 } GRID_FLOW {
 	int na = in->dim->n;
@@ -476,8 +480,8 @@
 GRID_INLET(1) {
 	NumberTypeE nt = NumberTypeE_type_of(data);
 	if (!put_at) { // reassign
-		if (in[0].dim) r.next = new Grid(in->dim,nt);
-		else           r      = new Grid(in->dim,nt);
+		if (in[0].sender) r.next = new Grid(in->dim,nt);
+		else              r      = new Grid(in->dim,nt);
 		return;
 	}
 	// put_at ( ... )
@@ -536,7 +540,7 @@
 \def 1 put_at (...) {
 	if (argv[0].a_type==A_LIST) put_at=convert(argv[0],(Grid **)0);
 	else {
-		put_at=new Grid(new Dim(argc),int32_e);
+		put_at=new Grid(Dim(argc),int32_e);
 		int32 *v = (int32 *)*put_at;
 		for (int i=0; i<argc; i++) v[i]=convert(argv[i],(int32 *)0);
 	}
@@ -549,7 +553,7 @@
 \class GridOp : FObject {
 	\attr Numop *op;
 	PtrGrid r;
-	\constructor (Numop *op, Grid *r=0) {this->op=op; this->r=r?r:new Grid(new Dim(),int32_e,true);}
+	\constructor (Numop *op, Grid *r=0) {this->op=op; this->r=r?r:new Grid(Dim(),int32_e,true);}
 	\grin 0
 	\grin 1
 };
@@ -610,7 +614,7 @@
 	COPY(v+yi,in->dim->v+an-bn,bn);
 	if (seed) SAME_DIM(an-(yi+1),in->dim,(yi+1),seed->dim,0);
 	if (!op->on(*data)->fold) RAISE("operator %s does not support fold",op->name);
-	out=new GridOutlet(this,0,new Dim(an-1,v),in->nt);
+	out=new GridOutlet(this,0,Dim(an-1,v),in->nt);
 	in->set_chunk(yi);
 	if (in->dim->prod(yi)==0) {
 		long n = out->dim->prod();
@@ -689,8 +693,8 @@
 	\constructor (Grid *r=0) {
 		this->op = op_mul;
 		this->fold = op_add;
-		this->seed = new Grid(new Dim(),int32_e,true);
-		this->r    = r ? r : new Grid(new Dim(),int32_e,true);
+		this->seed = new Grid(Dim(),int32_e,true);
+		this->r    = r ? r : new Grid(Dim(),int32_e,true);
 	}
 	\grin 0
 	\grin 1
@@ -723,7 +727,7 @@
 GRID_INLET(0) {
 	SAME_TYPE(in,r);
 	SAME_TYPE(in,seed);
-	P<Dim> a=in->dim, b=r->dim;
+	Dim a=in->dim, b=r->dim;
 	if (a->n<1) RAISE("a: minimum 1 dimension");
 	if (b->n<1) RAISE("b: minimum 1 dimension");
 	if (seed->dim->n != 0) RAISE("seed must be a scalar");
@@ -732,12 +736,12 @@
 	int32 v[n];
 	COPY(v,a->v,a->n-1);
 	COPY(v+a->n-1,b->v+1,b->n-1);
-	out=new GridOutlet(this,0,new Dim(n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(n,v),in->nt);
 	in->set_chunk(a->n-1);
 	long sjk=r->dim->prod(), sj=in->dim->prod(a->n-1), sk=sjk/sj;
 	long chunk = max(1L,GridOutlet::MAX_PACKET_SIZE/sjk);
 	T *rdata = (T *)*r;
-	r2=new Grid(new Dim(chunk*sjk),r->nt);
+	r2=new Grid(Dim(chunk*sjk),r->nt);
 	T *buf3 = (T *)*r2;
 	for (long i=0; i<sj; i++)
 		for (long j=0; j<chunk; j++)
@@ -816,7 +820,7 @@
 	PtrGrid r;
 	\constructor (Numop *op, Grid *r=0) {
 		this->op = op;
-		this->r = r ? r : new Grid(new Dim(),int32_e,true);
+		this->r = r ? r : new Grid(Dim(),int32_e,true);
 	}
 	\grin 0
 	\grin 1
@@ -824,13 +828,13 @@
 
 GRID_INLET(0) {
 	SAME_TYPE(in,r);
-	P<Dim> a = in->dim;
-	P<Dim> b = r->dim;
+	Dim a = in->dim;
+	Dim b = r->dim;
 	int n = a->n+b->n;
 	int32 v[n];
 	COPY(v,a->v,a->n);
 	COPY(v+a->n,b->v,b->n);
-	out=new GridOutlet(this,0,new Dim(n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(n,v),in->nt);
 } GRID_FLOW {
 	long b_prod = r->dim->prod();
 	if (!b_prod) return; /* nothing to do... and avoid deadly divisions by zero */
@@ -908,9 +912,9 @@
 		if (nn[i]<0) nn[i]=0;
 		to2[i] = fromb[i]+stepb[i]*nn[i];
 	}
-	P<Dim> d;
-	if (from->dim->n==0) { d = new Dim(*nn); }
-	else { nn[n]=n;        d = new Dim(n+1,nn); }
+	Dim d;
+	if (from->dim->n==0) { d = Dim(*nn); }
+	else { nn[n]=n;        d = Dim(n+1,nn); }
 	int total = d->prod();
 	out=new GridOutlet(this,0,d,from->nt);
 	if (total==0) return;
@@ -953,20 +957,15 @@
 	\constructor () {}
 	\grin 0
 };
-GRID_INLET(0) {
-	//in->set_mode(0);
-} GRID_FINISH {
-	outlet_bang(outlets[0]);
-} GRID_END
+GRID_INLET(0) {} GRID_FINISH {outlet_bang(outlets[0]);} GRID_END
 \end class {install("#finished",1,1); add_creator("@finished");}
 \class GridDim : FObject {
 	\constructor () {}
 	\grin 0
 };
 GRID_INLET(0) {
-	GridOutlet out(this,0,new Dim(in->dim->n));
+	GridOutlet out(this,0,Dim(in->dim->n));
 	out.send(in->dim->n,in->dim->v);
-	//in->set_mode(0);
 } GRID_END
 \end class {install("#dim",1,1); add_creator("@dim");}
 \class GridType : FObject {
@@ -975,14 +974,13 @@
 };
 GRID_INLET(0) {
 	outlet_symbol(outlets[0],gensym(const_cast<char *>(number_type_table[in->nt].name)));
-	/*in->set_mode(0);*/
 } GRID_END
 \end class {install("#type",1,1); add_creator("@type");}
 
 //****************************************************************
 //{ Dim[*As]<T>,Dim[B] -> Dim[*Cs]<T> }
 \class GridRedim : FObject {
-	\attr P<Dim> dim;
+	/*\attr*/ Dim dim;
 	PtrGrid dim_grid;
 	PtrGrid temp; // temp->dim is not of the same shape as dim
 	~GridRedim() {}
@@ -998,7 +996,7 @@
 
 GRID_INLET(0) {
 	long a=in->dim->prod(), b=dim->prod();
-	if (a<b) temp=new Grid(new Dim(a),in->nt);
+	if (a<b) temp=new Grid(Dim(a),in->nt);
 	out=new GridOutlet(this,0,dim,in->nt);
 } GRID_FLOW {
 	long i = dex;
@@ -1017,7 +1015,7 @@
 } GRID_END
 
 GRID_INPUT(1,dim_grid) {
-	P<Dim> d = dim_grid->to_dim();
+	Dim d = dim_grid->to_dim();
 //	if (!d->prod()) RAISE("target grid size must not be zero"); else post("d->prod=%d",d->prod());
 	dim = d;
 } GRID_END

Modified: trunk/src/classes3.cxx
==============================================================================
--- trunk/src/classes3.cxx (original)
+++ trunk/src/classes3.cxx Wed May  5 18:57:21 2010
@@ -21,7 +21,7 @@
 	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 #include "gridflow.hxx.fcs"
-static void expect_max_one_dim (P<Dim> d) {if (d->n>1) RAISE("expecting Dim[] or Dim[n], got %s",d->to_s());}
+static void expect_max_one_dim (const Dim &d) {if (d->n>1) RAISE("expecting Dim[] or Dim[n], got %s",d->to_s());}
 // BAD HACK: GCC complains: unimplemented (--debug mode only) (i don't remember which GCC this was)
 #ifdef HAVE_DEBUG
 #define SCOPY(a,b,n) COPY(a,b,n)
@@ -42,7 +42,7 @@
 		sigout = new t_outlet *[chans];
 		for (int i=0; i<n; i++) sigout[i] = outlet_new((t_object *)bself,&s_signal);
 		chans = n;
-		blah=new Grid(new Dim(16384,n),float32_e);
+		blah=new Grid(Dim(16384,n),float32_e);
 		start=0;
 		size=0;
 	}
@@ -106,7 +106,7 @@
 GRID_INLET(0) {
 	NOTEMPTY(r);
 	SAME_TYPE(in,r);
-	P<Dim> d = in->dim;
+	Dim &d = in->dim;
 	if (d->n != r->dim->n) RAISE("wrong number of dimensions");
 	int w = which_dim;
 	if (w<0) w+=d->n;
@@ -120,7 +120,7 @@
 			if (v[i]!=r->dim->v[i]) RAISE("dimensions mismatch: dim #%i, left is %d, right is %d",i,v[i],r->dim->v[i]);
 		}
 	}
-	out=new GridOutlet(this,0,new Dim(d->n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(d->n,v),in->nt);
 	in->set_chunk(w);
 } GRID_FLOW {
 	int w = which_dim;
@@ -178,13 +178,15 @@
 #undef FOO
 
 GRID_INLET(0) {
+	if (in->dim.n<1) RAISE("minimum 1 dimension");
 	out=new GridOutlet(this,0,in->dim);
 	in->set_chunk(in->dim->n-1);
 } GRID_FLOW {
 	long m = in->dim->prod(in->dim->n-1);
+	post("in->dim=%s m=%ld",in->dim.to_s(),m);
 	T *foo[m];
 	T  bar[m];
-	for (; n; n-=m,data+=m) {
+	if (m) for (; n; n-=m,data+=m) {
 		for (int i=0; i<m; i++) foo[i] = &data[i];
 		qsort(foo,m,sizeof(T *),(comparator_t)GradeFunction<T>::comparator);
 		for (int i=0; i<m; i++) bar[i] = foo[i]-(T *)data;
@@ -221,13 +223,13 @@
 		RAISE("would swap dimensions %d and %d but this grid has only %d dimensions", dim1,dim2,in->dim->n);
 	memswap(v+d1,v+d2,1);
 	if (d1==d2) {
-		out=new GridOutlet(this,0,new Dim(in->dim->n,v), in->nt);
+		out=new GridOutlet(this,0,Dim(in->dim->n,v), in->nt);
 	} else {
 		nd = in->dim->prod(1+max(d1,d2));
 		nc = in->dim->v[max(d1,d2)];
 		nb = nc&&nd ? in->dim->prod(1+min(d1,d2))/nc/nd : 0;
 		na = in->dim->v[min(d1,d2)];
-		out=new GridOutlet(this,0,new Dim(in->dim->n,v), in->nt);
+		out=new GridOutlet(this,0,Dim(in->dim->n,v), in->nt);
 		in->set_chunk(min(d1,d2));
 	}
 	// Turns a Grid[*,na,*nb,nc,*nd] into a Grid[*,nc,*nb,na,*nd].
@@ -264,7 +266,7 @@
 	if (d>=in->dim->n || d<0)
 		RAISE("would reverse dimension %d but this grid has only %d dimensions",
 			dim1,in->dim->n);
-	out=new GridOutlet(this,0,new Dim(in->dim->n,in->dim->v), in->nt);
+	out=new GridOutlet(this,0,Dim(in->dim->n,in->dim->v), in->nt);
 	in->set_chunk(d);
 } GRID_FLOW {
 	long f1=in->dim->prod(d), f2=in->dim->prod(d+1);
@@ -290,7 +292,7 @@
 	if (in->dim->n != 3) RAISE("expecting 3 dims");
 	if (in->dim->v[2] != 1) RAISE("expecting 1 channel");
 	in->set_chunk(1);
-	out=new GridOutlet(this,0,new Dim(2), in->nt);
+	out=new GridOutlet(this,0,Dim(2), in->nt);
 	sumx=0; sumy=0; sum=0; y=0;
 } GRID_FLOW {
 	int sx = in->dim->v[1];
@@ -316,7 +318,7 @@
 \end class {install("#centroid",1,3);}
 
 //****************************************************************
-static void expect_pair (P<Dim> dim) {if (dim->prod()!=2) RAISE("expecting only two numbers. Dim(2)");}
+static void expect_pair (const Dim &dim) {if (dim.prod()!=2) RAISE("expecting only two numbers. Dim(2)");}
 
 \class GridMoment : FObject {
 	\constructor (int order=1) {
@@ -339,8 +341,8 @@
 	if (in->dim->v[2] != 1) RAISE("expecting 1 channel");
 	in->set_chunk(1);
 	switch (order) {
-	    case 1: out=new GridOutlet(this,0,new Dim(2  ), in->nt); break;
-	    case 2: out=new GridOutlet(this,0,new Dim(2,2), in->nt); break;
+	    case 1: out=new GridOutlet(this,0,Dim(2  ), in->nt); break;
+	    case 2: out=new GridOutlet(this,0,Dim(2,2), in->nt); break;
 	    default: RAISE("supports only orders 1 and 2 for now");
 	}
 	sumx=0; sumy=0; sumxy=0; sum=0; y=0;
@@ -456,15 +458,15 @@
 				s.x/s.area};
 			float a[] = {s.area};
 			send_out(3,1,a);
-			GridOutlet o2(this,2,new Dim(2));   o2.send(2,cooked+4);
-			GridOutlet o1(this,1,new Dim(2,2)); o1.send(4,cooked);
+			GridOutlet o2(this,2,Dim(2));   o2.send(2,cooked+4);
+			GridOutlet o1(this,1,Dim(2,2)); o1.send(4,cooked);
 		} else {
 			float32 cooked[4] = {s.y,s.x1,s.y,s.x2};
-			GridOutlet o1(this,1,new Dim(2,2)); o1.send(4,cooked);
+			GridOutlet o1(this,1,Dim(2,2)); o1.send(4,cooked);
 		}
 		label++;
 	}
-	out = new GridOutlet(this,0,new Dim(sy,sx,1),in->nt);
+	out = new GridOutlet(this,0,Dim(sy,sx,1),in->nt);
 	out->send(n,dat);
 	DELBUF(dat);
 } GRID_END
@@ -488,7 +490,7 @@
 	COPY(v,in->dim->v,n);
 	v[n-1]--;
 	in->set_chunk(in->dim->n-1);
-	out=new GridOutlet(this,0,new Dim(n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(n,v),in->nt);
 } GRID_FLOW {
 	int m = in->dim->prod(in->dim->n-1);
 	for (; n; n-=m,data+=m) {
@@ -501,8 +503,8 @@
 
 //****************************************************************
 \class GridBorder : FObject {
-	\attr P<Dim> diml;
-	\attr P<Dim> dimr;
+	/*\attr */ Dim diml;
+	/*\attr */ Dim dimr;
 	PtrGrid diml_grid;
 	PtrGrid dimr_grid;
 	\grin 0
@@ -526,7 +528,7 @@
 	int32 v[n];
 	for (int i=0; i<n; i++) v[i]=in->dim->v[i]+diml->v[i]+dimr->v[i];
 	in->set_chunk(0);
-	out=new GridOutlet(this,0,new Dim(n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(n,v),in->nt);
 } GRID_FLOW {
 	int sy = in->dim->v[0];
 	int sx = in->dim->v[1]; int zx = sx+diml->v[1]+dimr->v[1];
@@ -548,19 +550,14 @@
 
 \end class {install("#border",3,1);}
 
-static void expect_picture (P<Dim> d) {
-	if (d->n!=3) RAISE("(height,width,chans) dimensions please");}
-static void expect_rgb_picture (P<Dim> d) {
-	expect_picture(d);
-	if (d->get(2)!=3) RAISE("(red,green,blue) channels please");}
-static void expect_rgba_picture (P<Dim> d) {
-	expect_picture(d);
-	if (d->get(2)!=4) RAISE("(red,green,blue,alpha) channels please");}
+static void expect_picture (const Dim &d) {if (d->n!=3) RAISE("(height,width,chans) dimensions please");}
+static void expect_rgb_picture  (const Dim &d) {expect_picture(d); if (d->get(2)!=3) RAISE("(red,green,blue) channels please");}
+static void expect_rgba_picture (const Dim &d) {expect_picture(d); if (d->get(2)!=4) RAISE("(red,green,blue,alpha) channels please");}
 
 //****************************************************************
 //{ Dim[A,B,*Cs]<T>,Dim[D,E]<T> -> Dim[A,B,*Cs]<T> }
 
-static void expect_convolution_matrix (P<Dim> d) {
+static void expect_convolution_matrix (const Dim &d) {
 	if (d->n != 2) RAISE("only exactly two dimensions allowed for now (got %d)",
 		d->n);
 }
@@ -584,8 +581,8 @@
 		b.constrain(expect_convolution_matrix); plan=0;
 		this->op = op_mul;
 		this->fold = op_add;
-		this->seed = new Grid(new Dim(),int32_e,true);
-		this->b= r ? r : new Grid(new Dim(1,1),int32_e,true);
+		this->seed = new Grid(Dim(),int32_e,true);
+		this->b= r ? r : new Grid(Dim(1,1),int32_e,true);
 		this->wrap = true;
 		this->anti = true;
 	}
@@ -610,7 +607,7 @@
 }
 
 template <class T> void GridConvolve::make_plan (T bogus) {
-	P<Dim> da = a->dim, db = b->dim;
+	Dim &db = b->dim;
 	long dby = db->get(0);
 	long dbx = db->get(1);
 	if (plan) delete[] plan;
@@ -640,8 +637,7 @@
 GRID_INLET(0) {
 	SAME_TYPE(in,b);
 	SAME_TYPE(in,seed);
-	P<Dim> da=in->dim, db=b->dim;
-	if (!db) RAISE("right inlet has no grid");
+	Dim &da=in->dim, &db=b->dim;
 	if (!seed) RAISE("seed missing");
 	if (db->n != 2) RAISE("right grid must have two dimensions");
 	if (da->n < 2) RAISE("left grid has less than two dimensions");
@@ -655,7 +651,7 @@
 	//a=new Grid(da,in->nt); // with this condition it's 2% faster but takes more RAM.
 	int v[da->n]; COPY(v,da->v,da->n);
 	if (!wrap) {v[0]-=db->v[0]-1; v[1]-=db->v[1]-1;}
-	out=new GridOutlet(this,0,new Dim(da->n,v),in->nt);
+	out=new GridOutlet(this,0,Dim(da->n,v),in->nt);
 } GRID_FLOW {
 	COPY((T *)*a+dex, data, n);
 } GRID_FINISH {
@@ -697,9 +693,8 @@
 /* "#scale_by" does quick scaling of pictures by integer factors */
 /*{ Dim[A,B,3]<T> -> Dim[C,D,3]<T> }*/
 
-static void expect_scale_factor (P<Dim> dim) {
-	if (dim->prod()!=1 && dim->prod()!=2)
-		RAISE("expecting only one or two numbers");
+static void expect_scale_factor (const Dim &dim) {
+	if (dim->prod()!=1 && dim->prod()!=2) RAISE("expecting only one or two numbers");
 }
 
 \class GridScaleBy : FObject {
@@ -723,9 +718,9 @@
 };
 
 GRID_INLET(0) {
-	P<Dim> a = in->dim;
+	Dim &a = in->dim;
 	expect_picture(a);
-	out=new GridOutlet(this,0,new Dim(a->get(0)*scaley,a->get(1)*scalex,a->get(2)),in->nt);
+	out=new GridOutlet(this,0,Dim(a->get(0)*scaley,a->get(1)*scalex,a->get(2)),in->nt);
 	in->set_chunk(1);
 } GRID_FLOW {
 	int rowsize = in->dim->prod(1);
@@ -778,12 +773,12 @@
 };
 
 GRID_INLET(0) {
-	P<Dim> a = in->dim;
+	Dim &a = in->dim;
 	if (a->n!=3) RAISE("(height,width,chans) please");
-	out=new GridOutlet(this,0,new Dim(a->get(0)/scaley,a->get(1)/scalex,a->get(2)),in->nt);
+	out=new GridOutlet(this,0,Dim(a->get(0)/scaley,a->get(1)/scalex,a->get(2)),in->nt);
 	in->set_chunk(1);
 	// i don't remember why two rows instead of just one.
-	temp=new Grid(new Dim(2,in->dim->get(1)/scalex,in->dim->get(2)),in->nt);
+	temp=new Grid(Dim(2,in->dim->get(1)/scalex,in->dim->get(2)),in->nt);
 } GRID_FLOW {
 	int rowsize = in->dim->prod(1);
 	int rowsize2 = temp->dim->prod(1);
@@ -807,7 +802,7 @@
 			}
 			#undef LOOP
 			y++;
-			if (y%scaley==0 && out->dim) {
+			if (y%scaley==0 && out->sender) {
 				op_div->map(rowsize2,buf,(T)(scalex*scaley));
 				out->send(rowsize2,buf);
 				CLEAR(buf,rowsize2);
@@ -818,7 +813,7 @@
 	#undef Z
 	} else {
 	#define Z(z) buf[p+z]=data[i+z]
-		for (; n>0 && out->dim; data+=rowsize, n-=rowsize,y++) {
+		for (; n>0 && out->sender; data+=rowsize, n-=rowsize,y++) {
 			if (y%scaley!=0) continue;
 			#define LOOP(z) for (int i=0,p=0; p<rowsize2; i+=xinc, p+=z)
 			switch(in->dim->get(2)) {
@@ -850,7 +845,7 @@
 GRID_INLET(0) {
 	NOTEMPTY(r);
 	SAME_TYPE(in,r);
-	P<Dim> a = in->dim;
+	Dim &a = in->dim;
 	expect_rgba_picture(a);
 	if (a->get(1)!=r->dim->get(1)) RAISE("same width please");
 	if (a->get(0)!=r->dim->get(0)) RAISE("same height please");
@@ -878,9 +873,7 @@
 // pad1,pad2 only are there for 32-byte alignment
 struct Line {int32 y1,x1,y2,x2,x,m,ox,pad2;};
 
-static void expect_polygon (P<Dim> d) {
-	if (d->n!=2 || d->get(1)!=2) RAISE("expecting Dim[n,2] polygon");
-}
+static void expect_polygon (const Dim &d) {if (d->n!=2 || d->get(1)!=2) RAISE("expecting Dim[n,2] polygon");}
 
 enum DrawMode {DRAW_FILL,DRAW_LINE,DRAW_POINT};
 enum OmitMode {OMIT_NONE,OMIT_LAST,OMIT_ODD};
@@ -927,7 +920,7 @@
 	if (!polygon) return;
 	int tnl = polygon->dim->get(0);
 	int nl = omit==OMIT_LAST ? tnl-1 : omit==OMIT_ODD ? (tnl+1)/2 : tnl;
-	lines=new Grid(new Dim(nl,8), int32_e);
+	lines=new Grid(Dim(nl,8), int32_e);
 	Line *ld = (Line *)(int32 *)*lines;
 	int32 *pd = *polygon;
 	for (int i=0,j=0; i<nl; i++) {
@@ -960,7 +953,7 @@
 	int nl = lines->dim->get(0);
 	qsort((int32 *)*lines,nl,sizeof(Line),order_by_starting_scanline);
 	int cn = color->dim->prod();
-	color2=new Grid(new Dim(cn*16), color->nt);
+	color2=new Grid(Dim(cn*16), color->nt);
 	for (int i=0; i<16; i++) COPY((T *)*color2+cn*i,(T *)*color,cn);
 } GRID_FLOW {
 	int nl = lines->dim->get(0);
@@ -1038,7 +1031,7 @@
 \end class {install("#draw_polygon",3,1); add_creator("@draw_polygon");}
 
 //****************************************************************
-static void expect_position(P<Dim> d) {
+static void expect_position(const Dim &d) {
 	if (d->n!=1) RAISE("position should have 1 dimension, not %d", d->n);
 	if (d->v[0]!=2) RAISE("position dim 0 should have 2 elements, not %d", d->v[0]);
 }
@@ -1056,7 +1049,7 @@
 		this->image.constrain(expect_picture);
 		if (image) this->image=image;
 		if (position) this->position=position;
-		else this->position=new Grid(new Dim(2),int32_e,true);
+		else this->position=new Grid(Dim(2),int32_e,true);
 	}
 	\grin 0
 	\grin 1
@@ -1229,7 +1222,7 @@
 	\constructor (int n=2, NumberTypeE nt=int32_e) {
 		if (n<1) RAISE("n=%d must be at least 1",n);
 		if (n>32) RAISE("n=%d is too many?",n);
-		a = new Grid(new Dim(n),nt,true);
+		a = new Grid(Dim(n),nt,true);
 		this->n=n;
 		ninlets_set(this->n);
 	}
@@ -1289,7 +1282,7 @@
 	float th = angle * M_PI / 18000;
 	for (int i=0; i<2; i++) for (int j=0; j<2; j++)
 		rotator[(i?to:from)*n+(j?to:from)] = (int32)round(scale*cos(th+(j-i)*M_PI/2));
-	GridOutlet out(this,0,new Dim(n,n),int32_e);
+	GridOutlet out(this,0,Dim(n,n),int32_e);
 	out.send(n*n,rotator);
 }
 \def 0 axis(int from, int to, int n) {
@@ -1302,7 +1295,7 @@
 }
 \end class {install("#rotatificator",2,1);}
 
-static void expect_min_one_dim (P<Dim> d) {
+static void expect_min_one_dim (const Dim &d) {
 	if (d->n<1) RAISE("expecting at least one dimension, got %s",d->to_s());}
 
 #define OP(x) op_dict[string(#x)]
@@ -1337,12 +1330,12 @@
 	int32 v[r->dim->n];
 	COPY(v,r->dim->v,r->dim->n-1);
 	v[r->dim->n-1]=1;
-	P<Dim> t = new Dim(r->dim->n,v);
+	Dim t = Dim(r->dim->n,v);
 	if (!t->equal(in->dim)) RAISE("left %s must be equal to right %s except last dimension should be 1",in->dim->to_s(),r->dim->to_s());
 	in->set_chunk(0);
 	int32 w[2] = {numClusters,r->dim->v[r->dim->n-1]};
-	sums   = new Grid(new Dim(2,w),r->nt,  true);
-	counts = new Grid(new Dim(1,w),int32_e,true);
+	sums   = new Grid(Dim(2,w),r->nt,  true);
+	counts = new Grid(Dim(1,w),int32_e,true);
 } GRID_FLOW {
 	#define FOO(U) make_stats(n,data,(U *)*r);
 	TYPESWITCH(r->nt,FOO,)
@@ -1375,7 +1368,7 @@
 	out = new GridOutlet(this,0,in->dim,in->nt);
 	in->set_chunk(w);
 	int sxc = in->dim->prod(w);
-	r2 = new Grid(new Dim(sxc),in->nt);
+	r2 = new Grid(Dim(sxc),in->nt);
 	T *rdata = (T *)*r2;
 	size_t rn = r->dim->prod();
 	for (int i=0; i<sxc; i++) rdata[i] = ((T *)*r)[mod(i,rn)];

Modified: trunk/src/formats.cxx
==============================================================================
--- trunk/src/formats.cxx (original)
+++ trunk/src/formats.cxx Wed May  5 18:57:21 2010
@@ -163,7 +163,8 @@
 	GridHeader head;
 	int endian;
 	NumberTypeE nt;
-	P<Dim> headerless_dim; // if null: headerful; if Dim: it is the assumed dimensions of received grids
+	Dim dim; // it is the assumed dimensions of received grids
+	bool headerless;
 	\grin 0
 	\constructor (t_symbol *mode, string filename) {
 		nt = int32_e;
@@ -183,11 +184,11 @@
 };
 \def 0 bang () {
 	//post("#io.grid 0 bang: ftell=%ld",ftell(f));
-	P<Dim> dim;
+	Dim dim;
 	if (feof(f)) {outlet_bang(bself->te_outlet); return;}
 	//post("#in grid bang: offset %ld",ftell(f));
-	if (headerless_dim) {
-		dim = headerless_dim;
+	if (headerless) {
+		dim = this->dim;
 	} else {
 		int r = fread(&head,1,8,f);
 		if (feof(f)) {outlet_bang(bself->te_outlet); return;} /* damn */
@@ -211,7 +212,7 @@
 		int32 dimv[head.dimn];
 		if (fread(dimv,1,head.dimn*4,f)<size_t(head.dimn*4)) RAISE("can't read dimension list");
 		if (endian != is_le()) swap32(head.dimn,(uint32 *)dimv);
-		dim = new Dim(head.dimn,dimv);
+		dim = Dim(head.dimn,dimv);
 	}
 	GridOutlet out(this,0,dim,nt);
 	long nn = dim->prod();
@@ -226,7 +227,7 @@
 }
 
 GRID_INLET(0) {
-	if (!headerless_dim) {
+	if (!headerless) {
 		strncpy(head.magic,is_le()?"\x7fgrid":"\x7fGRID",5);
 		switch (in->nt) {
 		case uint8_e: head.type = 9; break;
@@ -257,9 +258,10 @@
 		t_binbuf *b = (t_binbuf *)argv[0]; argc = binbuf_getnatom(b); argv = (t_atom2 *)binbuf_getvec(b);}
 	int v[argc];
 	for (int i=0; i<argc; i++) v[i] = argv[i];
-	headerless_dim = new Dim(argc,v);
-}
-\def 0 headerful () {headerless_dim = 0;}
+	dim = Dim(argc,v);
+	headerless = true;
+}
+\def 0 headerful () {headerless = false;}
 \def 0 seek_byte (int64 pos) {fseek(f,pos,SEEK_SET);}
 
 //\def void raw_open_gzip_in(string filename) {
@@ -313,7 +315,7 @@
 	int maxnum = getuint();
 	if (maxnum!=255) RAISE("expected max to be 255 (8 bits per value)");
 	size_t sxc = sx*sc;
-	GridOutlet out(this,0,new Dim(sy,sx,sc),cast);
+	GridOutlet out(this,0,Dim(sy,sx,sc),cast);
 	uint8 row[sx*3];
 	switch (b) {
 		case '2': case '3': {

Modified: trunk/src/gridflow.cxx
==============================================================================
--- trunk/src/gridflow.cxx (original)
+++ trunk/src/gridflow.cxx Wed May  5 18:57:21 2010
@@ -196,7 +196,7 @@
 
 // !@#$ big leak machine?
 // returns a string like "Dim[240,320,3]"
-char *Dim::to_s() {
+char *Dim::to_s() const {
 	// if you blow 256 chars it's your own fault
 	char buf[256];
 	char *s = buf;

Modified: trunk/src/gridflow.hxx
==============================================================================
--- trunk/src/gridflow.hxx (original)
+++ trunk/src/gridflow.hxx Wed May  5 18:57:21 2010
@@ -278,7 +278,7 @@
 //template <class T> class P : T * {};
 //a reference counting pointer class
 //note: T <= CObject
-//used mostly as P<Dim>, P<Grid>, P<BitPacking>
+//used mostly as P<Grid>, P<BitPacking>
 template <class T> class P {
 public:
 #define INCR if (p) p->refcount++;
@@ -302,7 +302,7 @@
 };
 
 void gfmemcopy(uint8 *out, const uint8 *in, long n);
-template <class T> inline void COPY(T *dest, T *src, long n) {
+template <class T> inline void COPY(T *dest, const T *src, long n) {
 	gfmemcopy((uint8*)dest,(const uint8*)src,n*sizeof(T));
 }
 template <class T> inline void CLEAR(T *dest, long n) {
@@ -329,7 +329,7 @@
 // a Dim is a list of dimensions that describe the shape of a grid
 typedef int32 Card; /* should be switched to long int soon */
 struct Dim : CObject {
-	static const Card MAX_DIM=16; // maximum number of dimensions in a grid
+	static const Card MAX_DIM=15; // maximum number of dimensions in a grid
 	Card n;
 	Card v[MAX_DIM]; // real stuff
 	void check(); // test invariants
@@ -338,6 +338,7 @@
 	Dim(Card a)              {n=1;v[0]=a;              check();}
 	Dim(Card a,Card b)       {n=2;v[0]=a;v[1]=b;       check();}
 	Dim(Card a,Card b,Card c){n=3;v[0]=a;v[1]=b;v[2]=c;check();}
+	Dim &operator = (const Dim &a) {n=a.n; COPY(&*v,&*a.v,n); check(); return *this;}
 	Dim(Dim *a, Dim *b, Dim *c=0) {
 		n=a->n+b->n; if(c) n+=c->n;
 		if (n>Dim::MAX_DIM) RAISE("too many dims");
@@ -346,19 +347,24 @@
 		if(c) COPY(v+a->n+b->n,c->v,c->n);
 	}
 	Card count() {return n;}
-	Card get(Card i) {return v[i];}
+	Card get       (Card i) const {return v[i];}
+	Card operator[](Card i) const {return v[i];}
+	const Dim & operator *() const {return *this;}
+	      Dim & operator *()       {return *this;}
+	const Dim *operator ->() const {return this;}
+	      Dim *operator ->()       {return this;}
 /*	Dim *range(Card i, Card j) {return new Dim(...);} */
-	Card prod(Card start=0, Card end=-1) {
+	Card prod(Card start=0, Card end=-1) const {
 		if (start<0) start+=n;
 		if (end  <0) end  +=n;
 		Card tot=1;
 		for (Card i=start; i<=end; i++) tot *= v[i];
 		return tot;
 	}
-	char *to_s(); // should be std::string
-	bool equal(P<Dim> o) {
-		if (n!=o->n) return false;
-		for (Card i=0; i<n; i++) if (v[i]!=o->v[i]) return false;
+	char *to_s() const; // should be std::string
+	bool equal(const Dim &o) {
+		if (n!=o.n) return false;
+		for (Card i=0; i<n; i++) if (v[i]!=o[i]) return false;
 		return true;
 	}
 };
@@ -569,26 +575,25 @@
 
 // ****************************************************************
 struct Grid : CObject {
-	P<Dim> dim;
+	Dim dim;
 	NumberTypeE nt;
 	void *data;
 	int state; /* 0:borrowing 1:owning -1:expired(TODO) */
-	Grid(P<Dim> dim, NumberTypeE nt, bool clear=false) {
+	Grid(const Dim &dim, NumberTypeE nt, bool clear=false) {
 		state=1; 
-		if (!dim) RAISE("hell");
 		init(dim,nt);
 		if (clear) {long size = bytes(); CLEAR((char *)data,size);}
 	}
 	Grid(const t_atom &x) {state=1; init_from_atom(x);}
 	Grid(int n, t_atom *a, NumberTypeE nt_=int32_e) {state=1; init_from_list(n,a,nt_);}
-	template <class T> Grid(P<Dim> dim, T *data) {
+	template <class T> Grid(const Dim &dim, T *data) {
 		state=0; this->dim=dim;
 		this->nt=NumberTypeE_type_of((T *)0);
 		this->data = data;
 	}
 	// parens are necessary to prevent overflow at 1/16th of the word size (256 megs)
-	long bytes() { return dim->prod()*(number_type_table[nt].size/8); }
-	P<Dim> to_dim () { return new Dim(dim->prod(),(int32 *)*this); }
+	long bytes() {return dim.prod()*(number_type_table[nt].size/8);}
+	Dim to_dim() {return Dim(dim.prod(),(int32 *)*this);}
 #define FOO(T) operator T *() { return (T *)data; }
 EACH_NUMBER_TYPE(FOO)
 #undef FOO
@@ -599,14 +604,12 @@
 	}
 	~Grid() {if (state==1 && data) free(data);}
 private:
-	void init(P<Dim> dim, NumberTypeE nt) {
+	void init(const Dim &dim, NumberTypeE nt) {
 		this->dim = dim;
 		this->nt = nt;
 		data = 0;
-		if (dim) {
-			data = memalign(16,bytes()+16);
-			if (!data) RAISE("out of memory (?) trying to get %ld bytes",bytes());
-		}
+		data = memalign(16,bytes()+16);
+		if (!data) RAISE("out of memory (?) trying to get %ld bytes",bytes());
 		//fprintf(stderr,"rdata=%p data=%p align=%d\n",rdata,data,align);
 	}
 	void init_from_atom(const t_atom &x);
@@ -617,7 +620,7 @@
 // DimConstraint interface:
 // return if d is acceptable
 // else RAISE with proper descriptive message
-typedef void (*DimConstraint)(P<Dim> d);
+typedef void (*DimConstraint)(const Dim &d);
 
 struct PtrGrid : public P<Grid> {
 	DimConstraint dc;
@@ -638,11 +641,11 @@
 #undef INCR
 #undef DECR
 
-static inline P<Dim> convert(const t_atom &x, P<Dim> *foo) {
+static inline Dim convert(const t_atom &x, Dim *foo) {
 	Grid *d = convert(x,(Grid **)0);
 	if (!d) RAISE("urgh");
-	if (d->dim->n!=1) RAISE("dimension list must have only one dimension itself");
-	return new Dim(d->dim->v[0],(int32 *)(d->data));
+	if (d->dim.n!=1) RAISE("dimension list must have only one dimension itself");
+	return Dim(d->dim[0],(int32 *)d->data);
 }
 static inline PtrGrid convert(const t_atom &x, PtrGrid *foo) {return PtrGrid(convert(x,(Grid **)0));}
 
@@ -684,7 +687,7 @@
 	FObject *parent;
 	const GridHandler *gh;
 	GridOutlet *sender;
-	P<Dim> dim;
+	Dim dim;
 	NumberTypeE nt; // kill this
 	long dex;
 	int chunk;
@@ -694,7 +697,7 @@
 		parent(parent_), gh(gh_), sender(0), dim(0), nt(int32_e), dex(0), chunk(-1), bufi(0) {}
 	~GridInlet() {}
 	void set_chunk(long whichdim);
-	int32 factor() {return buf?buf->dim->prod():1;} // which is usually not the same as this->dim->prod(chunk)
+	int32 factor() {return buf?buf->dim.prod():1;} // which is usually not the same as this->dim->prod(chunk)
 	void begin(GridOutlet *sender);
 	void finish();
 	template <class T> void flow(long n, T *data); // n=-1 is begin, and n=-2 is finish.
@@ -747,13 +750,15 @@
 	static const long MAX_PACKET_SIZE = 1<<12;
 // those are set only once
 	FObject *parent;
-	P<Dim> dim; // dimensions of the grid being sent
+	Dim dim; // dimensions of the grid being sent
 	NumberTypeE nt;
 	std::vector<GridInlet *> inlets; // which inlets are we connected to
 // those are updated during transmission
 	long dex;  // how many numbers were already sent in this connection
 
-	GridOutlet(FObject *parent_, int woutlet, P<Dim> dim_, NumberTypeE nt_=int32_e);
+	GridOutlet *sender; // dummy (because of P<Dim> to Dim)
+
+	GridOutlet(FObject *parent_, int woutlet, const Dim &dim_, NumberTypeE nt_=int32_e);
 	~GridOutlet() {}
 	void callback(GridInlet *in);
 
@@ -828,7 +833,7 @@
 #define SAME_TYPE(_a_,_b_) if ((_a_)->nt != (_b_)->nt) RAISE("same type please (%s has %s; %s has %s)", \
 	#_a_, number_type_table[(_a_)->nt].name, \
 	#_b_, number_type_table[(_b_)->nt].name);
-static void SAME_DIM(int n, P<Dim> a, int ai, P<Dim> b, int bi) {
+static void SAME_DIM(int n, const Dim &a, int ai, const Dim &b, int bi) {
 	if (ai+n > a->n) RAISE("left hand: not enough dimensions");
 	if (bi+n > b->n) RAISE("right hand: not enough dimensions");
 	for (int i=0; i<n; i++) {



More information about the Gridflow-cvs mailing list