string *readLinesFromStream(FILE *infile) { string *buffer, *nbuffer, line; int i, n, size; n = 0; size = INITIAL_BUFFER_SIZE; buffer = newArray(size, string); while (true) { line = readLine(infile); if (line == NULL) break; if (n == size) { size *= 2; nbuffer = newArray(size, string); for (i = 0; i < n; i++) { nbuffer[i] = buffer[i]; } freeBlock(buffer); buffer = nbuffer; } buffer[n++] = line; } nbuffer = newArray(n + 1, string); for (i = 0; i < n; i++) { nbuffer[i] = buffer[i]; } nbuffer[n] = NULL; freeBlock(buffer); return nbuffer; }
string *listDirectory(string path) { struct _finddata_t data; intptr_t fp; string *result; int i, n; path = expandPathname(path); if (!isDirectory(path)) error("listDirectory: Path is not a directory"); fp = _findfirst(concat(path, "\\*"), &data); if ((long) fp == -1L) { if (errno != ENOENT) { error("listDirectory: %s", strerror(errno)); } result = newArray(1, string); result[0] = NULL; return result; } n = 1; while (_findnext(fp, &data) == 0) n++; _findclose(fp); result = newArray(n + 1, string); fp = _findfirst(concat(path, "\\*"), &data); result[0] = copyString(data.name); for (i = 1; i < n; i++) { _findnext(fp, &data); result[i] = copyString(data.name); } result[n] = NULL; return result; }
/** Loads initial configuration at process 0 and sends it to other processes */ void setupArrays(const char* configFile, ARRAY2D arrays[2], int* pUpper, int* pLower) { int lengthWidth[2] = {0}; int size, rank; // get the size process group and the rank of this process MPI_Comm_size(MPI_COMM_WORLD, &size); MPI_Comm_rank(MPI_COMM_WORLD, &rank); // load initial configuration at process 0 if(rank == 0) { arrays[0] = readArray(configFile); arrays[1] = newArray(arrays[0].length, arrays[0].width); // check the size and number of processes - we currently only support even decompositions if(arrays[0].length % size != 0) { // TODO: used MPI_Scatterv and MPI_Gatherv to support uneven decompositions printf("This program does not support uneven decompositions. The number of rows (%d) \n" "should be divisible by the number of processes (%d)\n", arrays[0].length, size); MPI_Abort(MPI_COMM_WORLD, -1); } lengthWidth[0] = arrays[0].length; lengthWidth[1] = arrays[0].width; } // broadcast the size of the array to everyone MPI_Bcast(lengthWidth, 2, MPI_INT, 0, MPI_COMM_WORLD); // non root nodes must allocate buffer space if(rank != 0) { arrays[0] = newArray(lengthWidth[0], lengthWidth[1]); arrays[1] = newArray(lengthWidth[0], lengthWidth[1]); } // calculate bounds *pLower = rowsLowerBound(arrays[0].length, size, rank); *pUpper = rowsUpperBound(arrays[0].length, size, rank); // if running on more than one process if(size > 1) { // TODO: requires an even size decomposition // send configuration to each process /*MPI_Scatter( arrays[0].data, (*pUpper-*pLower)*arrays[0].width, MPI_CHAR, ARRAY2D_PTR(arrays[0],*pLower,0), (*pUpper-*pLower)*arrays[0].width, MPI_CHAR, 0, MPI_COMM_WORLD);*/ // each process needs not only its own chunk, but also the row above and // below it, so the simple MPI_Scatter call is insufficient // Broadcasting the entire config is an inefficient solution, but it's // also simple MPI_Bcast(arrays[0].data, arrays[0].length*arrays[0].width, MPI_CHAR, 0, MPI_COMM_WORLD); } }
ResponseRecord *newResponseRecord() { ResponseRecord *rec = calloc(1, sizeof(ResponseRecord)); rec->statusCode = newString(); rec->statusMessage = newString(); rec->headerNames = newArray(10); rec->headerValues = newArray(10); rec->fd = -1; return rec; }
double* FFTHandler::copy(double* in){ double* ret = newArray(); for(int ii = 0; ii < 2*(n/2+1); ++ii){ ret[ii]=in[ii]; } return ret; }
void dictionarySet(Object *dicto, Object *keyString, Something value, Thread *thread){ EmojicodeDictionary *dict = dicto->value; size_t index = findSlot(dict, keyString); slots(dict)[index].key = keyString; slots(dict)[index].value = value; if(++dict->count > 2 * (dict->capacity/3)){ size_t oldCapacity = dict->capacity; EmojicodeDictionaryKVP *oldSlots = slots(dict); stackPush(dicto, 0, 0, thread); Object *slotso = newArray(dict->capacity * 2 * sizeof(EmojicodeDictionaryKVP)); EmojicodeDictionary *dict = stackGetThis(thread)->value; stackPop(thread); dict->slots = slotso; dict->capacity *= 2; dict->count = 0; for (size_t i = 0; i < oldCapacity; i++) { if(oldSlots[i].key){ dictionarySet(dicto, oldSlots[i].key, oldSlots[i].value, thread); } } } }
/* readDeclaration reads a declaration of a class */ static void readClassDeclaration() { object classObj, super, vars; int i, size, instanceTop; object instanceVariables[15]; if (nextToken() != nameconst) sysError("bad file format", "no name in declaration"); classObj = findClass(tokenString); size = 0; if (nextToken() == nameconst) /* read superclass name */ { super = findClass(tokenString); basicAtPut(classObj, superClassInClass, super); size = intValue(basicAt(super, sizeInClass)); nextToken(); } if (token == nameconst) /* read instance var names */ { instanceTop = 0; while (token == nameconst) { instanceVariables[instanceTop++] = newSymbol(tokenString); size++; nextToken(); } vars = newArray(instanceTop); for (i = 0; i < instanceTop; i++) { basicAtPut(vars, i+1, instanceVariables[i]); } basicAtPut(classObj, variablesInClass, vars); } basicAtPut(classObj, sizeInClass, newInteger(size)); }
/*! Copies the given expression or list of expressions, depending on the next flag @param expr the expression(s) to copy @param next whether or not to copy the next expression in the list @return the new, duplicate expression(s) */ static expression* copyExpression_ (expression* expr, int next) { if (expr == NULL) { // if the expression is null then just return null return NULL; } else { expression* nextexpr; if (next) { nextexpr = copyExpression(expr->next); } else { nextexpr = NULL; } expression* duplicate = newExpressionAll(expr->type, NULL, nextexpr, expr->line); // create a new expression with identical properties to the original one exprvals* ev1 = &(duplicate->ev); exprvals* ev2 = &(expr->ev); int i; switch (expr->type) { // some data type require their inner contents to be copied case TYPE_STR: ev1->strval = copyString(ev2->strval); // copy the string's content break; case TYPE_ARR: ev1->arrval = newArray(ev2->arrval->size); for (i = 0; i < ev1->arrval->size; ++i) { // copy each of the array's elements ev1->arrval->content[i] = copyExpression(ev2->arrval->content[i]); } break; case TYPE_FUN: // copy the function's properties, arguments, and body ev1->funval = newTapFunction(ev2->funval->args, ev2->funval->minargs, ev2->funval->maxargs, copyExpression(ev2->funval->body)); break; default: // if the original expression value is a primitive then copy it to the new expression value ev1->intval = ev2->intval; break; } duplicate->flag = expr->flag; duplicate->isref = expr->isref; duplicate->refs = expr->refs; if (expr->type == TYPE_EXP) { // copy the child expressions if the expression is a container expression ev1->expval = copyExpression(ev2->expval); } else if (expr->type == TYPE_LAZ) { // copy the lazy expression content if the expression is lazy tap_laz* lazy = newLazyExpression(); lazy->expval = copyExpression(ev2->lazval->expval); exprstack* es2 = ev2->lazval->refs; if (es2 == NULL) { lazy->refs = NULL; } else { lazy->refs = allocate(sizeof(exprstack)); exprstack* es1 = lazy->refs; while (es2 != NULL) { es1->expr = es2->expr; if (es2->next != NULL) { es1->next = allocate(sizeof(exprstack)); es1 = es1->next; } es2 = es2->next; } } ev1->lazval = lazy; } return duplicate; } }
YArray* Array_map(YArray* arr, YLambda* lmbd, YThread* th) { if (lmbd->sig->argc != 1) return newArray(th); arr->parent.o.linkc++; lmbd->parent.o.linkc++; YArray* out = newArray(th); out->parent.o.linkc++; for (size_t i = 0; i < arr->size(arr, th); i++) { YValue* val = arr->get(arr, i, th); out->add(out, invokeLambda(lmbd, NULL, &val, 1, th), th); } out->parent.o.linkc--; arr->parent.o.linkc--; lmbd->parent.o.linkc--; return out; }
void integerToString(Thread *thread) { EmojicodeInteger base = thread->variable(0).raw; EmojicodeInteger n = thread->thisContext().value->raw, a = std::abs(n); bool negative = n < 0; EmojicodeInteger d = negative ? 2 : 1; while ((n /= base) != 0) { d++; } auto co = thread->retain(newArray(d * sizeof(EmojicodeChar))); Object *stringObject = newObject(CL_STRING); auto *string = stringObject->val<String>(); string->length = d; string->charactersObject = co.unretainedPointer(); EmojicodeChar *characters = string->characters() + d; do { *--characters = "0123456789abcdefghijklmnopqrstuvxyz"[a % base % 35]; } while ((a /= base) > 0); if (negative) { characters[-1] = '-'; } thread->release(1); thread->returnFromFunction(stringObject); }
// Transform whole array, including application of pre/post transform shift Array<double> Transformer::transformArray(Array<double> sourceX, Array<double> sourceY, double z, int target) { // If transform is not enabled, return original array if (!enabled_) return (target == 0 ? sourceX : sourceY); // If equation is not valid, just return original array if (!valid_) { msg.print("Equation is not valid, so returning original array.\n"); return (target == 0 ? sourceX : sourceY); } if (sourceX.nItems() != sourceY.nItems()) { msg.print("Error in Transformer::transformArray() - x and y array sizes do not match.\n"); return Array<double>(); } // Create new array, and create reference to target array Array<double> newArray(sourceX.nItems()); z_->set(z); bool success; // Loop over x points for (int n=0; n<sourceX.nItems(); ++n) { // Set x and y values in equation x_->set(sourceX[n]); y_->set(sourceY[n]); newArray[n] = equation_.execute(success); if (!success) break; } return newArray; }
UGenArray UGenArray::range(const int startIndex, const int endIndex) const throw() { ugen_assert(internal != 0); if(internal->size() == 0) return UGenArray(); const int startIndexChecked = clip(startIndex, 0, internal->size()); const int endIndexChecked = clip(endIndex, 0, internal->size()); const int size = endIndexChecked - startIndexChecked; if(size <= 0) { return UGenArray(); } else { UGenArray newArray(size); for(int i = 0; i < size; i++) { newArray.put(i, at(i + startIndexChecked)); } return newArray; } }
Something integerToString(Thread *thread) { EmojicodeInteger base = stackGetVariable(0, thread).raw; EmojicodeInteger n = stackGetThisContext(thread).raw, a = llabs(n); bool negative = n < 0; EmojicodeInteger d = negative ? 2 : 1; while (n /= base) d++; Object *co = newArray(d * sizeof(EmojicodeChar)); stackSetVariable(0, somethingObject(co), thread); Object *stringObject = newObject(CL_STRING); String *string = stringObject->value; string->length = d; string->characters = stackGetVariable(0, thread).object; EmojicodeChar *characters = characters(string) + d; do *--characters = "0123456789abcdefghijklmnopqrstuvxyz"[a % base % 35]; while (a /= base); if (negative) characters[-1] = '-'; return somethingObject(stringObject); }
Array<CryptoBuffer> CryptoBuffer::Slice(size_t sizeOfSlice) const { assert(sizeOfSlice <= GetLength()); size_t numberOfSlices = (GetLength() + sizeOfSlice - 1) / sizeOfSlice; size_t currentSliceIndex = 0; Array<CryptoBuffer> slices(numberOfSlices); for (size_t i = 0; i < numberOfSlices - 1; ++i) { CryptoBuffer newArray(sizeOfSlice); for (size_t cpyIdx = 0; cpyIdx < newArray.GetLength(); ++cpyIdx) { newArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex); } currentSliceIndex += sizeOfSlice; slices[i] = std::move(newArray); } CryptoBuffer lastArray(GetLength() % sizeOfSlice == 0 ? sizeOfSlice : GetLength() % sizeOfSlice); for (size_t cpyIdx = 0; cpyIdx < lastArray.GetLength(); ++cpyIdx) { lastArray[cpyIdx] = GetItem(cpyIdx + currentSliceIndex); } slices[slices.GetLength() - 1] = std::move(lastArray); return slices; }
YArray* Array_compact(YArray* arr, YThread* th) { YArray* out = newArray(th); for (size_t i = 0; i < arr->size(arr, th); i++) { YValue* val = arr->get(arr, i, th); if (val->type != &th->runtime->NullType) out->add(out, val, th); } return out; }
void dictionaryInit(Thread *thread){ #define initialCapactiy 7 Object *slots = newArray(sizeof(EmojicodeDictionaryKVP) * (initialCapactiy)); EmojicodeDictionary *dict = stackGetThis(thread)->value; dict->capacity = initialCapactiy; dict->slots = slots; #undef initialCapacity }
YArray* Array_sort(YArray* arr, YLambda* lmbd, YThread* th) { if (lmbd->sig->argc != 2) return arr; if (arr->size(arr, th) == 0) return arr; arr->parent.o.linkc++; lmbd->parent.o.linkc++; YArray* out = newArray(th); out->parent.o.linkc++; YArray* sub1 = newArray(th); YArray* sub2 = newArray(th); YArray* sub3 = newArray(th); sub1->parent.o.linkc++; sub2->parent.o.linkc++; sub3->parent.o.linkc++; YValue* val = arr->get(arr, arr->size(arr, th) / 2, th); for (size_t i = 0; i < arr->size(arr, th); i++) { YValue* v = arr->get(arr, i, th); YValue* args[] = { val, v }; YValue* res = invokeLambda(lmbd, NULL, args, 2, th); if (res->type != &th->runtime->IntType) break; int64_t ires = ((YInteger*) res)->value; if (ires == 0) sub2->add(sub2, v, th); else if (ires < 0) sub3->add(sub3, v, th); else sub1->add(sub1, v, th); } sub1->parent.o.linkc--; Array_addAll(out, Array_sort(sub1, lmbd, th), th); sub2->parent.o.linkc--; Array_addAll(out, sub2, th); sub3->parent.o.linkc--; Array_addAll(out, Array_sort(sub3, lmbd, th), th); arr->parent.o.linkc--; lmbd->parent.o.linkc--; out->parent.o.linkc--; return out; }
void ScriptEngine::defineProperty(QScriptValue &object, const QString &name, const QScriptValue &descriptor) { QScriptValue arguments = newArray(); arguments.setProperty(0, object); arguments.setProperty(1, name); arguments.setProperty(2, descriptor); QScriptValue result = m_definePropertyFunction.call(QScriptValue(), arguments); QBS_ASSERT(!hasErrorOrException(result), qDebug() << name << result.toString()); }
void ArrayEditor<ElementType>::SubmitInternalArray() { ArrayType newArray(mArray.size()); uint32 i = 0; for (typename InternalArray::iterator it = mArray.begin(); it != mArray.end(); ++i, ++it) { newArray[i] = **it; } mModel->SetValue(&newArray); }
RequestRecord *newRequestRecord() { RequestRecord *rec = calloc(1, sizeof(RequestRecord)); rec->host = newString(); rec->port = newString(); rec->method = newString(); rec->protocol = newString(); rec->path = newString(); rec->queryString = newString(); rec->headerNames = newArray(10); rec->headerValues = newArray(10); rec->parameterNames = newArray(10); rec->parameterValues = newArray(10); rec->fd = -1; return rec; }
static void symbolToString(Thread *thread) { auto co = thread->retain(newArray(sizeof(EmojicodeChar))); Object *stringObject = newObject(CL_STRING); auto *string = stringObject->val<String>(); string->length = 1; string->charactersObject = co.unretainedPointer(); thread->release(1); string->characters()[0] = thread->thisContext().value->character; thread->returnFromFunction(stringObject); }
void CFlashUIArrayConcatNode::ProcessEvent( EFlowEvent event,SActivationInfo* pActInfo ) { if (event == eFE_Activate && IsPortActive( pActInfo, eI_Set )) { SUIArguments newArray( GetPortString( pActInfo, eI_Arr1 ).c_str()); newArray.AddArguments( GetPortString( pActInfo, eI_Arr2 ).c_str()); ActivateOutput( pActInfo, eO_OnSet, true ); ActivateOutput( pActInfo, eO_ArgList, string( newArray.GetAsString())); } }
static Something stringFromSymbol(Thread *thread){ Object *co = newArray(sizeof(EmojicodeChar)); stackPush(somethingObject(co), 0, 0, thread); Object *stringObject = newObject(CL_STRING); String *string = stringObject->value; string->length = 1; string->characters = stackGetThisObject(thread); stackPop(thread); ((EmojicodeChar *)string->characters->value)[0] = (EmojicodeChar)stackGetThisContext(thread).raw; return somethingObject(stringObject); }
/* readDeclaration reads a declaration of a class */ static void readClassDeclaration() { ObjectHandle classObj, metaObj, vars; std::string className, superName; int i, size, instanceTop; // todo: fixed length variables array! ObjectHandle instanceVariables[15]; // todo: horrible fixed length arrays! char metaClassName[100]; char metaSuperClassName[100]; if (ll.nextToken() != nameconst) sysError("bad file format","no name in declaration"); className = ll.strToken(); if (ll.nextToken() == nameconst) { /* read superclass name */ superName = ll.strToken(); ll.nextToken(); } // todo: sprintf eradication! sprintf(metaClassName, "Meta%s", className.c_str()); if(!superName.empty()) sprintf(metaSuperClassName, "Meta%s", superName.c_str()); else sprintf(metaSuperClassName, "Class"); metaObj = createRawClass(metaClassName, "Class", metaSuperClassName); classObj = createRawClass(className.c_str(), metaClassName, superName.c_str()); classObj->_class = metaObj; // Get the current class size, we'll build on this as // we add instance variables. size = getInteger(classObj->basicAt(sizeInClass)); if (ll.currentToken() == nameconst) { /* read instance var names */ instanceTop = 0; while (ll.currentToken() == nameconst) { instanceVariables[instanceTop++] = createSymbol(ll.strToken().c_str()); size++; ll.nextToken(); } vars = newArray(instanceTop); for (i = 0; i < instanceTop; i++) { vars->basicAtPut(i+1, instanceVariables[i]); } classObj->basicAtPut(variablesInClass, vars); } classObj->basicAtPut(sizeInClass, newInteger(size)); classObj->basicAtPut(methodsInClass, newDictionary(39)); }
void ScriptEngine::setDeprecatedProperty(QScriptValue &object, const QString &oldName, const QString &newName, const QScriptValue &value) { QScriptValue data = newArray(); data.setProperty(0, oldName); data.setProperty(1, newName); data.setProperty(2, value); QScriptValue getterFunc = newFunction(js_deprecatedGet); getterFunc.setProperty(QLatin1String("qbsdata"), data); object.setProperty(oldName, getterFunc, QScriptValue::PropertyGetter | QScriptValue::SkipInEnumeration); }
void addStatementToStatementList( StatementList* sl, Statement* statement ) { if(sl->item==NULL){ sl->count=1; sl->item = newArray( Statement, sl->count ); sl->item[0] = *statement; }else{ sl->count++; sl->item = resizeArray( sl->item, Statement, sl->count ); sl->item[sl->count-1] = *statement; } }
void addExpressionToExpressionList( ExpressionList* el, Expression* expression ) { if(el->expressions==NULL){ el->count=1; el->expressions = newArray( Statement, el->count ); el->expressions[0] = *expression; }else{ el->count++; el->expressions = resizeArray( el->expressions, Expression, el->count ); el->expressions[el->count-1] = *expression; } }
static Something systemSystem(Thread *thread) { char *command = stringToChar(stackGetVariable(0, thread).object->value); FILE *f = popen(command, "r"); free(command); if (!f) { return NOTHINGNESS; } size_t bufferUsedSize = 0; int bufferSize = 50; Object *buffer = newArray(bufferSize); while (fgets((char *)buffer->value + bufferUsedSize, bufferSize - (int)bufferUsedSize, f) != NULL) { bufferUsedSize = strlen(buffer->value); if (bufferSize - bufferUsedSize < 2) { bufferSize *= 2; buffer = resizeArray(buffer, bufferSize); } } bufferUsedSize = strlen(buffer->value); EmojicodeInteger len = u8_strlen_l(buffer->value, bufferUsedSize); Object *so = newObject(CL_STRING); stackSetVariable(0, somethingObject(so), thread); String *string = so->value; string->length = len; Object *chars = newArray(len * sizeof(EmojicodeChar)); string = stackGetVariable(0, thread).object->value; string->characters = chars; u8_toucs(characters(string), len, buffer->value, bufferUsedSize); return stackGetVariable(0, thread); }
UGenArray UGenArray::operator- () const throw() { if(internal->size() == 0) return UGenArray(); UGenArray newArray(internal->size()); for(int i = 0; i < internal->size(); i++) { newArray.internal->getArray()[i] = internal->getArray()[i].neg(); } return newArray; }
static void como_compile_ast(ast_node *p, const char *filename) { Object *main_code = newArray(4); global_frame = create_frame(main_code); global_frame->filename = newString(filename); mapInsertEx(global_frame->cf_symtab, "__FUNCTION__", newString("__main__")); (void)como_compile(p, global_frame); arrayPushEx(main_code, newPointer((void *)create_op(HALT, NULL))); (void)como_execute(global_frame, NULL); }