treenode* insert_key(char* s,int val,treenode* node) { int i; if(strlen(s)==1) { // reached last character if(node->size>0){ for(i=0;i<node->size;i++){ if(s[0]==node->next[i]->letter) { node->next[i]->val=val; return(node);} } } //either node size==0 or new key letter node = new_branch(node); node->next[node->size-1]=new_lasttreenode(s[0],val); return(node); } else { // not the last character if(node->size>0){ for(i=0;i<node->size;i++){ if(s[0]==node->next[i]->letter) { node->next[i] = insert_key(s+1,val,node->next[i]); return(node); } } } //either node size==0 or new key letter node = new_branch(node); node->next[node->size-1]=new_treenode(s[0]); node->next[node->size-1]=insert_key(s+1,val,node->next[node->size-1]); return(node); } }
void append_mtree(struct mtree *mt, u64 key, struct lump val) { struct block *parent = NULL; struct block *child; struct branch *branch; struct leaf *leaf; struct twig *twig; int i; eaver(val.size < 20); /* Find level that is not full */ for (i = 0; ; i++) { if (i == MAX_LEVELS) fatal("out of levels"); if (!mt->level[i]) { /* Grow tree */ if (i == 0) { child = new_leaf(mt); } else { u64 oldroot = mt->level[i - 1]; child = new_branch(mt, oldroot); branch = child->buf; twig = branch->twig; twig->key = 0; twig->blknum = oldroot; branch->num_twigs = 1; } mt->level[i] = child->blknum; break; } child = get_blk(mt->dev, mt->level[i]); if (!isFull(child, val.size)) break; put_blk(child); } /* Work our way back down the tree expanding it as we go */ for (;;) { if (isLeaf(child)) break; parent = child; branch = parent->buf; --i; if (i == 0) { child = new_leaf(mt); } else { child = new_branch(mt, mt->level[i - 1]); } twig = &branch->twig[branch->num_twigs++]; twig->key = key; twig->blknum = child->blknum; mt->level[i] = child->blknum; put_blk(parent); } leaf = child->buf; append_leaf(leaf, key, val); put_blk(child); return; }
bool split_branch (tree_s *tree, branch_s *parent, branch_s *child, s64 k) { branch_s *sibling; snint upper; snint midpoint; u32 upper_key; if (!branch_is_full(child)) return FALSE; FN; sibling = new_branch(tree); midpoint = child->br_num / 2; upper = midpoint + 1; sibling->br_first = child->br_key[midpoint].k_node; upper_key = child->br_key[midpoint].k_key; memmove(sibling->br_key, &child->br_key[upper], sizeof(key_s) * (child->br_num - upper)); sibling->br_num = child->br_num - upper; child->br_num = midpoint; insert_sibling(parent, sibling, k, upper_key); return TRUE; }
bool split_branch (void *self, branch_s *parent, int k) { branch_s *child = self; branch_s *sibling; int upper; int midpoint; u64 midpoint_key; FN; if (!branch_is_full(child)) return FALSE; sibling = new_branch(bdev(child)); midpoint = child->br_num / 2; upper = midpoint + 1; sibling->br_first = child->br_key[midpoint].k_block; midpoint_key = child->br_key[midpoint].k_key; memmove(sibling->br_key, &child->br_key[upper], sizeof(key_s) * (child->br_num - upper)); sibling->br_num = child->br_num - upper; child->br_num = midpoint; bdirty(child); bput(child); insert_sibling(parent, sibling, k, midpoint_key); bput(sibling); return TRUE; }
static BtNode_s *grow (BtNode_s *node) { FN; BtNode_s *branch = new_branch(); branch->first = node; return branch; }
Branch *make_tree() { Branch *root = new_branch(100, 1, 0); if (!root) return NULL; if (add_children(root, 1) != NO_ERROR) { free_tree(root); return NULL; } return root; }
static BtNode_s *split_branch (BtNode_s *parent, BtNode_s *node) { FN; BtNode_s *sibling = new_branch(); u64 key = node->twig[TWIGS_LOWER_HALF].key; sibling->first = node->twig[TWIGS_LOWER_HALF].node; memmove(sibling->twig, &node->twig[TWIGS_LOWER_HALF+1], sizeof(Twig_s) * (TWIGS_UPPER_HALF - 1)); node->num = TWIGS_LOWER_HALF; sibling->num = TWIGS_UPPER_HALF - 1; branch_insert(parent, key, sibling); return parent; }
void *grow_tree (tree_s *tree, unint len) { void *root; branch_s *branch; int rc; FN; if (root_blkno(tree)) { root = bget(tree->t_dev, root_blkno(tree)); if (!root) return NULL; switch (type(root)) { case LEAF: if (!leaf_is_full(root, len)) { return root; } break; case BRANCH: if (!branch_is_full(root)) { return root; } break; default: bput(root); error("Bad block"); exit(1); break; } branch = new_branch(tree->t_dev); branch->br_first = bblkno(root); bput(root); root = branch; } else { root = new_leaf(tree->t_dev); } rc = change_root(tree, root); if (rc) { error("Couldn't grow tree"); return NULL; } return root; }
static void *grow_tree (tree_s *tree, unint size) { head_s *head; branch_s *branch; int rc; FN; if (root(tree)) { head = tau_bget(tree_inode(tree), root(tree)); if (!head) return NULL; switch (head->h_magic) { case LEAF: if (!leaf_is_full((leaf_s *)head, size)) { return head; } break; case BRANCH: if (!branch_is_full((branch_s *)head)) { return head; } break; default: tau_bput(head); eprintk("Bad block"); BUG(); break; } branch = new_branch(tree); branch->br_first = tau_bnum(head); tau_bput(head); head = (head_s *)branch; } else { head = (head_s *)new_leaf(tree); } rc = change_root(tree, head); if (rc) { error("Couldn't grow tree"); return NULL; } return head; }
void *grow_tree (tree_s *tree, unint size) { void *node; branch_s *branch; int rc; FN; node = root(tree); if (node) { switch (magic(node)) { case LEAF: if (!leaf_is_full(node, size)) { return node; } break; case BRANCH: if (!branch_is_full(node)) { return node; } break; default: eprintf("Bad node"); break; } branch = new_branch(tree); branch->br_first = node; node = branch; } else { node = new_leaf(tree); } rc = change_root(tree, node); if (rc) { eprintf("Couldn't grow tree"); return NULL; } return node; }
static Error add_children(Branch *branch, float ttl) { if (ttl <= 0.0) return NO_ERROR; int num_children = 2 + rand() % (MAX_CHILDREN - 2); for (int i = 0; i < num_children; i++) { float life = ttl - (0.08 + 0.05 / (randf() + 0.001)); float length; float thickness; if (life < 0) { length = 10; thickness = 1; } else { length = branch->length * 0.7; thickness = branch->thickness * randf_uniform(0.6, 0.7); } float angle = (1.0 - 0.7 * life) * M_PI * randf_uniform(-0.5, 0.5); Branch *child = new_branch(length, thickness, branch->angle - angle); if (!child) return ERROR; branch->children[i] = child; branch->num_children++; if (add_children(child, life) != NO_ERROR) return ERROR; } return NO_ERROR; }
void SwitchboardServerConnection::sendInk(std::string image) { this->assertConnectionStateIsAtLeast(SB_READY); if(users.size() == 1) { p2p.sendInk(*this, image); // return; } std::string body("base64:"+image); bool one_packet = false; if(body.size() <= 1202) // if we need more than 1 packet, then use multipacket one_packet = true ; if(one_packet) { std::ostringstream buf_, msg_; msg_ << "MIME-Version: 1.0\r\n"; msg_ << "Content-Type: image/gif\r\n\r\n"; msg_ << body; size_t msg_length = msg_.str().size(); buf_ << "MSG " << this->trID++ << " N " << (int) msg_length << "\r\n" << msg_.str(); write(buf_); return; } else { std::istringstream body_stream(body); std::string messageid = new_branch(); std::vector<std::string> chunks; // spliting the message while(!body_stream.eof()) { char *part = new char[1203]; memset(part,0,1203); body_stream.read((char*)part, 1202); std::string part1(part); chunks.push_back(part1); delete [] part; } // sending the first one std::ostringstream buf_, msg_; msg_ << "MIME-Version: 1.0\r\n"; msg_ << "Content-Type: image/gif\r\n"; msg_ << "Message-ID: " << messageid << "\r\n"; msg_ << "Chunks: " << chunks.size() << "\r\n\r\n"; msg_ << chunks.front(); size_t msg_length = msg_.str().size(); buf_ << "MSG " << this->trID++ << " N " << (int) msg_length << "\r\n" << msg_.str(); write(buf_); std::vector<std::string>::iterator i = chunks.begin(); for(int num=0; i!=chunks.end(); i++, num++) { if(i == chunks.begin()) continue; std::ostringstream buf2_, msg2_; msg2_ << "Message-ID: " << messageid << "\r\n"; msg2_ << "Chunk: " << num << "\r\n\r\n"; msg2_ << (*i); size_t msg_length2 = msg2_.str().size(); buf2_ << "MSG " << this->trID++ << " N " << (int) msg_length2 << "\r\n" << msg2_.str(); write(buf2_); } } }