int sm_block_partition(sm_matrix *A, sm_matrix **L, sm_matrix **R) { int cols_visited, rows_visited; register sm_row *prow; register sm_col *pcol; if (A->nrows == 0) { return 0; } for(prow = A->first_row; prow != 0; prow = prow->next_row) { prow->flag = 0; } for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) { pcol->flag = 0; } cols_visited = rows_visited = 0; if (visit_row(A, A->first_row, &rows_visited, &cols_visited)) { return 0; } else { *L = sm_alloc(); *R = sm_alloc(); for(prow = A->first_row; prow != 0; prow = prow->next_row) { if (prow->flag) { copy_row(*L, prow); } else { copy_row(*R, prow); } } return 1; } }
sdm_t *sdm_alloc(const enum eSM_Type t, const size_t nsubmaps, const size_t initial_reserve) { ASSERT_RETURN_IF(nsubmaps < 1, NULL); size_t alloc_size = sizeof(sdm_t) + sizeof(sm_t *) * (nsubmaps - 1); sdm_t *dm = (sdm_t *)__sd_malloc(alloc_size); ASSERT_RETURN_IF(!dm, NULL); memset(dm, 0, alloc_size); size_t i = 0, nelems = (initial_reserve / nsubmaps) + 1; for (; i < nsubmaps; i++) { if (!(dm->maps[i] = sm_alloc(t, nelems))) break; /* Allocation error */ } if (i != nsubmaps) { /* Handle allocation error */ for (i = 0; i < nsubmaps; i++) { if (dm->maps[i]) free(dm->maps[i]); else break; } free(dm); dm = NULL; } else { /* Set routing defaults */ sdm_set_routing(dm, NULL, NULL); dm->nmaps = nsubmaps; } return dm; }
pset do_sm_minimum_cover(pset_family A) { sm_matrix *M; sm_row *sparse_cover; sm_element *pe; pset cover; register int i, base, rownum; register unsigned val; register pset last, p; M = sm_alloc(); rownum = 0; for( p=A->data, last= p+A->count*A->wsize; p< last; p+=A->wsize) { for( i = (p[0] & 0x03ff); i > 0; ) for( val = p[ i], base = -- i << 5; val != 0; base++, val >>= 1) if ( val & 1) { (void) sm_insert(M, rownum, base); } rownum++; } sparse_cover = sm_minimum_cover(M, ((int *) 0), 1, 0); sm_free(M); cover = set_clear(((unsigned int *) malloc(sizeof(unsigned int) * ( ((A->sf_size) <= 32 ? 2 : (((((A->sf_size)-1) >> 5) + 1) + 1))))), A->sf_size); for( pe = sparse_cover->first_col; pe != 0; pe = pe->next_col) { (cover[((( pe->col_num) >> 5) + 1)] |= 1 << (( pe->col_num) & (32-1))); } sm_row_free(sparse_cover); return cover; }
void kernel_extract_init() { kernel_cube_table = cubeindex_alloc(); co_kernel_table = array_alloc(sm_row *, 0); kernel_cube_matrix = sm_alloc(); }
sm_matrix * sm_alloc_size(int row, int col) { register sm_matrix *A; A = sm_alloc(); sm_resize(A, row, col); return A; }
sm_t *sm_cpy(sm_t **m, const sm_t *src) { RETURN_IF(!m || !src, NULL); /* BEHAVIOR */ size_t i; const enum eSM_Type t = (enum eSM_Type)src->f.type; size_t ss = sm_size(src), src_buf_size = ST_SIZE_TO_ALLOC_SIZE(ss, sm_elem_size(t)); if (*m) { if (sd_is_using_ext_buffer((const sd_t *)src)) { /* If using ext buffer, we'll have grow limits */ sm_reset(*m); size_t oe = st_capacity(*m), os = ST_SIZE_TO_ALLOC_SIZE(oe, sm_elem_size(t)); RETURN_IF(os < src_buf_size, NULL); /* BEHAVIOR */ *m = sm_alloc_raw(t, S_TRUE, *m, os); } else { if ((*m)->f.type == t) { st_reserve(m, ss); } else { sm_free(m); *m = NULL; } } } if (!*m) *m = sm_alloc(t, ss); RETURN_IF(!*m || st_capacity(*m) < ss, NULL); /* BEHAVIOR */ switch (t) { /* * Fast copy: compact structure (without strings) */ case SM_I32I32: case SM_U32U32: case SM_IntInt: case SM_IntPtr: memcpy(*m, src, src_buf_size); break; /* * Slow map copy for types having strings as key or value: */ #define case_SM_CPY_InsertLoop(SM_xy, ST, INSERTF) \ case SM_xy: \ for (i = 0; i < ss; i++) { \ const ST *s = (const ST *)sm_enum_r(*m, i); \ INSERTF(m, s->x.k, s->v); \ } \ break; case_SM_CPY_InsertLoop(SM_IntStr, struct SMapIS, sm_is_insert); case_SM_CPY_InsertLoop(SM_StrInt, struct SMapSI, sm_si_insert); case_SM_CPY_InsertLoop(SM_StrStr, struct SMapSS, sm_ss_insert); case_SM_CPY_InsertLoop(SM_StrPtr, struct SMapSP, sm_sp_insert); #undef case_SM_CPY_InsertLoop default: break; } return *m; }
int sm_block_partition(sm_matrix *A, sm_matrix **L, sm_matrix **R) { int cols_visited, rows_visited; register sm_row *prow; register sm_col *pcol; /* Avoid the trivial case */ if (A->nrows == 0) { return 0; } /* Reset the visited flags for each row and column */ for(prow = A->first_row; prow != 0; prow = prow->next_row) { prow->flag = 0; } for(pcol = A->first_col; pcol != 0; pcol = pcol->next_col) { pcol->flag = 0; } cols_visited = rows_visited = 0; if (visit_row(A, A->first_row, &rows_visited, &cols_visited)) { /* we found all of the rows */ return 0; } else { *L = sm_alloc(); *R = sm_alloc(); for(prow = A->first_row; prow != 0; prow = prow->next_row) { if (prow->flag) { copy_row(*L, prow); } else { copy_row(*R, prow); } } return 1; } }
sm_matrix * sm_dup(sm_matrix *A) { register sm_row *prow; register sm_element *p; register sm_matrix *B; B = sm_alloc(); if (A->last_row != 0) { sm_resize(B, A->last_row->row_num, A->last_col->col_num); for(prow = A->first_row; prow != 0; prow = prow->next_row) { for(p = prow->first_col; p != 0; p = p->next_col) { (void) sm_insert(B, p->row_num, p->col_num); } } } return B; }
int main(int argc, const char **argv) { if (argc < 3) return syntax_error(argv, 5); int csize = atoi(argv[1]), climit0 = atoi(argv[2]); if (csize < 1 || csize > 4) return syntax_error(argv, 6); if (climit0 < 0) return syntax_error(argv, 7); int exit_code = 0; size_t count = 0; size_t cmax = csize == 4 ? 0xffffffff : 0xffffffff & ((1 << (csize * 8)) - 1); size_t climit = climit0 ? S_MIN((size_t)climit0, cmax) : cmax; #ifdef COUNTER_USE_BITSET #define COUNTER_SET(val) sb_set(&bs, val) #define COUNTER_POPCOUNT sb_popcount(bs) sb_t *bs = sb_alloc(0); sb_eval(&bs, cmax); #else #define COUNTER_SET(val) sm_uu32_insert(&m, val, 1) #define COUNTER_POPCOUNT sm_size(m) sm_t *m = sm_alloc(SM_U32U32, 0); #endif unsigned char buf[3 * 4 * 128]; int i; ssize_t l; for (;;) { l = read(0, buf, sizeof(buf)); l = (l / csize) * csize; if (l <= 0) break; #define CNTLOOP(inc, val) \ for (i = 0; i < l; i += inc) { \ COUNTER_SET(val); \ count++; \ if (COUNTER_POPCOUNT >= climit) \ goto done; \ } switch (csize) { case 1: CNTLOOP(1, buf[i]); break; case 2: CNTLOOP(2, (size_t)(buf[i] << 8 | buf[i + 1])); break; case 3: CNTLOOP(3, (size_t)(buf[i] << 16 | buf[i + 1] << 8 | buf[i + 2])); break; case 4: CNTLOOP(4, (size_t)buf[i] << 24 | (size_t)buf[i + 1] << 16 | (size_t)buf[i + 2] << 8 | (size_t)buf[i + 3]); break; default: goto done; } #undef CNTLOOP } done: printf(FMT_ZU ", " FMT_ZU, count, COUNTER_POPCOUNT); #ifdef COUNTER_USE_BITSET sb_free(&bs); #else sm_free(&m); #endif return exit_code; }