int reachStatus(NFA nfa, int status_index, wchar_t c) { int i, e_index; assert(nfa); assert(status_index>=0 && status_index<Array_length(nfa->statusArray)); Status currStatus; Array_T outEdges; Edge e; currStatus = Array_get(nfa->statusArray, status_index); outEdges = getOutEdges(currStatus); if (!outEdges) return -1; for (i=0; i<Array_length(outEdges); i++) { e_index = *(int*)Array_get(outEdges, i); e = Array_get(nfa->edgeArray, e_index); if (crossEdge(e, c)) { return getStatusID(gettoStatus(e)); } } return -1; }
char *test_get() { mu_assert(Array_get(array, 0) == val1, "Wrong first value."); mu_assert(Array_get(array, 1) == val2, "Wrong second value."); return NULL; }
static void DFS(Status fromS, NFA nfa, Array_T closure, int inverse[], int color[], int set) { int i, index, from, to; Edge e; Status toS; from = getStatusID(fromS); color[from] = 1; inverse[from] = set; Array_append(closure, &from); Array_T edgeArray = getEdgeArray(nfa); Array_T outEdges_indice = getOutEdges(fromS); if (!outEdges_indice) return; for (i=0; i<Array_length(outEdges_indice); i++) { index = *(int*)Array_get(outEdges_indice, i); e = (Edge)Array_get(edgeArray, index); if (isEpsilon(e)) { toS = gettoStatus(e); to = getStatusID(toS); if (color[to] == 0) DFS(toS, nfa, closure, inverse, color, set); } } }
// 根据nfa中图结构,为copyNFA构建一样的图结构,其中 // copyNFA中的顶点下标从s_offset开始计数,边下标从e_offset开始计数 static void adjustStatusEdges (NFA copyNFA, NFA nfa, int s_offset, int e_offset) { int i, id, id2; Edge e, e_temp; Status s1, s2, fromS, toS; for (i=0; i < Array_length (nfa->edgeArray); i++) { e_temp = Array_get (nfa->edgeArray, i); e = Array_get (copyNFA->edgeArray, i+e_offset); copyEdge_without_Status (e, e_temp); id = getStatusID (getfromStatus (e_temp)); s1 = Array_get (copyNFA->statusArray, id+s_offset); id2 = getStatusID (gettoStatus (e_temp)); s2 = Array_get (copyNFA->statusArray, id2+s_offset); if (id==id2) { wprintf(L"error!!!\n"); } link_Two_Status_In_Automaton (copyNFA, id+s_offset, id2+s_offset, i+e_offset); //linkTwoStatus_by_AnEdge (s1, s2, e); } }
NFA Union (NFA nfa1, NFA nfa2 ) { int i, j, n1, n2, n, e1, e2; NFA combined_NFA; Status s; Edge e; /* 得到两个NFA 边(Edge)数 和 顶点(Status)数 */ n1 = Array_length (nfa1->statusArray); n2 = Array_length (nfa2->statusArray); e1 = Array_length (nfa1->edgeArray); e2 = Array_length (nfa2->edgeArray); n = n1 + n2 + 2; combined_NFA = malloc (sizeof (struct Automaton)); assert(combined_NFA); combined_NFA->statusArray = allocStatusArray(n); /* 构造边集合, 即两个NFA的边数加上4条ε边 */ n = e1 + e2 + 4; combined_NFA->edgeArray = allocEdgeArray(n); /* 提取0号边 */ e = Array_get (combined_NFA->edgeArray, 0); setEpsilon (e); link_Two_Status_In_Automaton (combined_NFA, 0, 1, 0); adjustStatusEdges (combined_NFA, nfa1, 1, 2); e = Array_get (combined_NFA->edgeArray, 1); setEpsilon (e); link_Two_Status_In_Automaton (combined_NFA, 0, n1+1, 1); adjustStatusEdges (combined_NFA, nfa2, n1+1, e1+2); setEpsilon (Array_get (combined_NFA->edgeArray, e1+e2+2)); link_Two_Status_In_Automaton (combined_NFA, n1, n1+n2+1, e1+e2+2); setEpsilon (Array_get (combined_NFA->edgeArray, e1+e2+3)); link_Two_Status_In_Automaton (combined_NFA, n1+n2, n1+n2+1, e1+e2+3); adjustStatusID (combined_NFA); ensureFinalStatus (combined_NFA->end); free_Automaton(nfa1); free_Automaton(nfa2); return combined_NFA ; }
NFA Link (NFA frontNFA, NFA toNFA ) { int i, j, numStatus ; int n1, n2, n; NFA combined_NFA; Edge bridge; Status s, s1, s2; combined_NFA = malloc (sizeof (struct Automaton)); assert(combined_NFA); /* 构造combined_NFA所有状态 */ n = Array_length (frontNFA->statusArray) + Array_length (toNFA->statusArray); combined_NFA->statusArray = allocStatusArray(n); adjustStatusID (combined_NFA); combined_NFA->start = Array_get(combined_NFA->statusArray, 0); combined_NFA->end = Array_get(combined_NFA->statusArray, n-1); ensureFinalStatus(combined_NFA->end); /* combined_NFA边集合 <=> 两个nfa以及连接它们的边ε*/ n = Array_length(frontNFA->edgeArray) + Array_length(toNFA->edgeArray) + 1; combined_NFA->edgeArray = allocEdgeArray(n); adjustStatusEdges(combined_NFA, frontNFA, 0, 0); n1 = Array_length (frontNFA->statusArray); n2 = Array_length (toNFA->statusArray); /* 连结两个NFA图的边是epsilon edge */ bridge = Array_get(combined_NFA->edgeArray, Array_length(frontNFA->edgeArray)); setEpsilon (bridge); // 提取对应于frontNFA状态的最后一个状态 s1 = Array_get (combined_NFA->statusArray, n1-1); s2 = Array_get (combined_NFA->statusArray, n1); link_Two_Status_In_Automaton (combined_NFA, n1-1, n1, Array_length(frontNFA->edgeArray)); // linkTwoStatus_by_AnEdge (s1, s2, bridge); /* 根据已有的NFA, 确认combined_NFA中边和点之间的关系 */ /* 已边数: frontNFA所有边以及bridge, 所以边计数从n开始*/ n = Array_length (frontNFA->edgeArray) + 1; adjustStatusEdges (combined_NFA, toNFA, n1, n); free_Automaton(frontNFA); free_Automaton(toNFA); return combined_NFA ; }
// Same as the map function above, but also updates the values of the // input array. void RoomyList_mapAndModify(RoomyList *rl, void (*mapFunc)(void* oldVal, void* newValOut)) { // if list is locally empty, return without mapping if(rl->lSize == 0) return; char listFile[RGLO_STR_SIZE]; RoomyList_getListFileName(rl, RGLO_MY_RANK, listFile); uint64 bytesLeft = numBytesInFile(listFile); uint64 chunkSize = RGLO_BUFFER_SIZE; chunkSize = chunkSize - (chunkSize % rl->bytesPer); uint64 arraySize = chunkSize / rl->bytesPer; Array chunk = Array_make(rl->bytesPer, arraySize); FILE* f = fopen(listFile, "a+"); uint64 filePos = 0; void* newVal = malloc(rl->bytesPer); while(bytesLeft) { // load chunk uint64 curChunkSize = chunkSize; if(curChunkSize > bytesLeft) curChunkSize = bytesLeft; uint64 curArraySize = curChunkSize / rl->bytesPer; fseek(f, filePos, SEEK_SET); Array_fileDescLoad(&chunk, f, curArraySize); // apply map function, with modifications int i; for(i=0; i<curArraySize; i++) { mapFunc(Array_get(&chunk, i), newVal); // keep track of predicate changes int p; for(p=0; p<rl->numPredicates; p++) { rl->predicateVals[p] += rl->predFuncs[p](newVal) - rl->predFuncs[p](Array_get(&chunk, i)); } Array_set(&chunk, i, newVal); } // save chunk fseek(f, filePos, SEEK_SET); Array_fileDescSave(&chunk, f, curArraySize); filePos += curChunkSize; bytesLeft -= curChunkSize; } // clean up Array_destroy(&chunk); free(newVal); fclose(f); }
void get_two_StatusSet_Table (NFA nfa, StatusSet_table *T, int inverse[]) { int i, j, id, n, curr_set_id, e_index; Edge e; Status s; Array_T StatusArray, edgeArray, OutEdges, closure; T->size = 0; StatusArray = getStatusArray (nfa); edgeArray = getEdgeArray (nfa); n = Array_length (StatusArray); /* 一开始所有状态不属于任一集合 */ for (i=0; i<n; i++) inverse[i] = -1; for (i=0; i<n; i++) { if (inverse[i] != -1) continue; /* 闭包缓存 */ curr_set_id = T->size; inverse[i] = curr_set_id; closure = Array_new (0, sizeof(int)); s = Array_get(StatusArray, i); getEpsilonClosure(s, nfa, closure, inverse, curr_set_id); for (j=0; j<Array_length(closure); j++) { id = *(int*)Array_get(closure, j); s = Array_get(nfa->statusArray, id); if (isFinalStatus(s)) T->H[T->size].hasFinalStatus = true; } /* 以s为原点进行DFS,找出所有epsilon闭包 * getEpsilonClosure(s, closure, inverse) */ T->H[T->size].epsClosure = closure; //printStatusSet(T->H[T->size]); //wprintf(L"-------------------------------\n"); T->size++; } }
/* 一旦成功匹配regex,立即返回下标位置,若匹配失败,则返回-1 */ int greedy_match (regexNode re, wchar_t *str, int currPos) { int i; int length = wcslen(str); wchar_t c; wchar_t *p; bool match; match = false; p = str+currPos; i = 0; Array_T statusArray = re->dfa->statusArray; while (*p) { i = re->T[i][*p]; if (i>=0 && isFinalStatus(Array_get(statusArray, i))) match = 1; /* 前移直到无法匹配为止 */ if (match && i==-1) return p-str-currPos; if (i==-1) return -1; p++; } if (match) return p-str-currPos; return -1; }
char *test_remove() { int *val_check = Array_remove(array, 0); mu_assert(val_check != NULL, "Should not get NULL."); mu_assert(*val_check == *val1, "Should get the first value."); mu_assert(Array_get(array, 0) == NULL, "Should be gone."); Array_free(val_check); val_check = Array_remove(array, 1); mu_assert(val_check != NULL, "Should not get NULL."); mu_assert(*val_check == *val2, "Should get the first value."); mu_assert(Array_get(array, 1) == NULL, "Should be gone."); Array_free(val_check); return NULL; }
NFA CreateSingleNFA (wchar_t c) { NFA nfa; Edge e; nfa = malloc (sizeof (struct Automaton)); assert(nfa); /* 初始化nfa的Edge数组 */ nfa->edgeArray = allocEdgeArray(1); /* 初始化nfa的Status状态数组 */ nfa->statusArray = allocStatusArray(2); e = Array_get(nfa->edgeArray, 0); clearBits(e); addCharacter (e, c); link_Two_Status_In_Automaton(nfa,0,1,0); adjustStatusID(nfa); ensureFinalStatus (nfa->end); return nfa ; }
/************************************************************************* * Return a pointer to the current element */ void* ReadBuffer_current(ReadBuffer *rb) { if(!ReadBuffer_hasMore(rb)) { perror("Tried next on an empty ReadBuffer"); exit(1); } return Array_get(&(rb->buffer), rb->bufferIndex); }
void printStatusSet(StatusSet S) { int i; wprintf(L"eps_Closure: "); for (i=0; i<Array_length(S.epsClosure); i++) wprintf(L"%d ", *(int*)Array_get(S.epsClosure, i)); wprintf(L"\n"); }
/*** sharedBlock2 ***/ double DiscreteRandomSource_rand(double* seed, Token pmf, Token values) { int i; double randomValue; double cdf = 0.0; // Generate a double between 0 and 1, uniformly distributed. randomValue = RandomSource_nextDouble(seed); for (i = 0; i < pmf.payload.Array->size; i++) { cdf += Array_get(pmf, i).payload.Double; if (randomValue <= cdf) { return Array_get(values, i).payload.Double; } } // We shouldn't get here, but if we do, we output the last value. return Array_get(values, pmf.payload.Array->size - 1).payload.Double; }
/* * nfa1 G: n×e * 以下是 求nfa闭包 * e+2 号边 ————————————————————— | | | | * (0号边) —> ------ —————— * ————————————> | nfa | ————————————————— | ------ e+3号边 | | v ----- ----- | start | (0号顶点) | end | (n+1) 号顶点 ----- ----- | | ——————————————————————————————————————————— 1号边 边顺序: 0号 1号 nfa中的e条边: 2, 3, ... e e+2号边 (e+3)号边 顶点顺序: start: 0号, nfa中n个顶点: 1, 2, 3, ..., n end : n+1 */ NFA Closure(NFA nfa) { NFA newnfa; int n, e; newnfa = malloc (sizeof (struct Automaton)); assert (newnfa); /* 构造边数组 */ e = Array_length (nfa->edgeArray); newnfa->edgeArray = allocEdgeArray (e+4); // 得到nfa状态个数 n = Array_length (nfa->statusArray); newnfa->statusArray = allocStatusArray (n+2); /* 提取0号边 */ setEpsilon (Array_get (newnfa->edgeArray, 0)); link_Two_Status_In_Automaton (newnfa, 0, 1, 0); /* 提取1号边 */ setEpsilon (Array_get (newnfa->edgeArray, 1)); link_Two_Status_In_Automaton (newnfa, 0, n+1, 1); /* 在newnfa中构造与nfa对应一样的图结构 */ adjustStatusEdges (newnfa, nfa, 1, 2); /* 提取e+2号边 */ setEpsilon (Array_get (newnfa->edgeArray, e+2)); link_Two_Status_In_Automaton (newnfa, n, 1, e+2); /* 提取e+3号边 */ setEpsilon (Array_get (newnfa->edgeArray, e+3)); link_Two_Status_In_Automaton (newnfa, n, n+1, e+3); adjustStatusID (newnfa); free_Automaton(nfa); return newnfa; }
void testArray() { int n = 100; // make Array a = Array_make(sizeof(int), n); // set and get int i; for(i=0; i<n; i++) { Array_set(&a, i, &i); } for(i=0; i<n; i++) { int* val = (int*)Array_get(&a, i); assert(*val == i); } // save and load char* fname = "/tmp/test.array"; Array_fileSave(&a, fname); Array b = Array_make(sizeof(int), n); Array_fileLoad(&b, fname); for(i=0; i<n; i++) { int* val = (int*)Array_get(&b, i); assert(*val == i); } // make init int buf[n]; for(i=0; i<n; i++) { buf[i] = i; } Array c = Array_makeInit(sizeof(int), n, &buf); for(i=0; i<n; i++) { int* val = (int*)Array_get(&c, i); assert(*val == i); } Array_destroy(&a); Array_destroy(&b); Array_destroy(&c); printf("ARRAY PASSED TESTS\n"); }
NFA CreateNFA (int n, int e) { assert(n>0); assert(e>=0); NFA p; p = malloc (sizeof (struct Automaton)); assert (p); if (e>0) p->edgeArray = allocEdgeArray(e); p->statusArray = allocStatusArray(n); if (n==1) p->start = p->end = Array_get(p->statusArray, 0); else { p->start = Array_get(p->statusArray,0); p->end = Array_get(p->statusArray,n-1); } adjustStatusID(p); return p; }
DFATable makeUpDFATable (DFA dfa) { int i, j, n; Edge e; /* 针对DFA的每条边e, 其上的非匹配字符 记录在此数组中 */ bool isInMatchChar[128]; wchar_t c; Range range; int fromID, toID; DFATable T; n = Array_length(dfa->statusArray); T = malloc (sizeof(int*) * n); assert(T); for( i=0; i < n; i++ ) { T[i] = malloc (sizeof(int) * 128); assert(T[i]); for (j=0; j<128; j++) T[i][j] = -1; } for (i=0; i < Array_length(dfa->edgeArray); i++) { e = Array_get(dfa->edgeArray, i); fromID = getStatusID(getfromStatus(e)); toID = getStatusID(gettoStatus(e)); Array_T content = getEdgeContent(e); for (j=0; j<Array_length(content); j++) { c = *(wchar_t*)Array_get(content, j); T[fromID][c] = toID; } } return T; }
/** * 配列の指定位置から見て先頭に立っているビットの位置を返す * * @param a 立っているビットを探す配列 * @param begin ビット探索の開始位置 * * @return ビットの立っている位置。見つからなければa->length */ size_t Array_findFirstBit(Array *a, size_t begin) { size_t i; if(!a) { return 0; } for(i = begin; i < a->length; ++i) { if(Array_get(a, i)) { return i; } } return a->length; }
void*LedsSalida::ledsActualizar(void*a){ const LedsSalida* ledSalida =(const LedsSalida*) a; int i; for(i=0; i< Array_count(&(ledSalida->array));i++){ const LedConfig * ledConfig = (const LedConfig*)Array_get(&(ledSalida->array),i); if(*(ledConfig->addr) & ledConfig->mask) (ledSalida->frente).setLed(1,ledConfig->ledNum); else ledSalida->frente.setLed(0,ledConfig->ledNum); } return NULL; }
/*** toString_Array() ***/ char* toString_Array(Token thisToken) { int i; int currentSize, allocatedSize; char* string; Token elementString; allocatedSize = 256; string = (char*) malloc(allocatedSize); string[0] = '{'; string[1] = '\0'; // Space for '{', '}', and '\0' characters. currentSize = 3; for (i = 0; i < thisToken.payload.Array->size; i++) { // Calculate the require storage size. elementString = $tokenFunc(Array_get(thisToken, i)::toString()); //functionTable[(int)thisToken.payload.Array->elements[i].type][FUNC_toString](thisToken.payload.Array->elements[i]); currentSize += strlen(elementString.payload.String); if (i != 0) { currentSize += 2; } // Re-allocate storage. if (currentSize > allocatedSize) { allocatedSize *= 2; string = (char*) realloc(string, allocatedSize); } // Concat the element strings and separators. if (i != 0) { strcat(string, ", "); } strcat(string, elementString.payload.String); free(elementString.payload.String); } strcat(string, "}"); return string; }
bool Recognition (Regex regex, wchar_t *str) { int i; int length = wcslen(str); wchar_t c; i = 0; while ((c=(*str++)) != '\0') { if (regex.T[i][c] == -1) return false; else i = regex.T[i][c]; } if (isFinalStatus(Array_get(regex.dfa->statusArray, i))) return true; return false; }
Array_T reach_Status (NFA nfa, StatusSet currSet, int inverse[], wchar_t c) { Array_T toSets; int from, to; toSets = Array_new(0,sizeof(int)); int i; for (i=0; i<Array_length(currSet.epsClosure); i++) { from = *(int*)Array_get(currSet.epsClosure, i); to = reachStatus(nfa, from, c); if (to!=-1) { Array_append(toSets, &inverse[to]); } } return toSets; }
NFA CopyNFA (NFA nfa) { int i, id, n; NFA copyNFA ; Status fromS, toS, s1, s2; Edge e, e_temp; copyNFA = malloc (sizeof (NFA)); assert (copyNFA); /* 首先,复制nfa状态数组中所有状态 */ n = Array_length (nfa->statusArray); copyNFA -> statusArray = Array_new (n, sizeOfStatus()); Array_copy_from_range (copyNFA->statusArray, 0, nfa->statusArray, 0, n); /* 第二步,复制所有边(除了指向前后Status的储存单元 */ n = Array_length (nfa->edgeArray); copyNFA -> edgeArray = Array_new (n, sizeOfEdge()); Array_copy_from_range (copyNFA->edgeArray, 0, nfa->edgeArray, 0, n); /* 第三部,针对edgeArray的每一条边,生成每个Status的InEdges和OutEdges */ for (i=0; i < Array_length (nfa->edgeArray); i++) { e = (Edge)Array_get (nfa->edgeArray, i); fromS = getfromStatus (e); toS = gettoStatus (e); e = Array_get (copyNFA->edgeArray, i); id = getStatusID (fromS); s1 = Array_get (copyNFA->statusArray, id); appendOutEdge (s1, i); id = getStatusID (toS); s2 = Array_get (copyNFA->statusArray, id); appendInEdge (s2, i); setFromToStatus (e, s1, s2); } /* 最后,更新start 和 end */ n = Array_length (copyNFA->statusArray); copyNFA -> start = Array_get (copyNFA->statusArray, 0); copyNFA -> end = Array_get (copyNFA->statusArray, n-1); ensureFinalStatus (copyNFA->end); return copyNFA; }
Array_T getCharSet(NFA nfa) { int i; ULL128 v; ULL64 a = 1ULL; Edge e; wchar_t c; Array_T charSet; charSet = Array_new(0,sizeof(wchar_t)); setZero(&v); for (i=0; i<Array_length(nfa->edgeArray); i++) { e = Array_get(nfa->edgeArray, i); if (!isEpsilon(e)) { v = Or(getMatchBitVector(e), v); } } for (i=0; i<64; i++) { c = (wchar_t)i; if ((a & v.L64) != 0) Array_append (charSet, &c); a <<= 1; } a = 1ULL; for (i=0; i<64; i++) { c = (wchar_t)(i+64); if ((a & v.H64) != 0) Array_append (charSet, &c); a <<= 1; } return charSet; }
void Player_beginner2_think(Player* player, Othello* othello, int* x, int* y, BOOL* isResign){ // 最もたくさんの数が取れる場所に打つ Evaluation eval; PointList list; Othello_moveList(othello, &list); if(list.length > 0){ eval = Player_beginner2_search(player, othello, &list, 5); *x = list.x[eval.index]; *y = list.y[eval.index]; *isResign = FALSE; } else{ *isResign = TRUE; } { Array int_array; int temp; int temp_out; int i; Array_new(&int_array, 5, sizeof(int)); temp = 0; Array_append(&int_array, &temp); temp = 1; Array_append(&int_array, &temp); temp = 2; Array_append(&int_array, &temp); temp = 3; Array_append(&int_array, &temp); temp = 4; Array_append(&int_array, &temp); temp = 5; Array_append(&int_array, &temp); for(i = 0; i < Array_length(&int_array); i++){ temp_out = *(int*)Array_get(&int_array, i); } Array_delete(&int_array); } }
/** * 指定サイズの部分文字列で、2つ以上存在するものの位置をビット配列にセットする * * @param source 文字列全体 * @param dest 出力先ビット列 * @param oldBits 前回検索時のビット列 * @param len 部分文字列の長さ * @param hash ハッシュテーブル * * @return 一致する部分文字列があればR_OK。なければR_NOTFOUND */ Result scanSameSubstrings(const Array *source, Array *dest, const Array *oldBits, size_t len, Hash *hash) { size_t slen = source->length - len + 1; size_t i = 0; size_t j = 0; int found = 0; Result result = R_NG; Hash_clear(hash); /* 指定サイズの全部分文字列について、ハッシュを使用して重複しているものを洗い出す */ for(i = 0; i < (slen - 1); ++i) { const char *p; const HashEntry *before = NULL; /* 前回検査時の重複文字列の位置でなければスキップ */ if(!Array_get(oldBits, i)) { continue; } /* 現在位置の部分文字列が重複しているか検査する。未登録であればハッシュに登録する */ p = Array_pointer(source, i); if(Hash_putIfAbsent(hash, p, len, &before) != R_OK) { return R_NG; } /* 重複している部分文字列だった場合、以前と今回の出現箇所のビットを立てる */ if(before) { Array_set(dest, before->p - (const char *)source->p, 1); Array_set(dest, i, 1); found = 1; } } return found ? R_OK : R_NOTFOUND; }
/*** TokenFireBlock ***/ $ref(output) = Array_get($ref(input), $ref(index)); /**/ /*** PrimitiveFireBlock ***/ $ref(output) = Array_get($ref(input), $ref(index)).payload.$cgType(output); /**/