void phone_vec_expand(Phone **ph, int *maxph, int iph); /* Assumes that {ph} has been allocated with {maxph} elements. If element {ph[iph]} does not exist (i.e. {iph >= maxph}) , reallocates {ph} with a large enough size, preserving its old contents. */ void phone_vec_trim(Phone **ph, int *maxph, int nph); /* Assumes that {ph} has been allocated with {maxph} elements. If {maxph != nph}, reallocates {ph} with size {nph}, preserving {ph[0..nph-1]}. */ void phone_vec_expand(Phone **ph, int *maxph, int iph) { int oldmax = (*maxph); Phone *old = *ph; if (iph >= oldmax) { int newmax = 2*oldmax; if (iph >= newmax) { newmax = iph + 1; } newmax += 100; Phone *new = (Phone *)malloc(newmax*sizeof(Phone)); if (old != NULL) { int k; for (k = 0; k < oldmax; k++) { new[k] = old[k]; } free(old); } (*ph) = new; (*maxph) = newmax; } } void phone_vec_trim(Phone **ph, int *maxph, int nph) { int oldmax = (*maxph); Phone *old = *ph; if (nph != oldmax) { Phone *new = (Phone *)malloc(nph*sizeof(Phone)); if (old != NULL) { int k; for (k = 0; k < nph; k++) { new[k] = old[k]; } free(old); } (*ph) = new; (*maxph) = nph; } } st_map_init_costs(m, d, e, c); int pi; for(pi = 0; pi < ph.nel; pi++) { Phone *phi = &(ph.el[pi]); int vi = phi->vertex; VertexData *vd = m->vd[vi]; Point *vp = &(vd->p); /* Find vertices served by {phi}, add a fraction of their demands: */ double usage = 0.0; st_map_compute_costs(m, vi, stopDist, r, &nr, d, e, c); int k; for (k = 0; k < nr; k++) { int wi = r[k]; ... usage += frac*dem[wi]; } phi->usage = usage; st_map_reset_costs(m, r, nr, d, e, c); }