void print_tree(bintree* tree) { unsigned int height = get_depth(tree); node* root = tree->root; node *arr[100], *arr2[100]; unsigned int nodes; for (int i = 0; i < height * 2; i++) printf(" "); printf("%2d\n", root->data); arr2[0] = root; nodes = 1; int not_null_flag = 1; for(int depth = 2;not_null_flag; depth++) { not_null_flag = 0; for (int i = 0; i < nodes; i++) { arr[i*2] = arr2[i] ? arr2[i]->lchild : NULL; arr[i*2 + 1] = arr2[i] ? arr2[i]->rchild : NULL; if (arr[i*2] || arr[i*2 + 1]) not_null_flag = 1; } nodes *= 2; memcpy(arr2, arr, nodes * sizeof(node*)); if (not_null_flag) { print_slice(arr2, nodes, height, depth); } } }
int main( int argc, char **argv ) { double arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; struct arr_slice *s = NULL; struct arr_slice *a = NULL; s = Slice.wrap_array( 10, sizeof(double), arr ); /* Prints '5.0' */ a = Slice.go( s, "[4]" ); print_slice( a ); Slice.clean( a ); /* Prints '1.0, 2.0' */ a = Slice.go( s, "[0:2]" ); print_slice( a ); Slice.clean( a ); /* Prints '3.0, 4.0' */ a = Slice.go( s, "[2:4]" ); print_slice( a ); Slice.clean( a ); /* Prints '1.0, 2.0' */ a = Slice.go( s, "[:2]" ); print_slice( a ); Slice.clean( a ); /* Prints '3.0-10.0' */ a = Slice.go( s, "[2:]" ); print_slice( a ); Slice.clean( a ); /* Prints '10.0' */ a = Slice.go( s, "[-1]" ); print_slice( a ); Slice.clean( a ); /* Prints '2.0-10.0' */ a = Slice.go( s, "[1:100]" ); print_slice( a ); Slice.clean( a ); /* Prints '' */ a = Slice.go( s, "[10:]" ); print_slice( a ); Slice.clean( a ); /* Prints '' */ a = Slice.go( s, "[2:1]" ); print_slice( a ); Slice.clean( a ); /* Prints '9.0' */ a = Slice.go( s, "[-2]" ); print_slice( a ); Slice.clean( a ); /* Prints '9.0, 10.0' */ a = Slice.go( s, "[-2:]" ); print_slice( a ); Slice.clean( a ); /* Prints '1.0-8.0' */ a = Slice.go( s, "[:-2]" ); print_slice( a ); Slice.clean( a ); /* Prints '1.0' */ a = Slice.go( s, "[-0]" ); print_slice( a ); Slice.clean( a ); return 0; }
int main(int argc, char *argv[]) { struct stat st; void *filemap = NULL; #ifdef __linux__ unsigned char *pages = NULL; #else char *pages = NULL; #endif size_t lip; /* lip: length in pages */ unsigned long pim, /* pim: pages in memory counter */ k; unsigned long spim; /* spim: pages in memory "in slices" counter */ long int pagesize, slice_start, slice_end; int i, fd, in_a_slice, sindex; pagesize = sysconf(_SC_PAGESIZE); if (pagesize == -1) error(1, errno, "Unable to get pagesize"); else printf("Pagesize is: %ld bytes.\n", pagesize); for (i = 1; i < argc; i++) { fd = open(argv[i], O_RDONLY); if (fd != -1) { if (fstat(fd, &st) == -1) { warning(errno, "Unable to stat '%s'", argv[i]); continue; } filemap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); if (filemap == MAP_FAILED) { warning(errno, "Unable to map '%s'", argv[i]); continue; } lip = (st.st_size + pagesize - 1) / pagesize; pages = malloc(lip); if (pages == NULL) error(2, errno, "Unable to allocate pages[%lu]", (unsigned long) lip); if (mincore(filemap, st.st_size, pages) == -1) { warning(errno, "Unable to get core info for '%s'", argv[i]); continue; } pim = spim = 0; sindex = slice_start = slice_end = in_a_slice = 0; printf("'%s':\n", argv[i]); for (k = 0; k < lip; k++) { if (pages[k] & 1) { if (!in_a_slice) { in_a_slice = 1; slice_start = pagesize * k; slice_end = slice_start + pagesize - 1; } else slice_end += pagesize; pim++; } else if (in_a_slice) { in_a_slice = 0; print_slice(sindex, pagesize, slice_start, slice_end, &spim); sindex++; } } if (in_a_slice) { /* last page of the file in pagecache */ print_slice(sindex, pagesize, slice_start, slice_end, &spim); } if (spim) printf("\t%lu pages in slices appear to be in " "pagecache\n", spim); printf("\t%lu pages out of %lu appear to be in " "pagecache\n", pim, (unsigned long) lip); free(pages); pages = NULL; if (munmap(filemap, st.st_size) == -1) { warning(errno, "Unable to unmap '%s'", argv[i]); continue; } if (close(fd) == -1) warning(errno, "Problem closing '%s'", argv[i]); } else warning(errno, "Unable to open '%s'", argv[i]); } return (0); }