void ListInsert(List *a_list, int index, double a_value) { Iterator iter; iter.node = a_list->head; if (index > a_list->size) { index = a_list->size; } if (index == 0) { Node *new_node = NodeCreate(a_value); if (iter.node == NULL) { iter.node = new_node; a_list->head = iter.node; } else { new_node->next = iter.node; iter.node = new_node; a_list->head = iter.node; } a_list->size++; } else { Node *new_node = NodeCreate(a_value); int i; for (i = 0; i < index - 1 && iter.node->next != NULL; i++) { iter.node = iter.node->next; } new_node->next = iter.node->next; iter.node->next = new_node; a_list->size++; } }
/*-1 if first obj smaller, 0 if equal, 1 if 2nd obj is smaller*/ int SLInsert(SortedListPtr list, void *newObj){ if (newObj == NULL) return 0; /*Disallow null objects storage*/ ListNode curNode = list->begin; if (curNode == NULL) { ListNode lnNew = NodeCreate(newObj, list->begin); list->begin = lnNew; return 1; } else if ( list->cf(newObj, curNode->item) < 0) { /*New obj is greater than head node, becomes new head node*/ ListNode lnNew = NodeCreate(newObj, list->begin); if (lnNew == NULL) return 0; list->begin = lnNew; lnNew->next = curNode; /*Now predecessor points to new object's list node, and new list node points to the predecessor's previous successor*/ return 1; } else{ return SLInsert_T(list, list->begin, curNode, newObj); } }
/*SLInsert_T is a function that should only be called internally by SLInsert. it is used for the recursive cases of insertion.*/ static int SLInsert_T(SortedListPtr list, ListNode prevNode, ListNode curNode, void *newObj){ /*If traversed to node that doesn't exist, failure occurs*/ if (curNode == NULL) return 0; if ( list->cf(newObj, curNode->item) > 0) { /*If nowhere else to go, insert at end of list*/ if (curNode->next == NULL){ ListNode lnNew = NodeCreate(newObj, curNode->next); curNode->next = lnNew; return 1; } /*If new obj is less than current, keep iterating through*/ return SLInsert_T(list, curNode, curNode->next, newObj); } /*At this point, new obj is less than or equal current node's obj, insert before it*/ ListNode lnNew = NodeCreate(newObj, curNode->next); /*At this point, it is in list node and pointing to predecessor's sucessor*/ prevNode->next = lnNew; lnNew->next = curNode; /*Now predecessor points to new object's list node, and new list node points to the predecessor's previous successor*/ return 1; }
stream* GetStream(anynode *AnyNode, const tchar_t* URL, int Flags) { tchar_t Protocol[MAXPROTOCOL]; stream* Stream = NULL; fourcc_t FourCC; GetProtocol(URL,Protocol,TSIZEOF(Protocol),NULL); FourCC = NodeEnumClassStr(AnyNode,NULL,STREAM_CLASS,NODE_PROTOCOL,Protocol); #if defined(CONFIG_STREAM_CACHE) if ((Flags & (SFLAG_NO_CACHING|SFLAG_WRONLY|SFLAG_CREATE))==0) Stream = (stream*)NodeCreate(AnyNode,NodeClass_Meta(NodeContext_FindClass(AnyNode,FourCC),STREAM_CACHE_CLASS,META_PARAM_CUSTOM)); #endif if (!Stream) Stream = (stream*)NodeCreate(AnyNode,FourCC); if (Stream && (Flags & SFLAG_NON_BLOCKING)) Stream_Blocking(Stream,0); if (!Stream && !(Flags & SFLAG_SILENT)) { tcsupr(Protocol); NodeReportError(AnyNode,NULL,ERR_ID,ERR_PROTO_NOT_FOUND,Protocol); } #if defined(CONFIG_DEBUGCHECKS) if (Stream) tcscpy_s(Stream->URL,TSIZEOF(Stream->URL),URL); #endif return Stream; }
int SLInsert(SortedListPtr list, void *newObj, char*key) { if(list->head == NULL) { NodePtr newNode = NodeCreate(newObj, key); list->head = newNode; list->head->refcount += 1; return 1; } else if(list->head != NULL) { NodePtr cur = list->head; NodePtr prev = NULL; int comp = 0; comp = (int)strcmp(cur->name, key); while (cur->next!=NULL && comp!=0) { prev = cur; cur = cur->next; printf("Entered: %s\n", prev->name); if(cur->name==NULL){ comp = -1; break;} comp = (int)strcmp(cur->name, key); } if (comp==0) { int * y = (int*) malloc(sizeof(int)); *y = *((int*)cur->data) + 1; SLRemove(list, key); Insert(list, (void*) y , key); cur = list->head; return 1; } else{ NodePtr nu = NodeCreate(newObj, key); printf("cur: %s\n", cur->name); if(cur->name==NULL){ cur = NULL; prev->next = NULL;}; Insert(list, newObj, key); } } return 1; }
int main(int argc,char** argv) { node* p[10000]; int i; nodecontext Context; NodeContext_Init(&Context,NULL,NULL,NULL); for (i=0;i<10000;++i) p[i] = NodeCreate(&Context,NODE_CLASS); for (i=0;i<1000000;++i) NodeDelete(NodeCreate(&Context,NODE_CLASS)); for (i=0;i<10000;++i) NodeDelete(p[i]); NodeContext_Done(&Context); return 0; }
NOINLINE nodetree* NodeTree_CreateChild(void* p, const tchar_t* Name, fourcc_t ClassId, void* Before) { nodetree* Child = (nodetree*)NodeCreate(p,ClassId); if (Child) { NodeTree_SetParent(Child,p,Before); if (Name && Name[0]) Node_SetData((node*)Child,NODE_ID,TYPE_STRING,Name); } return Child; }
void ListAddNode(List *a_list, double a_value) //vstavka v konec { Node *node = NodeCreate(a_value); if (a_list->head == NULL) { a_list->head = node; } else { a_list->last->next = node; a_list->last = node; a_list->size++; } }
int SLInsert(SortedListPtr list, void *newObj){ double compareVal; void* currObj; SortedListIteratorPtr iterPtr = SLCreateIterator(list); if (iterPtr == NULL){ return 0; } node* prev = iterPtr->curr; if (iterPtr->curr == NULL || (*list->compF)(newObj, iterPtr->curr->obj) > 0){ //empty list/first object bigger list->head = NodeCreate(newObj,iterPtr->curr); // head insert SLDestroyIterator(iterPtr); return 1; } currObj=SLNextItem(iterPtr); while(iterPtr->curr != NULL){ compareVal = (*list->compF)(newObj, currObj); //compare curr/new objs if(compareVal >= 0){ //add here break; }else if(compareVal == 0){ //duplicate, ignore SLDestroyIterator(iterPtr); printf("duplicate item!!!\n"); return 1; }else{ //increment pointers prev = iterPtr->curr; currObj=SLNextItem(iterPtr); } } prev->next = NodeCreate(newObj,iterPtr->curr); //insert node if (prev->next==NULL) { printf("Malloc fail!\n"); return 0; } SLDestroyIterator(iterPtr); return 1; }
/* 插入节点,在第i个之前 */ void NodeInsert(LinkList List, int i, ElemType data) { struct Node *p = List; int j = 0; while (p->next && j < i - 1) { p = p->next; j++; } p->next = NodeCreate(data, p->next); }
int main(int argc, char* argv[]) { BtNode root = NodeCreate(1); BtNode node2 = NodeCreate(2); BtNode node3 = NodeCreate(3); BtNode node4 = NodeCreate(4); BtNode node5 = NodeCreate(5); BtNode node6 = NodeCreate(6); BtNode node7 = NodeCreate(7); BtNode node8 = NodeCreate(8); BtNode node9 = NodeCreate(9); root->left = node2; root->right = node3; node2->left = node4; node2->right = node5; node5->left = node7; node5->right = node8; node8->right = node9; node3->right = node6; if(IsBalancedTree(node5) == true) { printf("bal\n"); } else { printf("nonbal\n"); } return 0; }
/* inserts a node into its sorted position */ int Keylist_Data_Add( OS_Keylist list, KEY key, void *data) { struct Keylist_Node *node; /* holds the new node */ int index = -1; /* return value */ int i; /* counts through the array */ if (list && CheckArraySize(list)) { /* figure out where to put the new node */ if (list->count) { (void) FindIndex(list, key, &index); /* Add to the beginning of the list */ if (index < 0) index = 0; /* Add to the end of the list */ else if (index > list->count) index = list->count; /* Move all the items up to make room for the new one */ for (i = list->count; i > index; i--) { list->array[i] = list->array[i - 1]; } } else { index = 0; } /* create and add the node */ node = NodeCreate(); if (node) { list->count++; node->key = key; node->data = data; list->array[index] = node; } } return index; }
// Поиск в ширину (FIFO) int TreeSearch(){ int i,j; node *n = NodeCreate(start_i,start_j,NULL,0,NOTHING); array arr; queue *h = QueueCreate(); QueueAdd(h,n); while(!QueueIsEmpty(h)){ n = QueueGet(h); if(NodeIsFinish(n)){ while(n->parent != NULL){ NodePrint(n); field[n->state[0]][n->state[1]] = USED; n = n->parent; } field[n->state[0]][n->state[1]] = USED; printf("\nКарта пути:\n"); printf("\"X\" - Препятствие\n"); printf("\".\" - Свободная ячейка\n"); printf("\"*\" - Путь робота\n\n"); for(i = 0; i<SIZE; i++){ for(j = 0; j<SIZE; j++){ if(field[i][j] == FREE)printf("."); if(field[i][j] == OBSTACLE)printf("X"); if(field[i][j] == USED)printf("*"); } printf("\n"); } free(h); return 0; } //если решение еще не нашлось, то заново записываем переферию arr = Expand(n); for(i = 0; i < arr.size; i++){ QueueAdd(h, arr.arr[i]); } } printf("Can't find solution\n"); free(h); return 0; }
stream* StreamOpen(anynode *AnyNode, const tchar_t* Path, int Flags) { stream* File = GetStream(AnyNode,Path,Flags); if (File) { err_t Err = Stream_Open(File,Path,Flags); if (Err != ERR_NONE && Err != ERR_NEED_MORE_DATA) { NodeDelete((node*)File); File = NULL; } else { stream* Buf; if ((Flags & SFLAG_BUFFERED) && (Buf = (stream*)NodeCreate(AnyNode,BUFSTREAM_CLASS)) != NULL) { Node_SET(Buf,BUFSTREAM_STREAM,&File); File = Buf; } } } return File; }
// Функция Expand возращает множество узлов successor array Expand(node *n){ array successor; successor.size = 0; int i = 0; // Количество возможных прямолинейных шагов if(Include_field(n->state[0]+1, n->state[1]) && n->action != UP) // вниз if(field[n->state[0]+1][n->state[1]] != OBSTACLE)successor.size++; if(Include_field(n->state[0]-1, n->state[1]) && n->action != DOWN) // верх if(field[n->state[0]-1][n->state[1]] != OBSTACLE)successor.size++; if(Include_field(n->state[0], n->state[1]+1) && n->action != LEFT) // вправо if(field[n->state[0]][n->state[1]+1] != OBSTACLE)successor.size++; if(Include_field(n->state[0], n->state[1]-1) && n->action != RIGHT) // влево if(field[n->state[0]][n->state[1]-1] != OBSTACLE)successor.size++; // Количество возможных шагов по диагонали /* if(Include_field(n->state[0]+1, n->state[1]+1) && n->action != UP && n->action != LEFT) // вниз вправо if(field[n->state[0]+1][n->state[1]+1] != OBSTACLE)successor.size++; if(Include_field(n->state[0]-1, n->state[1]-1) && n->action != DOWN && n->action != RIGHT) // вверх влево if(field[n->state[0]-1][n->state[1]-1] != OBSTACLE)successor.size++; if(Include_field(n->state[0]-1, n->state[1]+1) && n->action != DOWN && n->action != LEFT) // вверх вправо if(field[n->state[0]-1][n->state[1]+1] != OBSTACLE)successor.size++; if(Include_field(n->state[0]+1, n->state[1]-1) && n->action != UP && n->action != RIGHT) // вниз влево if(field[n->state[0]+1][n->state[1]-1] != OBSTACLE)successor.size++; */ successor.arr = (node**)malloc(sizeof(node*)*successor.size); // временный массив для периферии // Добавляем возможные действия во временный массив if(Include_field(n->state[0]-1, n->state[1]) && n->action != DOWN) if(field[n->state[0]-1][n->state[1]] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]-1, n->state[1], n, n->depth+1, UP); i++; } if(Include_field(n->state[0]+1, n->state[1]) && n->action != UP) if(field[n->state[0]+1][n->state[1]] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]+1, n->state[1], n, n->depth+1, DOWN); i++; } if(Include_field(n->state[0], n->state[1]-1) && n->action != RIGHT) if(field[n->state[0]][n->state[1]-1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0], n->state[1]-1, n, n->depth+1, LEFT); i++; } if(Include_field(n->state[0], n->state[1]+1) && n->action != LEFT) if(field[n->state[0]][n->state[1]+1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0], n->state[1]+1, n, n->depth+1, RIGHT); i++; } /* if(Include_field(n->state[0]+1, n->state[1]+1) && n->action != UP && n->action != LEFT ) if(field[n->state[0]+1][n->state[1]+1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]+1, n->state[1]+1, n, n->depth+1, DOWN_RIGHT); i++; } if(Include_field(n->state[0]-1, n->state[1]-1) && n->action != RIGHT && n->action != DOWN ) if(field[n->state[0]-1][n->state[1]-1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]-1, n->state[1]-1, n, n->depth+1, UP_LEFT); i++; } if(Include_field(n->state[0]-1, n->state[1]+1) && n->action != LEFT && n->action != DOWN ) if(field[n->state[0]-1][n->state[1]+1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]-1, n->state[1]+1, n, n->depth+1, UP_RIGHT); i++; } if(Include_field(n->state[0]+1, n->state[1]-1) && n->action != RIGHT && n->action != UP ) if(field[n->state[0]+1][n->state[1]-1] != OBSTACLE){ successor.arr[i] = NodeCreate(n->state[0]+1, n->state[1]-1, n, n->depth+1, DOWN_LEFT); i++; }*/ return successor; }
std::pair<int, int> NuTo::Structure::InterfaceElementsCreate(int rElementGroupId, int rInterfaceInterpolationType, int rFibreInterpolationType) { // find element group boost::ptr_map<int, GroupBase>::iterator itGroupElements = mGroupMap.find(rElementGroupId); if (itGroupElements == mGroupMap.end()) throw Exception(__PRETTY_FUNCTION__, "Group with the given identifier does not exist."); if (itGroupElements->second->GetType() != NuTo::eGroupId::Elements) throw Exception(__PRETTY_FUNCTION__, "Group is not an element group."); // gets member ids from an element group. The element group must only contain truss elements auto elementIds = GroupGetMemberIds(rElementGroupId); int groupElementsInterface = GroupCreate(NuTo::eGroupId::Elements); int groupElementsFibre = GroupCreate(NuTo::eGroupId::Elements); // loop over elements in element group for (int elementId : elementIds) { auto nodeIds = ElementGetNodes(elementId); assert((nodeIds.size() == 2 or nodeIds.size() == 3) and "Only implemented for the 4 node and 6 node interface element"); std::vector<int> nodeIdsFibre(nodeIds.size()); std::vector<int> nodeIdsMatrix(nodeIds.size()); // loop over nodes of element for (unsigned int k = 0; k < nodeIds.size(); ++k) { Eigen::VectorXd nodeCoordinates; NodeGetCoordinates(nodeIds[k], nodeCoordinates); int groupNodes = GroupCreate(NuTo::eGroupId::Nodes); GroupAddNodeRadiusRange(groupNodes, nodeCoordinates, 0.0, 1e-6); // create an additional node at the same position if it has not been created already if (GroupGetNumMembers(groupNodes) > 1) { assert(GroupGetNumMembers(groupNodes) == 2 and "This group should have exactly two members. Check what went wrong!"); auto groupNodeMemberIds = GroupGetMemberIds(groupNodes); if (groupNodeMemberIds[0] == nodeIds[k]) { nodeIdsFibre[k] = groupNodeMemberIds[1]; } else { nodeIdsFibre[k] = groupNodeMemberIds[0]; } } else { std::set<NuTo::Node::eDof> dofs; dofs.insert(NuTo::Node::eDof::COORDINATES); dofs.insert(NuTo::Node::eDof::DISPLACEMENTS); nodeIdsFibre[k] = NodeCreate(nodeCoordinates, dofs); } nodeIdsMatrix[k] = nodeIds[k]; } // create interface element std::vector<int> nodeIndicesInterface(2 * nodeIds.size()); for (unsigned int iIndex = 0; iIndex < nodeIds.size(); ++iIndex) { nodeIndicesInterface[iIndex] = nodeIdsMatrix[iIndex]; nodeIndicesInterface[nodeIndicesInterface.size() - iIndex - 1] = nodeIdsFibre[iIndex]; } int newElementInterface = ElementCreate(rInterfaceInterpolationType, nodeIndicesInterface); GroupAddElement(groupElementsInterface, newElementInterface); // create new truss element with duplicated nodes int newElementFibre = ElementCreate(rFibreInterpolationType, nodeIdsFibre); GroupAddElement(groupElementsFibre, newElementFibre); // delete old element ElementDelete(elementId); } return std::make_pair(groupElementsFibre, groupElementsInterface); }
int StringInsert(SortedListPtr list, void *newObj, char*key){ if(list->head==NULL) { NodePtr newNode = NodeCreate(newObj, key); list->head = newNode; return 1; } else { NodePtr cur = (NodePtr)malloc(sizeof(Node)); cur = list->head; NodePtr prev = NULL; /*Move the cur and prev pointers until cur points to the node where the newObj should be inserted and prev points to the node before that point. */ CompareFuncT comp = list->compareTo; int aa = strcmp(key, cur->name); if(aa < 0) { NodePtr newNode = NodeCreate(newObj, key); newNode->next = cur; list->head = newNode; newNode->refcount += 1; return 1; } else { while((cur != NULL) && (list->compareTo(newObj,cur->data)) >= 0) { if(newObj == cur->data) { return 1; } prev = cur; cur = cur->next; } NodePtr newNode = NodeCreate(newObj, key); /*Insert*/ prev->next = newNode; newNode->refcount += 1; newNode->next = cur; return 1; } } }
/* 初始化链表 */ LinkList InitList() { struct Node *heading = NodeCreate(0, NULL); return (LinkList)heading; }
static void UpdatePage(settings* p) { datadef DataDef; node* Node; bool_t Found = 0; int Class = Context()->SettingsPage; int No,i; winunit y; bool_t CheckList; tchar_t Data[MAXDATA/sizeof(tchar_t)]; #ifndef REGISTRY_GLOBAL if (p->Win.Flags & WIN_PROP_CHANGED) { p->Win.Flags &= ~WIN_PROP_CHANGED; if (p->Current) NodeRegSave(p->Current); } #endif WinBeginUpdate(&p->Win); for (No=0;No<p->Count;++No) { if (p->Node[No]==Class) Found = 1; WinMenuCheck(&p->Win,p->Menu,SETTINGS_PAGES+No,p->Node[No]==Class); } if (!Found && p->Count>0) Class = p->Node[0]; NodeDelete(p->Current); p->Current = Node = NodeCreate(Class); if (Node) { WinTitle(&p->Win,LangStr(Class,NODE_NAME)); p->Win.LabelWidth = (Class == PLATFORM_ID) ? 60: (p->Win.ScreenWidth < 130) ? 80: (Class == PLAYER_ID || Class == ADVANCED_ID) ? 120:90; CheckList = Class == ASSOCIATION_ID; y = 2; // if the menu is in bottom of screen. print a hint before platform settings (first page shown) if (Class == PLATFORM_ID && (p->Win.Flags & WIN_BOTTOMTOOLBAR) && !(p->Win.Flags & WIN_2BUTTON)) { WinLabel(&p->Win,&y,-1,-1,LangStr(SETTINGS_ID,SETTINGS_HINT),PROPSIZE,0,NULL); y += 10; } for (No=0;NodeEnum(Node,No,&DataDef)==ERR_NONE;++No) if ((DataDef.Flags & DF_SETUP) && !(DataDef.Flags & DF_HIDDEN)) { if (DataDef.Flags & DF_GAP) y += 7; if (!(DataDef.Flags & DF_RDONLY)) WinPropValue(&p->Win,&y,Node,DataDef.No); else if (Node->Get(Node,DataDef.No,Data,DataDef.Size)==ERR_NONE) { switch (DataDef.Type) { case TYPE_LABEL: WinLabel(&p->Win,&y,-1,-1,DataDef.Name,PROPSIZE,0,NULL); break; case TYPE_TICK: TickToString(Data,TSIZEOF(Data),*(tick_t*)Data,0,1,0); WinPropLabel(&p->Win,&y,DataDef.Name,Data); break; case TYPE_INT: i = *(int*)Data; if (DataDef.Flags & DF_ENUMCLASS) tcscpy_s(Data,TSIZEOF(Data),LangStr(i,NODE_NAME)); else if (DataDef.Flags & DF_ENUMSTRING) tcscpy_s(Data,TSIZEOF(Data),LangStr(DataDef.Format1,i)); else Data[0] = 0; if (!Data[0]) IntToString(Data,TSIZEOF(Data),i,(DataDef.Flags & DF_HEX)!=0); if (DataDef.Flags & DF_KBYTE) tcscat_s(Data,TSIZEOF(Data),T(" KB")); if (DataDef.Flags & DF_MHZ) tcscat_s(Data,TSIZEOF(Data),T(" Mhz")); WinPropLabel(&p->Win,&y,DataDef.Name,Data); break; case TYPE_STRING: WinPropLabel(&p->Win,&y,DataDef.Name,Data); break; case TYPE_HOTKEY: HotKeyToString(Data,TSIZEOF(Data),*(int*)Data); WinPropLabel(&p->Win,&y,DataDef.Name,Data); break; case TYPE_BOOL: WinPropLabel(&p->Win,&y,DataDef.Name, LangStr(PLATFORM_ID,*(bool_t*)Data ? PLATFORM_YES:PLATFORM_NO)); break; default: WinPropLabel(&p->Win,&y,DataDef.Name,NULL); break; } } } } WinEndUpdate(&p->Win); }