bool getBuffer(const uint32 vertexSize, const uint32 indexSize, Vertex2D** pVertex, IndexType** pIndices, IndexType* indexOffset, D3D11Render2DCommandManager& commandManager) { // VB const uint32 requiredVertexSize = m_vertexArrayWritePos + vertexSize; if (m_vertices.size() < requiredVertexSize) { if (MaxVertexSize < requiredVertexSize) { return false; } resizeVertices(requiredVertexSize); } // IB const uint32 requiredIndexSize = m_indexArrayWritePos + indexSize; if (m_indices.size() < requiredIndexSize) { if (MaxIndexSize < requiredIndexSize) { return false; } resizeIndices(requiredIndexSize); } if (VertexBufferSize < (m_batches.back().vertexPos + vertexSize) || IndexBufferSize < (m_batches.back().indexPos + indexSize)) { m_batches.emplace_back(); commandManager.pushNextBatch(); } *pVertex = m_vertices.data() + m_vertexArrayWritePos; *pIndices = m_indices.data() + m_indexArrayWritePos; *indexOffset = m_batches.back().vertexPos; m_vertexArrayWritePos += vertexSize; m_indexArrayWritePos += indexSize; m_batches.back().vertexPos += vertexSize; m_batches.back().indexPos += indexSize; return true; }
int main() { Array <int> test; for (int i = 0; i < 10; ++i) { test.push_back (i); } for (int i = 0; i < test.size(); ++i) { cout << test[i] << " "; } cout << endl; Array <int> test1 (test); for (int i = 0; i < test1.size(); ++i) { cout << test1[i] << " "; } cout << endl; Array <int> test2 = test1; for (int i = 0; i < test2.size(); ++i) { cout << test2[i] << " "; } cout << endl; test.pop_back(); for (int i = 0; i < test.size(); ++i) { cout << test[i] << " "; } cout << endl; cout << test.front() << " " << test.back() << endl; cout << (test1 == test2) << " " << (test == test1) << endl; }
TEST(ArrayTest, testOperations) { const int SIZE = 6; typedef Poco::Array<int,SIZE> Array; Array a = { { 1 } }; // use some common STL container operations EXPECT_TRUE(a.size() == SIZE); EXPECT_TRUE(a.max_size() == SIZE); EXPECT_TRUE(a.empty() == false); EXPECT_TRUE(a.front() == a[0]); EXPECT_TRUE(a.back() == a[a.size()-1]); //EXPECT_TRUE(a.data() == &a[0]); // assign a.assign(100); for(int i = 0; i<a.size(); i++){ EXPECT_TRUE(a[i] == 100); } // swap Array b; b.assign(10); for(int i=0; i<SIZE; i++){ EXPECT_TRUE(a[i] == 100); EXPECT_TRUE(b[i] == 10); } a.swap(b); for(int i=0; i<SIZE; i++){ EXPECT_TRUE(a[i] == 10); EXPECT_TRUE(b[i] == 100); } }
static void sampleBezier(float max_life, const Array<Vec2>& values, Array<float>& sampled) { ASSERT(values.size() >= 6); ASSERT(values[values.size() - 2].x - values[1].x > 0); static const int SAMPLES_PER_SECOND = 10; sampled.resize(int(max_life * SAMPLES_PER_SECOND)); float x_range = values[values.size() - 2].x - values[1].x; int last_idx = 0; for (int i = 1; i < values.size() - 3; i += 3) { int step_count = int(5 * sampled.size() * ((values[i + 3].x - values[i].x) / x_range)); float t_step = 1.0f / (float)step_count; for (int i_step = 1; i_step <= step_count; i_step++) { float t = t_step * i_step; float u = 1.0f - t; float w1 = u * u * u; float w2 = 3 * u * u * t; float w3 = 3 * u * t * t; float w4 = t * t * t; auto p = values[i] * (w1 + w2) + values[i + 1] * w2 + values[i + 2] * w3 + values[i + 3] * (w3 + w4); int idx = int(sampled.size() * ((p.x - values[1].x) / x_range)); ASSERT(idx <= last_idx + 1); last_idx = idx; sampled[idx >= sampled.size() ? sampled.size() - 1 : idx] = p.y; } } sampled[0] = values[0].y; sampled.back() = values[values.size() - 2].y; }
void printModelCalibration( const std::vector<boost::shared_ptr<CalibrationHelper> > &basket, const Array &volatility) { std::cout << "\n" << std::left << std::setw(20) << "Expiry" << std::setw(14) << "Model sigma" << std::setw(20) << "Model price" << std::setw(20) << "market price" << std::setw(14) << "Model ivol" << std::setw(14) << "Market ivol" << std::fixed << std::setprecision(6) << std::endl; std::cout << "====================" "====================" "====================" "====================" "====================" << std::endl; for (Size j = 0; j < basket.size(); ++j) { boost::shared_ptr<SwaptionHelper> helper = boost::dynamic_pointer_cast<SwaptionHelper>(basket[j]); Date expiry = helper->swaption()->exercise()->date(0); std::ostringstream expiryString; expiryString << expiry; std::cout << std::setw(20) << expiryString.str() << std::setw(14) << volatility[j] << std::setw(20) << basket[j]->modelValue() << std::setw(20) << basket[j]->marketValue() << std::setw(14) << basket[j]->impliedVolatility(basket[j]->modelValue(), 1E-6, 1000, 0.0, 2.0) << std::setw(14) << basket[j]->volatility()->value() << std::endl; } if (volatility.size() > basket.size()) // only for markov model std::cout << std::setw(20) << " " << volatility.back() << std::endl; }
godot_variant GDAPI godot_array_back(const godot_array *p_arr) { Array *a = (Array *)p_arr; godot_variant v; Variant *val = (Variant *)&v; memnew_placement(val, Variant); *val = a->back(); return v; }
inline bool CheckDebugThrows(Array& Arr) { try { Arr.back(); } catch (std::__libcpp_debug_exception const&) { return true; } return false; }
Process Process::popen(StringRef exe, ArrayRef<StringRef> arguments) { Process p; int in[2]; int out[2]; int err[2]; int pid; pipe(in); pipe(out); pipe(err); pid = fork(); if (pid > 0) { // parent: set up pipes ::close(in[0]); ::close(out[1]); ::close(err[1]); p.impl->pid = pid; p.impl->stdin_raw = OutputPipeStream(in[1]); p.impl->stdout = InputPipeStream(out[0]); p.impl->stderr = InputPipeStream(err[0]); p.impl->status = Status::Running; } else if (pid == 0) { // transform arguments into a format that execvp can understand COPY_STRING_REF_TO_CSTR_BUFFER(exe_cstr, exe); Array<String> args; Array<const char*> argv; args.reserve(arguments.size()+1); argv.reserve(arguments.size()+2); argv.push_back(exe_cstr.data()); for (auto& arg: arguments) { args.emplace_back(String(arg) + '\0'); argv.emplace_back(args.back().data()); } argv.push_back(nullptr); // child: replace stdin/stdout/stderr ::close(in[1]); ::close(out[0]); ::close(err[0]); ::close(0); dup(in[0]); ::close(1); dup(out[1]); ::close(2); dup(err[1]); ::execvp(exe_cstr.data(), (char* const*)argv.data()); ::perror("execvp"); exit(1); } else { raise<PipeError>("fork: {0}", ::strerror(errno)); } return std::move(p); }
void createPrimitiveMethod(Module& module, const SEM::TypeInstance* const typeInstance, SEM::Function* const semFunction, llvm::Function& llvmFunction) { const auto argInfo = getFunctionArgInfo(module, semFunction->type()); Function function(module, llvmFunction, argInfo, &(module.templateBuilder(TemplatedObject::TypeInstance(typeInstance)))); const auto debugSubprogram = genDebugFunctionInfo(module, semFunction, &llvmFunction); assert(debugSubprogram); function.attachDebugInfo(*debugSubprogram); function.setDebugPosition(semFunction->debugInfo()->scopeLocation.range().start()); SEM::ValueArray templateArgs; for (const auto& templateVar: semFunction->templateVariables()) { templateArgs.push_back(templateVar->selfRefValue()); } PendingResultArray args; const auto contextValue = argInfo.hasContextArgument() ? function.getContextValue(typeInstance) : nullptr; RefPendingResult contextPendingResult(contextValue, typeInstance->selfType()); if (argInfo.hasContextArgument()) { args.push_back(contextPendingResult); } // Need an array to store all the pending results // being referred to in 'genTrivialPrimitiveFunctionCall'. Array<ValuePendingResult, 10> pendingResultArgs; const auto& argTypes = semFunction->type().parameterTypes(); for (size_t i = 0; i < argTypes.size(); i++) { const auto argValue = function.getArg(i); pendingResultArgs.push_back(ValuePendingResult(argValue, argTypes[i])); args.push_back(pendingResultArgs.back()); } MethodInfo methodInfo(typeInstance->selfType(), semFunction->name().last(), semFunction->type(), std::move(templateArgs)); const auto hintResultValue = argInfo.hasReturnVarArgument() ? function.getReturnVar() : nullptr; const auto result = genTrivialPrimitiveFunctionCall(function, std::move(methodInfo), std::move(args), hintResultValue); const auto returnType = semFunction->type().returnType(); // Return the result in the appropriate way. if (argInfo.hasReturnVarArgument()) { genMoveStore(function, result, function.getReturnVar(), returnType); function.getBuilder().CreateRetVoid(); } else if (!returnType->isBuiltInVoid()) { function.returnValue(result); } else { function.getBuilder().CreateRetVoid(); } // Check the generated function is correct. function.verify(); }
int main () { printf ("Results of tr1_array5_test:\n"); // define special type name typedef array<float,6> Array; // create and initialize an array const Array a = { { 42.42f } }; // use some common STL container operations printf ("static_size: %lu\n", a.size ()); printf ("size: %lu\n", a.size ()); // Can't use std::boolalpha because it isn't portable printf ("empty: %s\n", (a.empty()? "true" : "false")); printf ("max_size: %lu\n", a.max_size ()); printf ("front: %f\n", a.front ()); printf ("back: %f\n", a.back ()); printf ("[0]: %f\n", a[0]); printf ("elems: "); // iterate through all elements for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) printf ("%f ", *pos); printf ("\n"); test_static_size(a); // check copy constructor and assignment operator Array b(a); Array c; c = a; if (a==b && a==c) printf ("copy construction and copy assignment are OK\n"); else printf ("copy construction and copy assignment are BROKEN\n"); typedef array<double,6> DArray; typedef array<int,6> IArray; IArray ia = { { 1, 2, 3, 4, 5, 6 } } ; // extra braces silence GCC warning DArray da; da = ia; da.assign (42); return 0; }
int main() { std_log(LOG_FILENAME_LINE,"[Test Case for array1]"); // define special type name typedef boost::array<float,6> Array; // create and initialize an array Array a = { { 42 } }; // access elements for (unsigned i=1; i<a.size(); ++i) { a[i] = a[i-1]+1; } // use some common STL container operations std::cout << "size: " << a.size() << std::endl; std::cout << "empty: " << (a.empty() ? "true" : "false") << std::endl; std::cout << "max_size: " << a.max_size() << std::endl; std::cout << "front: " << a.front() << std::endl; std::cout << "back: " << a.back() << std::endl; std::cout << "elems: "; // iterate through all elements for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) { std::cout << *pos << ' '; } std::cout << std::endl; // check copy constructor and assignment operator Array b(a); Array c; c = a; if (a==b && a==c) { std::cout << "copy construction and copy assignment are OK" << std::endl; std_log(LOG_FILENAME_LINE,"Result : Passed"); } else { std::cout << "copy construction and copy assignment FAILED" << std::endl; std_log(LOG_FILENAME_LINE,"Result : Failed"); assert_failed = true; } testResultXml("array1"); close_log_file(); return 0; // makes Visual-C++ compiler happy }
TEST(jsonArray, OtherOperations) { Array arr = {1, 2, 3, 4, 5}; jsonat::Value k = 0; for (auto x : arr) { ++k; EXPECT_EQ(k, x); } EXPECT_EQ(arr[4], arr.size()); EXPECT_EQ(1, arr[0]); EXPECT_EQ(short(2), arr[1]); EXPECT_EQ(arr[2], (unsigned long long)(arr[arr[2] - 2] + 1)); arr.addValue("hello worl"); EXPECT_EQ(arr.back() + 'd', "hello world"); EXPECT_GT(arr[3], arr[2]); }
const A& back() const { return arr.back(); }
A& back() { return arr.back(); }
// Compute the convex hull using Graham Scan. void nv::convexHull(const Array<Vector2> & input, Array<Vector2> & output, float epsilon/*=0*/) { const uint inputCount = input.count(); Array<float> coords; coords.resize(inputCount); for (uint i = 0; i < inputCount; i++) { coords[i] = input[i].x; } RadixSort radix; radix.sort(coords); const uint * ranks = radix.ranks(); Array<Vector2> top(inputCount); Array<Vector2> bottom(inputCount); Vector2 P = input[ranks[0]]; Vector2 Q = input[ranks[inputCount-1]]; float topy = max(P.y, Q.y); float boty = min(P.y, Q.y); for (uint i = 0; i < inputCount; i++) { Vector2 p = input[ranks[i]]; if (p.y >= boty) top.append(p); } for (uint i = 0; i < inputCount; i++) { Vector2 p = input[ranks[inputCount-1-i]]; if (p.y <= topy) bottom.append(p); } // Filter top list. output.clear(); output.append(top[0]); output.append(top[1]); for (uint i = 2; i < top.count(); ) { Vector2 a = output[output.count()-2]; Vector2 b = output[output.count()-1]; Vector2 c = top[i]; float area = triangleArea(a, b, c); if (area >= -epsilon) { output.popBack(); } if (area < -epsilon || output.count() == 1) { output.append(c); i++; } } uint top_count = output.count(); output.append(bottom[1]); // Filter bottom list. for (uint i = 2; i < bottom.count(); ) { Vector2 a = output[output.count()-2]; Vector2 b = output[output.count()-1]; Vector2 c = bottom[i]; float area = triangleArea(a, b, c); if (area >= -epsilon) { output.popBack(); } if (area < -epsilon || output.count() == top_count) { output.append(c); i++; } } // Remove duplicate element. nvDebugCheck(output.front() == output.back()); output.popBack(); }
void test_array(){ OPH(); DEBUG("testing array ......"); Array* a =SafeNew<Array>(); for(int64_t i=0; i<14; ++i){ a->push_back(SafeNew<Int32>()); } for(int64_t i=0; i<14; ++i){ a->pop_back(); } ASSERT(a->empty()); // common array { Array* arr =SafeNew<Array>(); // push_back, size, pop_front, front for(int i=0; i<100; ++i){ arr->push_back(String::Format("%d", i)); } for(int i=0; i<100; ++i){ CHECK_EXIT(((String*)(arr->front()))->is(String::Format("%d", i)), 1); arr->pop_front(); } CHECK_EXIT(arr->size()==0, 1); // push_front, size, pop_back, back for(int i=99; i>=0; --i){ arr->push_front(String::Format("%d", i)); } CHECK_EXIT(arr->size()==100, 1); for(int i=99; i>=0; --i){ CHECK_EXIT(((String*)(arr->back()))->is(String::Format("%d", i)), 1); arr->pop_back(); } CHECK_EXIT(arr->size()==0, 1); // insert, remove for(int i=0; i<100; ++i){ arr->push_back(SafeNew<Int64, int64_t>(i)); } arr->push_front(SafeNew<Int64, int64_t>(-1)); arr->push_back(SafeNew<Int64, int64_t>(100)); for(int i=0; i<102; ++i){ CHECK_EXIT(((Int64*)(arr->get(i)))->getValue() == i-1, 1); } arr->insert(50, SafeNew<Int64, int64_t>(9999)); CHECK_EXIT(((Int64*)(arr->get(50)))->getValue() == 9999, 1); CHECK_EXIT(((Int64*)(arr->get(51)))->getValue() == 49, 1); CHECK_EXIT(((Int64*)(arr->get(49)))->getValue() == 48, 1); arr->remove(102); arr->remove(50); arr->remove(0); for(int i=0; i<100; ++i){ CHECK_EXIT(((Int64*)(arr->front()))->getValue() == i, 1); arr->pop_front(); } CHECK_EXIT(arr->size()==0, 1); } // int64 array { Int64Array* arr =SafeNew<Int64Array>(); // push_back, size, pop_front, front for(int i=0; i<100; ++i){ arr->push_back(i); } for(int i=0; i<100; ++i){ CHECK_EXIT(arr->front() == i, 1); arr->pop_front(); } CHECK_EXIT(arr->size()==0, 1); // push_front, size, pop_back, back for(int i=99; i>=0; --i){ arr->push_front(i); } CHECK_EXIT(arr->size()==100, 1); for(int i=99; i>=0; --i){ CHECK_EXIT(arr->back() == i, 1); arr->pop_back(); } CHECK_EXIT(arr->size()==0, 1); // insert, remove for(int i=0; i<100; ++i){ arr->push_back(i); } arr->push_front(-1); arr->push_back(100); for(int i=0; i<102; ++i){ CHECK_EXIT(arr->get(i) == i-1, 1); } arr->insert(50, 9999); CHECK_EXIT(arr->get(50) == 9999, 1); CHECK_EXIT(arr->get(51) == 49, 1); CHECK_EXIT(arr->get(49) == 48, 1); arr->remove(102); arr->remove(50); arr->remove(0); for(int i=0; i<100; ++i){ CHECK_EXIT(arr->front() == i, 1); arr->pop_front(); } CHECK_EXIT(arr->size()==0, 1); } }
void Main() { ResourceLoader resources; auto sampler = SamplerState(SamplerState::Default2D); sampler.filter = TextureFilter::MinMagMipPoint; Graphics2D::SetSamplerState(sampler); Window::Resize(320 * 2, 240 * 2); Window::SetTitle(L"まちへかえろう(仮)"); Array<HurdleObject> hurdles; Array<BuildingLayer> buildingLayers; Array<BuildingLayer2> buildingLayers2; Map::instance().init(40, 15); for (int i = 0; i < 40; i++) { Map::instance().set(i, 14, 5); Map::instance().set(i, 13, 5); Map::instance().set(i, 12, 4); Map::instance().set(i, 11, 3); Map::instance().set(i, 10, 3); Map::instance().set(i, 9, 2); Map::instance().set(i, 8, 1); } auto viewLeft = 0.0; auto mycharDead = false; ScreenState currentState = ScreenState::InTitle, prevState = currentState; bool prevSpacePressed = false; auto TitleTimer = TimerMillisec(); ScriptEngine asEngine; asEngine.RegisterMethod(L"int getAt(double x, double y)", &Map::getAt, Map::instance()); asEngine.RegisterProperty(L"double _viewX", &viewLeft); asEngine.RegisterProperty(L"bool _isGameOvered", &mycharDead); asEngine.RegisterFunction(L"void playJumpSound()", PlayJumpSound); auto context_mychar = asEngine.CreateContextFromScript(L"res/scripts/Mychar.as"); Record::instance().init(); int fc = 0; while (System::Update()) { bool spacePressedInFrame = !prevSpacePressed && Input::KeySpace.pressed; prevSpacePressed = Input::KeySpace.pressed; asEngine.setFrameCount(fc); switch (currentState) { case ScreenState::InTitle: if (!TitleTimer.isActive) TitleTimer.start(); renderBackgrounds(viewLeft, resources); renderBuildingLayers(buildingLayers, buildingLayers2, resources); renderMap(viewLeft, resources); renderTitle(TitleTimer.elapsed(), resources); renderCurrentMax(); if (spacePressedInFrame) { // GameInit SoundAsset(SoundResourceLoader::decide).play(); viewLeft = 0.0; mycharDead = false; context_mychar.Reenter(); buildingLayers.clear(); buildingLayers2.clear(); hurdles.clear(); currentState = ScreenState::Gaming; } break; case ScreenState::Gaming: // appear hurdles at random if (RandomBool(0.02)) { if (hurdles.empty() || hurdles.back().getPos().x - (viewLeft + 320.0f) < -48.0f) { // allocate space least 48 logical-pixels hurdles.emplace_back(HurdleObject(Vec2(viewLeft + 320.0f, 8.0f * 16.0f + 8.0f))); } } // appear building at random if (RandomBool(0.02 / 30.0)) { if (buildingLayers.empty() || buildingLayers.back().getPos().x < 0.0f) { buildingLayers.emplace_back(BuildingLayer(Vec2(320.0f, -60.0f))); } } if (RandomBool(0.01 / 30.0)) { if (buildingLayers2.empty() || buildingLayers2.back().getPos().x < 0.0f) { buildingLayers2.emplace_back(BuildingLayer2(Vec2(320.0f, -60.0f))); } } asEngine.setFrameCount(fc); context_mychar.Execute(); renderBackgrounds(viewLeft, resources); renderBuildingLayers(buildingLayers, buildingLayers2, resources); renderMap(viewLeft, resources); if (renderHurdlesAndHitTest(viewLeft, context_mychar.GetGlobalDoubleVec2(L"x", L"y"), hurdles, resources)) { if (!mycharDead) SoundAsset(SoundResourceLoader::died).play(); mycharDead = true; } renderPlayer(viewLeft, context_mychar.GetGlobalDouble(L"angle"), context_mychar.GetGlobalDoubleVec2(L"x", L"y"), resources); renderInfo(toScore(context_mychar.GetGlobalDouble(L"x"))); // Host Processes if (mycharDead && context_mychar.GetGlobalDouble(L"x") < viewLeft - 120.0f) { // update record Record::instance().recordScore(toScore(context_mychar.GetGlobalDouble(L"x"))); currentState = ScreenState::Result; } break; case ScreenState::Result: renderBackgrounds(viewLeft, resources); renderBuildingLayers(buildingLayers, buildingLayers2, resources); renderMap(viewLeft, resources); renderHurdlesAndHitTest(viewLeft, context_mychar.GetGlobalDoubleVec2(L"x", L"y"), hurdles, resources); renderResult(toScore(context_mychar.GetGlobalDouble(L"x")), Record::instance().isUpdated(), fc); if (spacePressedInFrame) { // GameInit SoundAsset(SoundResourceLoader::decide).play(); TitleTimer.reset(); currentState = ScreenState::InTitle; } break; } if (prevState != currentState) { fc = 0; } prevState = currentState; // Common Postprocesses fc++; viewLeft += 2.5f; } Map::instance().clean(); }