struct node *Add2(struct node *p1, struct node *p2, int carry, int state) { if(p1 == NULL && p2 == NULL) return NULL; struct node *ret = NULL; push(&ret, 0); if(state > 0) { ret->data = carry + p1->data; carry = ret->data / 10; ret->data %= 10; ret->next = Add2(p1->next, p2, carry, state-1); } else if(state < 0) { ret->data = carry + p2->data; carry = ret->data / 10; ret->data %= 10; ret->next = Add2(p1, p2->next, carry, state+1); } else { ret->data = carry + p1->data + p2->data; carry = ret->data / 10; ret->data %= 10; ret->next = Add2(p1->next, p2->next, carry, 0); } printf("%s data[%d]\n", __FUNCTION__, ret->data); return ret; }
int ThreadCreation() { const unsigned long num_threads = 64; static HANDLE aThreads[64] = { 0 }; unsigned long slot = 0; unsigned long thread_id = 0; unsigned long cnt_th = 0; unsigned long thread_ret = 0; fprintf(stderr, "creating %d threads \n", num_threads); for (cnt_th = 0; cnt_th < num_threads; cnt_th++) { aThreads[cnt_th] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadRoutine,0,0,(LPDWORD)&thread_id); } fprintf(stderr, "created %d threads \n", num_threads); while (cnt_th > 0) { slot = WaitForMultipleObjects(cnt_th, aThreads, FALSE, INFINITE); GetExitCodeThread(aThreads[slot],&thread_ret); CloseHandle(aThreads[slot]); aThreads[slot] = aThreads[cnt_th-1]; cnt_th--; fprintf(stderr, "addit =%d\n", Add2(cnt_th, cnt_th)); } fprintf(stderr, "all %d threads terminated\n", num_threads); fflush(stderr); return 1; }
// Recursive approach int Add2(int x, int y) { if (y == 0) return x; else return Add2( x ^ y, (x & y) << 1); }
int main(int argc, char *argv[]) { int numthreads = 0; int i; pthread_t threads[MAXTHREADS]; #if defined(i386) asm("pusha;popa"); #endif numthreads = 20; assert(numthreads < MAXTHREADS); for (threads_started = 0; threads_started < numthreads; threads_started++) { printf("Creating thread\n"); fflush(stdout); pthread_create(threads+threads_started, 0, hello, 0); fflush(stdout); } for (i = 0; i < numthreads; i++) { pthread_join(threads[i], 0); printf("Joined %d, sum=%d\n", i, Add2(i,i)); } printf("All threads joined\n"); return 0; }
int main() { printf("%d \n", Add1(15, 32)); printf("%d \n", Add2(15, 32)); printf("%d \n", Add3(15, 32)); return 0; }
//------------------------------------------------------------------------------ void CUIEdProjectUI::AddToElement(CUIEdAttrLinkData* link,CUIEdAttrLinkData* parent,CUIEdAttrLinkData* sibling) { int i; CIwAttrInstance* inst2=link->m_Element; if (link->m_Type==LAYOUTITEM_PROPERTYSET) { CIwAttrData* data=parent->m_Element->FindData(link->m_Element->m_Parent->m_Member->m_Name); if (data==NULL) return; if (sibling==NULL) { i=data->m_Items.size(); data->m_Items.resize(i+1); } else { for (i=0; i<(int)data->m_Items.size(); i++) { if (data->m_Items[i].m_Inst==sibling->m_Element) { data->m_Items.insert(data->m_Items.begin()+i,CIwAttrMemberItem()); break; } } if (i==(int)data->m_Items.size()) data->m_Items.resize(i+1); } data->m_Items[i].m_Inst=inst2; data->m_Items[i].m_Flags=ATTRITEM_ALLOCED_F; inst2->m_Parent=data; if (data->m_Member->m_Type&ATTRMEMBER_LIST) link->m_Name=data->m_Member->m_Name+wxString::Format(L"[%d]",i); else link->m_Name=data->m_Member->m_Name; } else if (sibling==NULL) Add2(inst2,parent->m_Element); else Add2(inst2,parent->m_Element,sibling->m_Element); SetupElements(inst2,parent); }
void test() { Assign(); Add1(); Add2(); GetBuffer(); Compare(); Mid(); }
void CALLBACK Add2( __in HRESULT hr, __in WS_CALLBACK_MODEL callbackModel, __in void* state) { ADD_STATE* addState = (ADD_STATE*)state; // Mark the operation as being async addState->async = TRUE; Add2(hr, callbackModel, addState); }
struct node *Add(struct node *head1, struct node *head2) { int state = FindLength(head1) - FindLength(head2); struct node *sum = NULL; int carry = 0; sum = Add2(head1, head2, carry, state); return sum; }
int main() { Add1(1,2); Add2(1,2); int(*f)(int,int); int n; cin >> n; if(n==0) f = &Add1; else f = &Add2; f(1,2); }
HRESULT CALLBACK Add1( __in HRESULT hr, __in WS_CALLBACK_MODEL callbackModel, __in ADD_STATE* addState) { if (SUCCEEDED(hr)) { // Add the third value, and continue at Add2 WS_ASYNC_CONTEXT add2; add2.callback = Add2; add2.callbackState = addState; hr = Add(addState->sum, addState->c, &addState->sum, (addState->asyncContext.callback != NULL ? &add2 : NULL), addState->error); if (hr == WS_S_ASYNC) { // Add2 will get called asynchronously return hr; } } // Operation completed synchronously, so no callback will occur, so call directly return Add2(hr, callbackModel, addState); }
//------------------------------------------------------------------------------ CIwAttrInstance* CUIEdProjectUI::Move(CIwAttrInstance* inst,CIwAttrInstance* parent,int actionModifer) { int i; CUIEdAttrShared& shared=CIwTheHost.m_Shared; CUIEdAttrLinkData* link=GetLink(inst); CUIEdAttrLinkData* parentLink=GetLink(parent); CUIEdAttrLinkData* sibling=NULL; CUIEdAttrLinkData* layout=NULL; CUIEdAttrLinkData* layoutSibling=NULL; if (link==NULL) return NULL; if (link->m_Type==LAYOUTITEM_PROPERTYSET) if (parentLink==NULL || link->m_Element->m_Parent==NULL || parentLink->m_Element->m_Class->GetMember(link->m_Element->m_Parent->m_Member->m_Name)==NULL) return NULL; if (link->m_Parent!=NULL) { for (i=0; i<(int)link->m_Parent->m_Children.size(); i++) { if (link->m_Parent->m_Children[i]==link) { link->m_Parent->m_Children.erase(link->m_Parent->m_Children.begin()+i); break; } } if (link->m_LayoutItem!=NULL && link->m_Element->m_Parent!=NULL && link->m_Element->m_Parent->m_Instance==link->m_LayoutItem) shared.Remove(link->m_LayoutItem); else { if (link->m_LayoutItem!=NULL) shared.Remove(link->m_LayoutItem); shared.Remove(link->m_Element); } } if (actionModifer&ACTIONMODIFIER_INSERT) { sibling=parentLink; parentLink=parentLink->m_Parent; } if (link->m_Type==LAYOUTITEM_FOCUS) { if (parentLink->m_Type!=LAYOUTITEM_ELEMENT) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT); if (parentLink==NULL) return NULL; layout=parentLink->GetFocus(); if (layout!=NULL) Delete(layout->m_Element); MoveLink(link,parentLink,sibling); AddToElement(link,parentLink,sibling); return link->m_Element; } if (link->m_Type==LAYOUTITEM_PROPERTYSET) { if (parentLink->m_Type!=LAYOUTITEM_ELEMENT) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT,&sibling); if (parentLink==NULL) return NULL; MoveLink(link,parentLink,sibling); AddToElement(link,parentLink,sibling); return link->m_Element; } if (parentLink->m_Type==LAYOUTITEM_FOCUS || parentLink->m_Type==LAYOUTITEM_PROPERTYSET) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT,&sibling); if (parentLink->m_Type==LAYOUTITEM_LAYOUTITEM) parentLink=parentLink->GetParent(LAYOUTITEM_LAYOUT,&sibling); if (link->m_Type==LAYOUTITEM_ELEMENT) { if (parentLink->m_Type==LAYOUTITEM_LAYOUT) { layout=parentLink; layoutSibling=sibling; parentLink=layout->GetParent(LAYOUTITEM_ELEMENT,&sibling); } else if (parentLink->m_Type==LAYOUTITEM_ELEMENT) layout=parentLink->GetLayout(); if (layout!=NULL) MoveLink(link,layout,layoutSibling); else MoveLink(link,parentLink,sibling); AddToElement(link,parentLink,sibling); if (layout!=NULL) { if (link->m_LayoutItem==NULL) AddToLayout(link,layout,layoutSibling); else Add2(link->m_LayoutItem,layout->m_Element,layoutSibling!=NULL ? layoutSibling->m_Element : NULL); } return link->m_Element; } if (link->m_Type==LAYOUTITEM_LAYOUT) { if (parentLink->m_Type==LAYOUTITEM_LAYOUT) { layout=parentLink; layoutSibling=sibling; parentLink=layout->GetParent(LAYOUTITEM_ELEMENT,&sibling); } else if (parentLink->m_Type==LAYOUTITEM_ELEMENT) { layout=parentLink->GetLayout(); if (layout==NULL) { layout=parentLink; layoutSibling=sibling; } } if (layout==NULL) return NULL; MoveLink(link,layout,layoutSibling); if (layout->m_Type==LAYOUTITEM_LAYOUT) { if (link->m_LayoutItem==NULL) { AddToLayout(link,layout,layoutSibling); Add2(link->m_Element,link->m_LayoutItem); } else Add2(link->m_LayoutItem,layout->m_Element,layoutSibling!=NULL ? layoutSibling->m_Element : NULL); } else AddToElement(link,parentLink,sibling); for (i=0; i<(int)link->m_Children.size(); i++) { if (link->m_Children[i]->m_Type==LAYOUTITEM_ELEMENT) { shared.Remove(link->m_Children[i]->m_Element); AddToElement(link->m_Children[i],parentLink,sibling); } } return link->m_Element; } if (link->m_Type==LAYOUTITEM_LAYOUTITEM) { if (parentLink->m_Type==LAYOUTITEM_ELEMENT) { if (parentLink->GetLayout()==NULL) parentLink=parentLink->GetParent(LAYOUTITEM_LAYOUT,&sibling); else { parentLink=parentLink->GetLayout(); sibling=NULL; } } if (parentLink==NULL) return NULL; MoveLink(link,parentLink,sibling); AddToLayout(link,parentLink,sibling); Add2(link->m_Element,link->m_LayoutItem); return link->m_Element; } return inst; }
//------------------------------------------------------------------------------ void CUIEdProjectUI::AddToLayout(CUIEdAttrLinkData* link,CUIEdAttrLinkData* parent,CUIEdAttrLinkData* sibling) { link->m_LayoutItem=parent->m_Element->FindChild(link->m_Name); if (link->m_LayoutItem!=NULL) { link->m_LayoutItem->m_TempClass=(CIwAttrClass*)link; return; } link->m_LayoutItem=MakeContainer(link->m_Name); if (link->m_LayoutItem==NULL) return; link->m_LayoutItem->m_TempClass=(CIwAttrClass*)link; int i; int row=-1,col=-1; CIwAttrData* data; for (CIwAttrClass* klass=parent->m_Element->m_Class; klass!=NULL; klass=klass->m_Parent) { if (klass->m_Name.IsSameAs(L"CIwUILayoutGrid",false)) { int numcols=0,numrows=0; std::vector<CIwAttrData*> elems; for (i=0; i<(int)parent->m_Element->m_Data.size(); i++) { if (parent->m_Element->m_Data[i]->m_Member->m_Name.IsSameAs(L"element",false)) elems.push_back(parent->m_Element->m_Data[i]); if (parent->m_Element->m_Data[i]->m_Member->m_Name.IsSameAs(L"row",false)) numrows++; if (parent->m_Element->m_Data[i]->m_Member->m_Name.IsSameAs(L"column",false)) numcols++; } CIwAttrData* colNum=parent->m_Element->FindData(L"numColumns"); CIwAttrData* rowNum=parent->m_Element->FindData(L"numRows"); if (numcols==0 && colNum!=NULL) numcols=colNum->m_Items[0].m_Int; if (numcols==0) numcols=1; if (numrows==0 && rowNum!=NULL) numrows=rowNum->m_Items[0].m_Int; if (numrows==0) numrows=1; for (int num=0; num<numrows*numcols; num++) { row=num/numcols; col=num%numcols; for (i=0; i<(int)elems.size(); i++) { CIwAttrData* rowV=elems[i]->m_Items[0].m_Inst->FindData(L"row"); CIwAttrData* colV=elems[i]->m_Items[0].m_Inst->FindData(L"column"); if (rowV!=NULL && colV!=NULL) if (rowV->m_Items[0].m_Int==row && colV->m_Items[0].m_Int==col) break; } if (i==(int)elems.size()) break; row=col=-1; } break; } } if (sibling==NULL) Add2(link->m_LayoutItem,parent->m_Element); else Add2(link->m_LayoutItem,parent->m_Element,sibling->m_Element); if (row!=-1) { data=link->m_LayoutItem->FindData(L"row"); if (data!=NULL) data->m_Items[0].m_Int=row; } if (col!=-1) { data=link->m_LayoutItem->FindData(L"column"); if (data!=NULL) data->m_Items[0].m_Int=col; } }
void MIDITOR::ChannelAfterTouch(SysU8 p,SysF32 t) { if(t<0) t=Time-Bias; Add2(MIDIChannelAfterTouch|Channel,p,t); }
void MIDITOR::ProgChange(SysU8 p,SysF32 t) { if(t<0) t=Time-Bias; Add2(MIDIProgChange|Channel,p,t); }
int main() { int n1 = Add1(1,2); int n2 = Add2(1,2); }
//------------------------------------------------------------------------------ CIwAttrInstance* CUIEdProjectUI::Add(CIwAttrInstance* inst,CIwAttrInstance* parent,int actionModifer) { if (inst==NULL || parent==NULL) return NULL; CUIEdAttrLinkData* link=GetLink(inst); if (link==NULL) return NULL; if ((actionModifer&ACTIONMODIFIER_TAKEALL)==0) link=ResolveAddItem(link); CUIEdAttrLinkData* sibling=NULL; CUIEdAttrLinkData* parentLink=GetLink(parent); CUIEdAttrLinkData* layout=NULL; CUIEdAttrLinkData* layoutSibling=NULL; if (parentLink==NULL) return NULL; if ((actionModifer&ACTIONMODIFIER_INSERT) && parentLink->m_Parent!=NULL) { sibling=parentLink; parentLink=parentLink->m_Parent; } if (link->m_Type==LAYOUTITEM_FOCUS) { if (parentLink->m_Type!=LAYOUTITEM_ELEMENT) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT); if (parentLink==NULL) return NULL; return CopyFocus(link,parentLink); } if (link->m_Type==LAYOUTITEM_PROPERTYSET) { if (parentLink->m_Type!=LAYOUTITEM_ELEMENT) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT,&sibling); if (parentLink==NULL) return NULL; if (parentLink->m_Element->m_Class->GetMember(link->m_Element->m_Parent->m_Member->m_Name)==NULL) return NULL; link=Copy(link,parentLink,sibling,actionModifer); if (link==NULL) return NULL; AddToElement(link,parentLink,sibling); return link->m_Element; } if (parentLink->m_Type==LAYOUTITEM_FOCUS || parentLink->m_Type==LAYOUTITEM_PROPERTYSET) parentLink=parentLink->GetParent(LAYOUTITEM_ELEMENT,&sibling); if (parentLink->m_Type==LAYOUTITEM_LAYOUTITEM) parentLink=parentLink->GetParent(LAYOUTITEM_LAYOUT,&sibling); if (link->m_Type==LAYOUTITEM_ELEMENT) { if (parentLink->m_Type==LAYOUTITEM_LAYOUT) { layout=parentLink; layoutSibling=sibling; parentLink=layout->GetParent(LAYOUTITEM_ELEMENT,&sibling); } else if (parentLink->m_Type==LAYOUTITEM_ELEMENT) layout=parentLink->GetLayout(); if (layout!=NULL) link=Copy(link,layout,layoutSibling,actionModifer); else link=Copy(link,parentLink,sibling,actionModifer); if (link==NULL) return NULL; if ((actionModifer&ACTIONMODIFIER_KEEPNAME)==0) SetName(link); AddToElement(link,parentLink,sibling); if (layout!=NULL) AddToLayout(link,layout,layoutSibling); return link->m_Element; } if (link->m_Type==LAYOUTITEM_LAYOUT) { bool replaceLayout=false; if (parentLink->m_Type==LAYOUTITEM_ELEMENT) { if (parentLink->GetLayout()==NULL) replaceLayout=true; else parentLink=parentLink->GetLayout(); sibling=NULL; } layout=link; if (replaceLayout) link=CopyLayout(link,parentLink); else link=Copy(link,parentLink,sibling,actionModifer); if (link==NULL) return NULL; if ((actionModifer&ACTIONMODIFIER_KEEPNAME)==0) SetName(link); if (!replaceLayout) { AddToLayout(link,parentLink,sibling); Add2(link->m_Element,link->m_LayoutItem); } CopyLayoutChildren(link,layout,actionModifer); return link->m_Element; } if (link->m_Type==LAYOUTITEM_LAYOUTITEM) { if (parentLink->m_Type==LAYOUTITEM_ELEMENT) { if (parentLink->GetLayout()==NULL) parentLink=parentLink->GetParent(LAYOUTITEM_LAYOUT,&sibling); else { parentLink=parentLink->GetLayout(); sibling=NULL; } } if (parentLink==NULL) return NULL; link=Copy(link,parentLink,sibling,actionModifer); if (link==NULL) return NULL; if ((actionModifer&ACTIONMODIFIER_KEEPNAME)==0) SetName(link); AddToLayout(link,parentLink,sibling); Add2(link->m_Element,link->m_LayoutItem); return link->m_Element; } return NULL; }