static dbtype_t _bonsai_multi_delete(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t *value) { dbval_t *np = dbptr(ctx, node); dbtype_t min, left, right; int cmp; int i; if (!node.all) { value->type = Error; return DBNULL; } left = np->left; right = np->right; cmp = dbcmp(ctx, key, np->key); if (cmp < 0) { return balance(ctx, node, _bonsai_delete(ctx, left, key, value), right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, left, _bonsai_delete(ctx, right, key, value), subtree_right, 1); } if (np->nvalue == 1) { if (dbcmp(ctx, *value, np->values[0])!=0) { value->type = Error; return node; } if (!left.all) return right; if (!right.all) return left; right = delete_min(ctx, right, &min); rcuwinner(node, 0xe9); return balance(ctx, min, left, right, subtree_right, 0); } node = bonsai_copy(ctx, left, right, node); np = dbptr(ctx, node); for(i=0; i<np->nvalue; i++) { if (dbcmp(ctx, *value, np->values[i])==0) { memcpy(np->values+i, np->values+i+1, (np->nvalue-i-1)*sizeof(dbtype_t)); break; } } if (i == np->nvalue) { value->type = Error; } else { np->nvalue--; } return node; }
static dbtype_t _bonsai_delete(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t *valout) { dbval_t *np = dbptr(ctx, node); dbtype_t min, left, right; int cmp; if (!node.all) { if (valout) valout->type = Error; return DBNULL; } left = np->left; right = np->right; cmp = dbcmp(ctx, key, np->key); if (cmp < 0) { return balance(ctx, node, _bonsai_delete(ctx, left, key, valout), right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, left, _bonsai_delete(ctx, right, key, valout), subtree_right, 1); } if (valout) *valout = np->value; rcuwinner(node, 0xe9); if (!left.all) return right; if (!right.all) return left; right = delete_min(ctx, right, &min); return balance(ctx, min, left, right, subtree_right, 0); }
dbtype_t bonsai_insert(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t value, int insert_or_fail) { int cmp; dbval_t *np; if (!node.all) { return bonsai_new(ctx, DBNULL, DBNULL, key, value); } np = dbptr(ctx, node); cmp = dbcmp(ctx, key, np->key); if (insert_or_fail && cmp==0) { node.type = Error; return node; } if (cmp < 0) { return balance(ctx, node, bonsai_insert(ctx, np->left, key, value, insert_or_fail), np->right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, np->left, bonsai_insert(ctx, np->right, key, value, insert_or_fail), subtree_right, 1); } np->value = value; return node; }
dbtype_t bonsai_multi_insert(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t value) { int cmp; int i, j, done; dbval_t *np, *orig; if (!node.all) { return bonsai_multi_new(ctx, DBNULL, DBNULL, key, value); } np = dbptr(ctx, node); cmp = dbcmp(ctx, key, np->key); if (cmp < 0) { return balance(ctx, node, bonsai_multi_insert(ctx, np->left, key, value), np->right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, np->left, bonsai_multi_insert(ctx, np->right, key, value), subtree_right, 1); } orig = np; node = bonsai_ncopy(ctx, np->left, np->right, node, 1); np = dbptr(ctx, node); for(done=i=j=0; i<orig->nvalue; i++, j++) { if (!done) { cmp = dbcmp(ctx, value, orig->values[i]); if (cmp < 0) { np->values[j++] = value; np->nvalue++; done = 1; } else if (cmp == 0) { done = 1; } } np->values[j] = orig->values[i]; } if (!done) { np->values[j++] = value; np->nvalue++; } return node; }
int dblist_contains(pgctx_t *ctx, dbtype_t list, dbtype_t item) { _list_t *_list; int i; list.ptr = dbptr(ctx, list); assert(list.ptr->type == List); _list = dbptr(ctx, list.ptr->list); if (_list) { for(i=0; i<_list->len; i++) { if (dbcmp(ctx, _list->item[i], item) == 0) return 1; } } return 0; }
int bonsai_find(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t *value) { int cmp; while(node.all) { node.ptr = dbptr(ctx, node); cmp = dbcmp(ctx, key, node.ptr->key); if (cmp < 0) { node = node.ptr->left; } else if (cmp > 0) { node = node.ptr->right; } else { if (value) *value = node.ptr->value; return 0; } } return -1; }
dbtype_t bonsai_find_node(pgctx_t *ctx, dbtype_t node, dbtype_t key) { int cmp; dbtype_t ret; while(node.all) { ret = node; node.ptr = dbptr(ctx, node); cmp = dbcmp(ctx, key, node.ptr->key); if (cmp < 0) { node = node.ptr->left; } else if (cmp > 0) { node = node.ptr->right; } else { return ret; } } return DBNULL; }
int dblist_remove(pgctx_t *ctx, dbtype_t list, dbtype_t item, int sync) { _list_t *_list, *_newlist = NULL; int sz, i, j, remove; list.ptr = dbptr(ctx, list); assert(list.ptr->type == List); // Read-Copy-Update loop for modify operations do { _list = dbptr(ctx, list.ptr->list); if (!_list) return -1; sz = dblist_size(_list, 0); dbfree(_newlist, 0xf9); _newlist = dballoc(ctx, sz); _newlist->type = _InternalList; _newlist->len = _list->len - 1; remove = 0; for(i=j=0; i<_list->len; i++) { // Copy all items that don't match. Only remove // one matching item if (!remove && dbcmp(ctx, _list->item[i], item) == 0) { remove = 1; } else { _newlist->item[j++] = _list->item[i]; } } if (!remove) { dbfree(_newlist, 0xfa); // No item was removed, so discard the copy (no one will // ever see it) and return an error return -1; } } while(!synchronizep(ctx, sync, &list.ptr->list, _list, _newlist)); dbfree(_list, 0xfb); return 0; }
static PyObject * PongoIter_next_expr(PyObject *ob) { PongoIter *self = (PongoIter*)ob; dbval_t *internal, *pn; dbtype_t node, key, val; PyObject *ret=NULL, *k, *v; int ls, rs; _list_t *list; _obj_t *obj; dblock(self->ctx); internal = dbptr(self->ctx, self->dbptr); if (!internal) { PyErr_SetNone(PyExc_StopIteration); } else if (self->tag == List) { list = (_list_t*)internal; for(;;) { if (self->pos == self->len) { PyErr_SetNone(PyExc_StopIteration); break; } node = list->item[self->pos++]; if ((!self->lhdata.all || dbcmp(self->ctx, node, self->lhdata) >= self->lhex) && (!self->rhdata.all || dbcmp(self->ctx, node, self->rhdata) <= self->rhex)) { ret = to_python(self->ctx, node, TP_PROXY); break; } } } else if (self->tag == Object) { obj = (_obj_t*)internal; for(;;) { // If we've reached the end, quit with StopIteration if (self->pos == self->len) { PyErr_SetNone(PyExc_StopIteration); break; } key = obj->item[self->pos].key; val = obj->item[self->pos].value; self->pos++; // If the key doesn't satisfy the RHS, and since Objects are // sorted, we can quit with StopIteration if (!(!self->rhdata.all || dbcmp(self->ctx, key, self->rhdata) <= self->rhex)) { PyErr_SetNone(PyExc_StopIteration); break; } // If the key does satisfy the LHS, return it if (!self->lhdata.all || dbcmp(self->ctx, key, self->lhdata) >= self->lhex) { k = to_python(self->ctx, key, TP_PROXY); v = to_python(self->ctx, val, TP_PROXY); ret = PyTuple_Pack(2, k, v); break; } } } else if (self->tag == Collection || self->tag == MultiCollection) { if (self->pos && self->depth == -1) { PyErr_SetNone(PyExc_StopIteration); } else { // NOTE: I'm overloading the lowest bit to mean "already traversed the left // side". Normally, this bit is part of the 'type' field and would encode // this value as "Int". However, the BonsaiNode left/right can only point // to other BonsaiNodes and the stack (where the bit overloading is happening) // lives only in main memory, so we'll never write this bit modification // to disk. // If were at position 0, put the internal node onto the stack // I'm reusing pos as a flag since pos is otherwise unused by the tree iterator if (self->pos == 0) { node = self->dbptr; for(;;) { pn = _ptr(self->ctx, node.all); ls = (!self->lhdata.all || dbcmp(self->ctx, pn->key, self->lhdata) >= self->lhex); rs = (!self->rhdata.all || dbcmp(self->ctx, pn->key, self->rhdata) <= self->rhex); if (ls && rs) { self->stack[++self->depth] = node; self->pos = 1; break; } else if (ls && pn->left.all) { node = pn->left; } else if (rs && pn->right.all) { node = pn->right; } else { PyErr_SetNone(PyExc_StopIteration); goto exitproc; } } } // Get current top of stack. If we've already traversed the left side // of this node, go directly to the emit stage and traverse the right side. node = self->stack[self->depth]; pn = _ptr(self->ctx, node.all & ~1); if (node.all & 1) { node.all &= ~1; } else { // Walk as far left as possible, pushing onto stack as we // follow each link if (pn->left.all) { node = pn->left; for(;;) { pn = _ptr(self->ctx, node.all); ls = (!self->lhdata.all || dbcmp(self->ctx, pn->key, self->lhdata) >= self->lhex); rs = (!self->rhdata.all || dbcmp(self->ctx, pn->key, self->rhdata) <= self->rhex); if (ls && rs) { self->stack[++self->depth] = node; } if (ls && pn->left.all) { node = pn->left; } else if (rs && pn->right.all) { node = pn->right; } else { break; } } // Reset node and pn to whatever is on the top of stack now node = self->stack[self->depth]; pn = _ptr(self->ctx, node.all); } } // Now node, pn and top of stack all reference the same object, // so convert the object to python, pop the top of stack and // mark the new top as "left side traversed" ret = to_python(self->ctx, node, TP_NODEKEY|TP_NODEVAL|TP_PROXY); if (--self->depth >= 0) { self->stack[self->depth].all |= 1; } // Now check if there is a right branch in the tree and push // it for the next call to the iterator if (pn->right.all) { node = pn->right; for(;;) { pn = _ptr(self->ctx, node.all); ls = (!self->lhdata.all || dbcmp(self->ctx, pn->key, self->lhdata) >= self->lhex); rs = (!self->rhdata.all || dbcmp(self->ctx, pn->key, self->rhdata) <= self->rhex); if (ls && rs) { self->stack[++self->depth] = node; } if (ls && pn->left.all) { node = pn->left; } else if (rs && pn->right.all) { node = pn->right; } else { break; } } } } } exitproc: dbunlock(self->ctx); return ret; }
BOOL CLDlg::OnInitDialog() { CDialog::OnInitDialog(); m_hYellowBrush=(HBRUSH)GetStockObject(BLACK_BRUSH); m_hRedBrush=(HBRUSH)GetStockObject(BLACK_BRUSH); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here ///////////////////////// initializations /////////////////////////////////// pt1[0].x=0; pt1[0].y=0; pt2[0].x=1; pt2[0].y=1; pt1[1].x=0; pt1[1].y=3; pt2[1].x=0; pt2[1].y=3; pt1[2].x=8; pt1[2].y=7; pt2[2].x=9; pt2[2].y=9; pt1[3].x=8; pt1[3].y=5; pt2[3].x=9; pt2[3].y=6; pt1[4].x=4; pt1[4].y=4; pt2[4].x=5; pt2[4].y=5; cmpt[0].x=0; cmpt[0].y=0; cmpt[1].x=0; cmpt[1].y=4; cmpt[2].x=5; cmpt[2].y=5; cmpt[3].x=5; cmpt[3].y=7; cmpt[4].x=8; cmpt[4].y=8; ///////////////////////// initializations /////////////////////////////////// ///////////////////////////////database read from floortab///////////////////////////////////////////// CDatabase xx; xx.Open("ODBC;DSN=geoinfo"); CRecordset dbSQL(&xx); dbSQL.Open(CRecordset::forwardOnly,"select * from floortab"); cnt=0; while(!dbSQL.IsEOF()) { CString xy; dbSQL.GetFieldValue("fid",xy); flid[cnt]=atoi(xy); dbSQL.GetFieldValue("frows",xy); flrows[cnt]=atoi(xy); dbSQL.GetFieldValue("fclmns",xy); flclmns[cnt++]=atoi(xy); dbSQL.MoveNext(); } ///////////////////////////////database read from floortab////////////////////////////////////////////// ///////////////////////////////database read from labtab////////////////////////////////////////////// CRecordset dblab(&xx); dblab.Open(CRecordset::forwardOnly,"select * from labtab"); labcount=0; while(!dblab.IsEOF()) { CString xy; dblab.GetFieldValue("labid",xy); labid[labcount]=atoi(xy); dblab.GetFieldValue("fid",xy); labfl[labcount]=atoi(xy); dblab.GetFieldValue("labname",xy); strcpy(labname[labcount],xy); dblab.GetFieldValue("labrow1",xy); pt1[labcount].x=atoi(xy); dblab.GetFieldValue("labclmn1",xy); pt1[labcount].y=atoi(xy); dblab.GetFieldValue("labrow2",xy); pt2[labcount].x=atoi(xy); dblab.GetFieldValue("labclmn2",xy); pt2[labcount].y=atoi(xy); dblab.GetFieldValue("labrows",xy); labrows[labcount]=atoi(xy); dblab.GetFieldValue("labclmns",xy); labclmns[labcount++]=atoi(xy); dblab.MoveNext(); } ///////////////////////////////database read from labtab////////////////////////////////////////////// ///////////////////////////////database read from labtab////////////////////////////////////////////// CRecordset dbcmp(&xx); dbcmp.Open(CRecordset::forwardOnly,"select * from comptab"); cmcount=0; while(!dbcmp.IsEOF()) { CString xy; dbcmp.GetFieldValue("cmid",xy); cmid[cmcount]=atoi(xy); dbcmp.GetFieldValue("labid",xy); cmlabid[cmcount]=atoi(xy); dbcmp.GetFieldValue("ptx",xy); cmpt[cmcount].x =atoi(xy); dbcmp.GetFieldValue("pty",xy); cmpt[cmcount].y =atoi(xy); dbcmp.GetFieldValue("cmip1",xy); cmip[cmcount][0] =atoi(xy); dbcmp.GetFieldValue("cmip2",xy); cmip[cmcount][1] =atoi(xy); dbcmp.GetFieldValue("cmip3",xy); cmip[cmcount][2] =atoi(xy); dbcmp.GetFieldValue("cmip4",xy); cmip[cmcount][3] =atoi(xy); cmcount++; dbcmp.MoveNext(); } ///////////////////////////////database read from labtab////////////////////////////////////////////// return TRUE; // return TRUE unless you set the focus to a control }