set_t doUnion(const set_t* s1, const set_t* s2) { set_t unionSet; initSet( &unionSet ); node_set_t* traverse; if(s1) { traverse = s1->head; while(traverse) { addElements(&unionSet, traverse->variable); traverse = traverse->link; } } if(s2) { traverse = s2->head; while(traverse) { addElements(&unionSet, traverse->variable); traverse = traverse->link; } } return unionSet; }
int create_set() { static char inited = 0; int sd; if (!inited) { inited = 1; init(); } if (num_of_sets == MAX_NUM_OF_SETS) { printf("Maximum number of sets reached\n"); return 0; } flowArray[num_of_sets] = (struct anonflow *)malloc(sizeof(struct anonflow)); if (flowArray[num_of_sets] == NULL) { printf("Malloc failed\n"); return -1; } initSet(flowArray[num_of_sets]); sd = num_of_sets; num_of_sets++; return sd; }
bool containsNearbyDuplicate(int* nums, int numsSize, int k) { if(numsSize < 2) { return false; } struct Set set; initSet(&set, numsSize); for(int i = 0; i < numsSize; ++i) { if(i>k) { removeVal(&set,nums[i-k-1]); } if(!addValue(&set, nums[i])) { releaseSet(&set); return true; } } releaseSet(&set); return false; }
quadruple_t * make_quadruple( char str[50] ) { quadruple_t * temp = (quadruple_t*)malloc(sizeof(quadruple_t)); strcpy(temp->inst,str); temp->link = 0; initSet( &temp->live ); temp->def[0] = '\0'; return (quadruple_t*)temp; }
/* Compact font set */ void tcCompactSet(tcCtx g) { tcprivCtx h = g->ctx.tcpriv; fillSet(g); writeSet(g); freeFonts(g); initSet(g, h); #if TC_SUBR_SUPPORT subrReuse(g); #endif /* TC_SUBR_SUPPORT */ }
void printparan(int n) { char* s = new char[n*2]; int i = 0; initSet(n * 2); for (; i < n; i++) s[i] = '('; for (; i < n*2; i++) s[i]= ')'; rec(s, 2*n, 0); }
int main(int argc, char **argv) { int i,j,k; int hasOne; int old_a = -1,old_b = -1; int upLimit; int bLimit; int Max; fpin = fopen("../../data/ariprog.in","r"); fpout = fopen("../../data/ariprog.out","w"); fscanf(fpin, "%d %d",&N, &M); //N = 20, M = 200; hasOne = 0; initSet(); upLimit = count; bLimit = dcount; Max = set[upLimit-1]; for(j = 1; j < Max; j++) { int b = j; if( b > Max/(N-1)) continue; for(i = 0; i < upLimit-N; i++ ){ int a = set[i]; if(old_a == a) continue; else old_a = a; if(a > Max - b * (N-1)) continue; for(k =0; k < N; k++) if(test(a,b,k)) continue; else break; if(k >= N) { fprintf(fpout,"%d %d\n",a,b); hasOne = 1; } } } if(!hasOne) fprintf(fpout,"NONE\n"); return 0; }
/******************************************************** * server1 * general i divided the socket to two groups: * (1) socket that bind to the control port * (2) all the other socket (upload the files) ********************************************************/ void server1() { fd_set readfds; int max_sd, masterSocket; struct clientNode* clientsList; clientsList=NULL; //listen on control port puts("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); puts("~~~~~~~~~~~~(0)listen on control port~~~~~~~~~~~~"); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); masterSocket = listenOnPort(CONTROL_PORT); while(1) { printf("\ndebug mallocNum = %d\n",mallocNum); //initSet puts("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); puts("~~~~~~~~~~~~(1)initSet~~~~~~~~~~~~"); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); max_sd = initSet(&readfds, clientsList, masterSocket); //select if (select( max_sd + 1 , &readfds , NULL , NULL , NULL) < 0) { perror("select"); } //handle connection event if(FD_ISSET(masterSocket, &readfds)) { puts("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); puts("~~~~~~~~~~~~(2)handleControlPortEvent~~~~~~~~~~~~"); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); handleControlPortEvent(masterSocket,&clientsList); //handle clients event }else { puts("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); puts("~~~~~~~~~~~~(3)handleFilesPortsEvents~~~~~~~~~~~~"); puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); handleFilesPortsEvents(&clientsList, readfds); } } closeSocket(masterSocket); }
/* Create new context */ tcCtx tcNew(tcCallbacks *cb) { tcCtx g = cb->malloc(cb->ctx, sizeof(struct tcCtx_)); tcprivCtx h; g->cb = *cb; h = MEM_NEW(g, sizeof(struct tcprivCtx_)); tc_dna_memcb.ctx = g; tc_dna_memcb.manage = tc_manage; g->ctx.dnaCtx = dnaNew(&tc_dna_memcb, DNA_CHECK_ARGS); dnaINIT(g->ctx.dnaCtx, h->set, 4, 120); h->set.func = fontInit; initSet(g, h); /* Initialize other library modules */ g->ctx.sindex = NULL; g->ctx.fdselect = NULL; g->ctx.subr = NULL; g->ctx.cs = NULL; g->ctx.encoding = NULL; g->ctx.charset = NULL; g->ctx.recode = NULL; g->ctx.parse = NULL; g->ctx.tcpriv = NULL; g->ctx.t13 = NULL; sindexNew(g); encodingNew(g); charsetNew(g); parseNew(g); csNew(g); recodeNew(g); #if TC_SUBR_SUPPORT subrNew(g); #endif /* TC_SUBR_SUPPORT */ fdselectNew(g); t13New(g); /* Link contexts */ h->g = g; g->ctx.tcpriv = h; return g; }
int main( ) { int i; int a, b; scanf("%d%d%d", &n, &m, &p); initSet( ); for (i = 0; i < m; ++i) { scanf("%d%d", &a, &b); uniSet(a, b); } for (i = 0; i < p; ++i) { scanf("%d%d", &a, &b); if (find(a) == find(b)) { printf("Yes\n"); } else { printf("No\n"); } } return 0; }
static void stateMachine() { while (1) { if (zigbeeNetworkStatus == NWK_ONLINE) { if(moduleHasMessageWaiting()) //wait until SRDY goes low indicating a message has been received. { getMessage(); displayMessage(); } } switch (state) { case STATE_IDLE: { /* process command line commands only if not doing anything else: */ if (command != NO_CHARACTER_RECEIVED) { /* parse the command entered, and go to the required state */ state = processCommand(command); command = NO_CHARACTER_RECEIVED; } /* note: other flags (for different messages or events) can be added here */ break; } case STATE_INIT: { printf("Starting State Machine\r\n"); state = STATE_GET_DEVICE_TYPE; break; } /* A button press during startup will cause the application to prompt for device type */ case STATE_GET_DEVICE_TYPE: { set_type: while (command == NO_CHARACTER_RECEIVED) { printf("Setting Device Type: Press C for Coordinator, R for Router, or E for End Device.\r\n"); #define DEVICE_TYPE_DELAY_MS 2000 #define DEVICE_TYPE_DELAY_PER_CYCLE_MS 100 uint16_t delayCycles = DEVICE_TYPE_DELAY_MS / DEVICE_TYPE_DELAY_PER_CYCLE_MS; while ((command == NO_CHARACTER_RECEIVED) && (delayCycles--) > 0) delayMs(DEVICE_TYPE_DELAY_PER_CYCLE_MS); } switch (command) { case 'C': case 'c': printf("Coordinator it is...\r\n"); zigbeeDeviceType = COORDINATOR; break; case 'R': case 'r': printf("Router it is...\r\n"); zigbeeDeviceType = ROUTER; break; case 'E': case 'e': printf("End Device it is...\r\n"); zigbeeDeviceType = END_DEVICE; break; default: command = NO_CHARACTER_RECEIVED; goto set_type; } command = NO_CHARACTER_RECEIVED; state = STATE_MODULE_STARTUP; break; } case STATE_MODULE_STARTUP: { #define MODULE_START_DELAY_IF_FAIL_MS 5000 moduleResult_t result; /* Start with the default module configuration */ struct moduleConfiguration defaultConfiguration = DEFAULT_MODULE_CONFIGURATION_COORDINATOR; /* Make any changes needed here (channel list, PAN ID, etc.) We Configure the Zigbee Device Type (Router, Coordinator, End Device) based on what user selected */ defaultConfiguration.deviceType = zigbeeDeviceType; /* Change this below to be your operating region - MODULE_REGION_NORTH_AMERICA or MODULE_REGION_EUROPE */ #define OPERATING_REGION (MODULE_REGION_NORTH_AMERICA) // or MODULE_REGION_EUROPE while ((result = expressStartModule(&defaultConfiguration, GENERIC_APPLICATION_CONFIGURATION, OPERATING_REGION)) != MODULE_SUCCESS) { SET_NETWORK_FAILURE_LED_ON(); // Turn on the LED to show failure handleModuleError(result); printf("Retrying...\r\n"); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); SET_NETWORK_FAILURE_LED_OFF(); delayMs(MODULE_START_DELAY_IF_FAIL_MS/2); } printf("Success\r\n"); state = STATE_DISPLAY_NETWORK_INFORMATION; zigbeeNetworkStatus = NWK_ONLINE; break; } case STATE_DISPLAY_NETWORK_INFORMATION: { printf("Module Information:\r\n"); /* On network, display info about this network */ displayNetworkConfigurationParameters(); displayDeviceInformation(); displayCommandLineInterfaceHelp(); state = STATE_IDLE; //startup is done! break; } case STATE_VALID_SHORT_ADDRESS_ENTERED: //command line processor has a valid shortAddressEntered { state = pendingState; break; } case STATE_VALID_LONG_ADDRESS_ENTERED: { /* Flip byte order because we need to send it LSB first */ int8_t temp[8]; int i; for (i=0; i<8; i++) temp[7-i] = longAddressEntered[i]; memcpy(longAddressEntered, temp, 8); //Store LSB first since that is how it will be sent: state = pendingState; break; } case STATE_SEND_MESSAGE_VIA_SHORT_ADDRESS: { printf("Send to 0x%04X\r\n", shortAddressEntered); moduleResult_t result = afSendData(DEFAULT_ENDPOINT,DEFAULT_ENDPOINT,shortAddressEntered, TEST_CLUSTER, testMessage, 5); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); #ifdef RESTART_AFTER_ZM_FAILURE printf("\r\nRestarting\r\n"); state = STATE_MODULE_STARTUP; continue; #endif } state = STATE_IDLE; break; } case STATE_SEND_MESSAGE_VIA_LONG_ADDRESS: { printf("Send to (LSB first)"); printHexBytes(longAddressEntered, 8); moduleResult_t result = afSendDataExtended(DEFAULT_ENDPOINT, DEFAULT_ENDPOINT, longAddressEntered, DESTINATION_ADDRESS_MODE_LONG, TEST_CLUSTER, testMessage, 5); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); #ifdef RESTART_AFTER_ZM_FAILURE printf("\r\nRestarting\r\n"); state = STATE_MODULE_STARTUP; continue; #endif } state = STATE_IDLE; break; } #ifdef LEAVE_REQUEST case STATE_MANAGEMENT_LEAVE_REQUEST: { printf("Sending Leave Request for MAC (LSB first)"); printHexBytes(longAddressEntered, 8); moduleResult_t result = zdoManagementLeaveRequest(longAddressEntered, 0); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); } state = STATE_IDLE; break; } #endif case STATE_FIND_VIA_SHORT_ADDRESS: { printf("Looking for that device...\r\n"); moduleResult_t result = zdoRequestIeeeAddress(shortAddressEntered, INCLUDE_ASSOCIATED_DEVICES, 0); if (result == MODULE_SUCCESS) { #ifndef ZDO_NWK_ADDR_RSP_HANDLED_BY_APPLICATION displayZdoAddressResponse(zmBuf + SRSP_PAYLOAD_START); #endif } else { handleModuleError(result); } state = STATE_IDLE; break; } case STATE_FIND_VIA_LONG_ADDRESS: { printf("Looking for that device...\r\n"); moduleResult_t result = zdoNetworkAddressRequest(longAddressEntered, INCLUDE_ASSOCIATED_DEVICES, 0); if (result == MODULE_SUCCESS) { #ifndef ZDO_NWK_ADDR_RSP_HANDLED_BY_APPLICATION displayZdoAddressResponse(zmBuf + SRSP_PAYLOAD_START); #endif } else { handleModuleError(result); } state = STATE_IDLE; break; } #ifdef INCLUDE_PERMIT_JOIN case STATE_SET_PERMIT_JOIN_ON: { printf("Turning joining ON...\r\n"); moduleResult_t result = zdoManagementPermitJoinRequest(shortAddressEntered, PERMIT_JOIN_ON_INDEFINITELY, 0); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); } state = STATE_IDLE; break; } case STATE_SET_PERMIT_JOIN_OFF: { printf("Turning joining OFF...\r\n"); moduleResult_t result = zdoManagementPermitJoinRequest(shortAddressEntered, PERMIT_JOIN_OFF, 0); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); } state = STATE_IDLE; break; } #endif #ifdef DISCOVER_NETWORKS case STATE_NETWORK_DISCOVERY_REQUEST: { printf("Scanning...\r\n"); moduleResult_t result = zdoNetworkDiscoveryRequest(ANY_CHANNEL_MASK, BEACON_ORDER_480_MSEC); if (result == MODULE_SUCCESS) { printf("Success\r\n"); } else { handleModuleError(result); } state = STATE_IDLE; break; } #endif #ifdef INCLUDE_NETWORK_TOPOLOGY case STATE_GET_NETWORK_TOPOLOGY: { printf("Displaying NWK Topology...........\r\n"); initSet(&searchedSet); // Start the recursive search for all children of the short address //removeAllFromSet(); int8_t numChildren = displayChildren(shortAddressEntered); state = STATE_IDLE; break; } #endif default: //should never happen { printf("UNKNOWN STATE (%u)\r\n", state); state = STATE_IDLE; } break; } } }