//------------------------------------------------------------------------------------- PyObject* FixedArray::__py_count(PyObject* self, PyObject* args, PyObject* kwargs) { FixedArray* ary = static_cast<FixedArray*>(self); PyObject* pyItem = PyTuple_GetItem(args, 0); int count = 0, cur; for (uint32 i = 0; (cur = ary->findFrom(i, &*pyItem)) >= 0; i = cur + 1) ++count; return PyLong_FromLong(count); }
void CodeBlockHash::dump(PrintStream& out) const { FixedArray<char, 7> buffer = integerToSixCharacterHashString(m_hash); #if !ASSERT_DISABLED CodeBlockHash recompute(buffer.data()); ASSERT(recompute == *this); #endif // !ASSERT_DISABLED out.print(buffer.data()); }
TEST(Array, ConstructionTest) { ConstructionTest::Reset(); FixedArray<20, ConstructionTest> array; EXPECT_EQ(0, ConstructionTest::Count()); for(int i = 0; i<10; i++) { array.push_back( ); EXPECT_EQ(i+1, ConstructionTest::Count()); } }
//------------------------------------------------------------------------------------- PyObject* FixedArray::__py_index(PyObject* self, PyObject* args, PyObject* kwargs) { FixedArray* ary = static_cast<FixedArray*>(self); PyObject* pyItem = PyTuple_GetItem(args, 0); int index = ary->findFrom(0, &*pyItem); if (index == -1) { PyErr_SetString(PyExc_ValueError, "FixedArray::index: value not found"); return NULL; } return PyLong_FromLong(index); }
NODE_IMPLEMENTATION(transpose_mXX, Pointer) { FixedArray* Aarray = NODE_ARG_OBJECT(0, FixedArray); const Class* mtype = static_cast<const Class*>(Aarray->type()); FixedArray* Carray = static_cast<FixedArray*>(ClassInstance::allocate(mtype)); EigenMatXf A(Aarray->data<float>(), Aarray->size(0), Aarray->size(1)); EigenMatXf C(Carray->data<float>(), Aarray->size(0), Aarray->size(1)); C = A.transpose(); NODE_RETURN(Carray); }
NODE_IMPLEMENTATION(inverse_m44, Pointer) { FixedArray* Aarray = NODE_ARG_OBJECT(0, FixedArray); const Class* mtype = static_cast<const Class*>(Aarray->type()); FixedArray* Carray = static_cast<FixedArray*>(ClassInstance::allocate(mtype)); EigenMat44f A(Aarray->data<float>()); EigenMat44f C(Carray->data<float>()); C = A.inverse(); NODE_RETURN(Carray); }
//------------------------------------------------------------------------------------- PyObject* FixedArray::__py_remove(PyObject* self, PyObject* args, PyObject* kwargs) { FixedArray* ary = static_cast<FixedArray*>(self); PyObject* pyItem = PyTuple_GetItem(args, 0); int index = ary->findFrom(0, &*pyItem); if (index == -1) { PyErr_SetString(PyExc_ValueError, "FixedArray.remove: value not found"); return PyLong_FromLong(-1); } PyObject* pyTuple = PyTuple_New(0); return PyBool_FromLong(seq_ass_slice(self, index, index + 1, &*pyTuple) == 0); }
NODE_IMPLEMENTATION(mult_m33_m33, Pointer) { FixedArray* Aarray = NODE_ARG_OBJECT(0, FixedArray); FixedArray* Barray = NODE_ARG_OBJECT(1, FixedArray); const Class* mtype = static_cast<const Class*>(Aarray->type()); FixedArray* Carray = static_cast<FixedArray*>(ClassInstance::allocate(mtype)); EigenMat33f A(Aarray->data<float>()); EigenMat33f B(Barray->data<float>()); EigenMat33f C(Carray->data<float>()); C = A * B; NODE_RETURN(Carray); }
NODE_IMPLEMENTATION(mult_m44_v3, Vector3f) { FixedArray* Aarray = NODE_ARG_OBJECT(0, FixedArray); Mu::Vector3f v = NODE_ARG(1, Mu::Vector3f); const Class* mtype = static_cast<const Class*>(Aarray->type()); const float* m = Aarray->data<float>(); float x = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3]; float y = m[4] * v[0] + m[5] * v[1] + m[6] * v[2] + m[7]; float z = m[8] * v[0] + m[9] * v[1] + m[10] * v[2] + m[11]; float w = m[12] * v[0] + m[13] * v[1] + m[14] * v[2] + m[15]; NODE_RETURN(newVector(x / w, y / w, z / w)); }
NODE_IMPLEMENTATION(mult_m44_v4, Vector4f) { FixedArray* Aarray = NODE_ARG_OBJECT(0, FixedArray); Mu::Vector4f v = NODE_ARG(1, Mu::Vector4f); const Class* mtype = static_cast<const Class*>(Aarray->type()); const float* m = Aarray->data<float>(); Vector4f r = newVector( m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3] * v[3], m[4] * v[0] + m[5] * v[1] + m[6] * v[2] + m[7] * v[3], m[8] * v[0] + m[9] * v[1] + m[10] * v[2] + m[11] * v[3], m[12] * v[0] + m[13] * v[1] + m[14] * v[2] + m[15] * v[3] ); NODE_RETURN(r); }
virtual kernel::ApplicationResult startup( kernel::Params & params ) { #if 0 printf( "Memory Test: \n" ); Test * a = CREATE(Test); std::vector<int, GeminiAllocator<int> > int_vector; int_vector.push_back( 30 ); int_vector.push_back( 16 ); int_vector.push_back( 31 ); typedef std::map<int, int, std::less<int>, GeminiAllocator<int> > MyMap; MyMap int_map; int_map.insert( MyMap::value_type( 30, 17 ) ); int_map.insert( MyMap::value_type( 16, 100) ); int_map.insert( MyMap::value_type( 300, 2 ) ); // Test FixedArray - which should cause derived destructors // to be called as well as the BaseObject's destructors. FixedArray<BaseObject> objects; objects.allocate(3); for( size_t i = 0; i < 3; ++i ) { objects[i] = CREATE(DerivedObject); objects[i]->type = i+4; } objects.purge();# // added z-modifer to satisfy Xcode, C99 addition, we'll see who doesn't support it :) printf( "totalAllocations: %zu, totalBytes: %zu\n", memory::allocator().total_allocations(), memory::allocator().total_bytes() ); printf( "activeAllocations: %zu, activeBytes: %zu\n", memory::allocator().active_allocations(), memory::allocator().active_bytes() ); DESTROY(Test, a); printf( "activeAllocations: %zu, activeBytes: %zu\n", memory::allocator().active_allocations(), memory::allocator().active_bytes() ); #endif PooledObject * p = new DerivedPoolObject(); delete p; return kernel::Application_NoWindow; }
void CSceneNodeAABB::DrawFace(const FixedArray<vector3,4> &r,unsigned int divA,unsigned int divB) const{ glPushAttrib(GL_ALL_ATTRIB_BITS); glEnable(GL_MAP2_VERTEX_3); glEnable(GL_AUTO_NORMAL); glMap2f( GL_MAP2_VERTEX_3,0.0f,1.0f,3,2,0.0f,1.0f,6,2,(GLfloat*)r.PtrBegin()); GLenum err(glGetError()); glMapGrid2f(divA,0,1,divB,0,1); err = glGetError(); glEvalMesh2(GL_FILL,0,divA,0,divB); err = glGetError(); glPopAttrib(); }
//------------------------------------------------------------------------------------- PyObject* FixedArray::__unpickle__(PyObject* self, PyObject* args) { Py_ssize_t size = PyTuple_Size(args); if(size != 2) { ERROR_MSG("FixedArray::__unpickle__: args is wrong! (size != 2)"); S_Return; } PyObject* pyDatatypeUID = PyTuple_GET_ITEM(args, 0); DATATYPE_UID uid = (DATATYPE_UID)PyLong_AsUnsignedLong(pyDatatypeUID); PyObject* pyList = PyTuple_GET_ITEM(args, 1); if(pyList == NULL) { ERROR_MSG("FixedArray::__unpickle__: args is wrong!"); S_Return; } FixedArray* pFixedArray = new FixedArray(DataTypes::getDataType(uid)); pFixedArray->initialize(pyList); return pFixedArray; }
//---------------------------------------------------------------------------// FixedArray<glm::vec3, 8u> CameraComponent::getWSfrustumCorners(float aViewportAspectRatio) { //calculate frustum corner coordinates float fFov2 = getFovRad() * 0.5f; float tanFov2 = glm::tan( fFov2 ); float h2Far = tanFov2 * m_fFar; float h2Near = tanFov2 * m_fNear; float hFar = 2.0f * h2Far; float hNear = 2.0f * h2Near; Rendering::RenderOutput* renderer = Fancy::GetCurrentRenderOutput(); float aspect = aViewportAspectRatio; float w2Far = ( hFar * aspect ) / 2.0f; float w2Near = ( hNear * aspect ) / 2.0f; glm::vec3 v3Corners[ 8 ]; v3Corners[ 0 ] = glm::vec3( -1.0f * w2Near, -1.0f * h2Near, -m_fNear ); //l,b,n v3Corners[ 1 ] = glm::vec3( 1.0f * w2Near, -1.0f * h2Near, -m_fNear ); //r,b,n v3Corners[ 2 ] = glm::vec3( 1.0f * w2Near, 1.0f * h2Near, -m_fNear ); //r,t,n v3Corners[ 3 ] = glm::vec3( -1.0f * w2Near, 1.0f * h2Near, -m_fNear ); //l,t,n v3Corners[ 4 ] = glm::vec3( -1.0f * w2Far, -1.0f * h2Far, -m_fFar ); //l,b,n v3Corners[ 5 ] = glm::vec3( 1.0f * w2Far, -1.0f * h2Far, -m_fFar ); //r,b,n v3Corners[ 6 ] = glm::vec3( 1.0f * w2Far, 1.0f * h2Far, -m_fFar ); //r,t,n v3Corners[ 7 ] = glm::vec3( -1.0f * w2Far, 1.0f * h2Far, -m_fFar ); //l,t,n FixedArray<glm::vec3, 8u> vReturnCorners; vReturnCorners.resize(8); //transform each corner into WS for( int i = 0; i < 8; ++i ) { vReturnCorners[i] = glm::vec3( m_matViewInv * glm::vec4( v3Corners[ i ], 1.0f ) ); } return vReturnCorners; }
TEST(FixedArray, trivial){ FixedArray array; EXPECT_EQ(0, array.num()); EXPECT_EQ(0, array.len()); for (uint64_t i = 0; i < 10; ++i){ array.PushBack(i); } for (uint64_t i = 10; ; --i){ array.PushBack(i); if (i == 0) break; } for (uint64_t i = 0; i < 10; ++i){ EXPECT_EQ(i, array.Get(i)); } for (uint64_t i = 0; i < 10; ++i){ EXPECT_EQ(10 - i, array.Get(i+10)); } }
TEST(FixedArray, random){ for (int iter = 0; iter < 10; ++iter){ const uint64_t N = rand() % 10000; const uint64_t shift = rand() % 32; const uint64_t val_max = 1LLU << shift; FixedArray array; vector<uint64_t> vals; uint64_t max_val = 0; for (uint64_t i = 0; i < N; ++i){ uint64_t v = rand() % val_max; if (max_val < v){ max_val = v; } array.PushBack(v); vals.push_back(v); } uint64_t len = 0; for (; len < 64 && max_val >> len; ++len){ } ASSERT_EQ(N, array.num()); ASSERT_EQ(len, array.len()); for (uint64_t i = 0; i < N; ++i){ ASSERT_EQ(vals[i], array.Get(i)); } ostringstream os; array.Write(os); FixedArray array2; istringstream is(os.str()); array2.Read(is); ASSERT_EQ(N, array2.num()); ASSERT_EQ(len, array2.len()); for (uint64_t i = 0; i < N; ++i){ ASSERT_EQ(vals[i], array2.Get(i)); } } }
bool operator<(const FixedArray<N, T>& other) const { return std::lexicographical_compare(begin(), end(), other.begin(), other.end()); }
//------------------------------------------------------------------------------------- PyObject* FixedArray::__py_append(PyObject* self, PyObject* args, PyObject* kwargs) { FixedArray* ary = static_cast<FixedArray*>(self); uint32 seq_size = ary->length(); return PyBool_FromLong(seq_ass_slice(self, seq_size, seq_size, &*args) == 0); }
OpcodeStats::~OpcodeStats() { long long totalInstructions = 0; for (int i = 0; i < numOpcodeIDs; ++i) totalInstructions += opcodeCounts[i]; long long totalInstructionPairs = 0; for (int i = 0; i < numOpcodeIDs; ++i) for (int j = 0; j < numOpcodeIDs; ++j) totalInstructionPairs += opcodePairCounts[i][j]; FixedArray<int, numOpcodeIDs> sortedIndices; for (int i = 0; i < numOpcodeIDs; ++i) sortedIndices[i] = i; qsort(sortedIndices.data(), numOpcodeIDs, sizeof(int), compareOpcodeIndices); pair<int, int> sortedPairIndices[numOpcodeIDs * numOpcodeIDs]; pair<int, int>* currentPairIndex = sortedPairIndices; for (int i = 0; i < numOpcodeIDs; ++i) for (int j = 0; j < numOpcodeIDs; ++j) *(currentPairIndex++) = make_pair(i, j); qsort(sortedPairIndices, numOpcodeIDs * numOpcodeIDs, sizeof(pair<int, int>), compareOpcodePairIndices); printf("\nExecuted opcode statistics\n"); printf("Total instructions executed: %lld\n\n", totalInstructions); printf("All opcodes by frequency:\n\n"); for (int i = 0; i < numOpcodeIDs; ++i) { int index = sortedIndices[i]; printf("%s:%s %lld - %.2f%%\n", opcodeNames[index], padOpcodeName((OpcodeID)index, 28), opcodeCounts[index], ((double) opcodeCounts[index]) / ((double) totalInstructions) * 100.0); } printf("\n"); printf("2-opcode sequences by frequency: %lld\n\n", totalInstructions); for (int i = 0; i < numOpcodeIDs * numOpcodeIDs; ++i) { pair<int, int> indexPair = sortedPairIndices[i]; long long count = opcodePairCounts[indexPair.first][indexPair.second]; if (!count) break; printf("%s%s %s:%s %lld %.2f%%\n", opcodeNames[indexPair.first], padOpcodeName((OpcodeID)indexPair.first, 28), opcodeNames[indexPair.second], padOpcodeName((OpcodeID)indexPair.second, 28), count, ((double) count) / ((double) totalInstructionPairs) * 100.0); } printf("\n"); printf("Most common opcodes and sequences:\n"); for (int i = 0; i < numOpcodeIDs; ++i) { int index = sortedIndices[i]; long long opcodeCount = opcodeCounts[index]; double opcodeProportion = ((double) opcodeCount) / ((double) totalInstructions); if (opcodeProportion < 0.0001) break; printf("\n%s:%s %lld - %.2f%%\n", opcodeNames[index], padOpcodeName((OpcodeID)index, 28), opcodeCount, opcodeProportion * 100.0); for (int j = 0; j < numOpcodeIDs * numOpcodeIDs; ++j) { pair<int, int> indexPair = sortedPairIndices[j]; long long pairCount = opcodePairCounts[indexPair.first][indexPair.second]; double pairProportion = ((double) pairCount) / ((double) totalInstructionPairs); if (!pairCount || pairProportion < 0.0001 || pairProportion < opcodeProportion / 100) break; if (indexPair.first != index && indexPair.second != index) continue; printf(" %s%s %s:%s %lld - %.2f%%\n", opcodeNames[indexPair.first], padOpcodeName((OpcodeID)indexPair.first, 28), opcodeNames[indexPair.second], padOpcodeName((OpcodeID)indexPair.second, 28), pairCount, pairProportion * 100.0); } } printf("\n"); }
void TextureLoader::LoadTextureCube(const char * filenames[6], DescTextureCube & texture, FixedArray<unsigned char> & texture_data) { int texture_width = 0; int texture_height = 0; int texture_channels = 0; unsigned int pixel_format; unsigned int total_face_bytes = 0; unsigned int dest_offset = 0; for (unsigned int i = 0; i < 6; ++i) { std::string file(filenames[i]); size_t pos = file.find('.'); if (pos == std::string::npos) { throw Exception("No file extension found in filename."); } pos += 1; std::string extension = file.substr(pos, file.length()-pos); if (extension.compare("png") == 0 || extension.compare("jpg") == 0 || extension.compare("jpeg") == 0 || extension.compare("bmp") == 0 || extension.compare("tga") == 0 || extension.compare("dds") == 0 || extension.compare("hdr") == 0 || extension.compare("psd") == 0) { int width, height, channels; unsigned char *data = SOIL_load_image ( filenames[i], &width, &height, &channels, SOIL_LOAD_RGBA ); unsigned int format; /* switch(channels) { case 1: throw Exception("Format not recognized or supported."); break; case 2: throw Exception("Format not recognized or supported."); break; case 3: throw Exception("Format not recognized or supported."); break; case 4: format = PF_32_R8G8B8A8; break; default: throw Exception("Format not recognized or supported."); break; } */ format = PF_32_R8G8B8A8; if (texture_width == 0 && texture_height == 0) { texture_width = width; texture_height = height; texture_channels = channels; pixel_format = format; total_face_bytes = texture_width*texture_height*4; texture_data.Set(total_face_bytes * 6); //texture.BindFlags = BIND_SHADER_RESOURCE; texture.Format = format; texture.Width = width; texture.Height = height; //texture.Usage = TEXTURE_USAGE_STATIC; //texture.MipLevels = 1; } else { if (texture_width != width) { throw Exception("Different Widths In Cube Map."); } if (texture_height != height) { throw Exception("Different Heights In Cube Map."); } if (texture_channels != channels) { throw Exception("Different Channels In Cube Map."); } if (pixel_format != format) { throw Exception("Different Format In Cube Map."); } } memcpy(texture_data.GetData() + dest_offset, data, total_face_bytes); texture.InitialData[i] = texture_data.GetData() + dest_offset; dest_offset += total_face_bytes; free(data); } else { throw Exception("File extension not supported."); } } }
void TextureLoader::LoadTexture2D(const char * filename, DescTexture2D & texture, FixedArray<unsigned char> & texture_data) { std::string file(filename); size_t pos = file.find('.'); CONDITIONAL_EXCEPTION(pos == std::string::npos, "No file extension found in filename."); pos += 1; std::string extension = file.substr(pos, file.length()-pos); if (extension.compare("png") == 0 || extension.compare("jpg") == 0 || extension.compare("jpeg") == 0 || extension.compare("bmp") == 0 || extension.compare("tga") == 0 || extension.compare("dds") == 0 || extension.compare("hdr") == 0 || extension.compare("psd") == 0) { int width, height, channels; unsigned char *data = SOIL_load_image ( filename, &width, &height, &channels, SOIL_LOAD_RGBA ); unsigned int format; /* switch(channels) { case 1: throw Exception("Format not recognized or supported."); break; case 2: throw Exception("Format not recognized or supported."); break; case 3: throw Exception("Format not recognized or supported."); break; case 4: format = PF_32_R8G8B8A8; break; default: throw Exception("Format not recognized or supported."); break; } */ format = PF_32_R8G8B8A8; unsigned int texture_size = width*height*4; texture_data.Set(texture_size); memcpy(texture_data.GetData(), data, texture_size); //texture.BindFlags = BIND_SHADER_RESOURCE; texture.Format = format; texture.Width = width; texture.Height = height; //texture.Usage = TEXTURE_USAGE_STATIC; //texture.MipLevels = 1; texture.InitialData = texture_data.GetData(); free(data); } else { throw Exception("File extension not supported."); } }
TEST(Containers, ArrayBool) { FixedArray<20, bool> array; FixedArray<20, bool> &cref = array; EXPECT_TRUE(array.empty()); EXPECT_TRUE(cref.empty()); EXPECT_EQ(array.size(), 0); EXPECT_EQ(cref.size(), 0); EXPECT_TRUE(array.push_back(true)); EXPECT_FALSE(array.empty()); EXPECT_FALSE(cref.empty()); EXPECT_EQ(array.front(), true); EXPECT_EQ(cref.front(), true); EXPECT_TRUE(array.pop_back()); EXPECT_FALSE(array.pop_back()); EXPECT_TRUE(array.empty()); EXPECT_TRUE(cref.empty()); EXPECT_TRUE(array.push_back(false)); EXPECT_TRUE(array.push_back(true)); EXPECT_EQ(array.front(), false); EXPECT_EQ(cref.front(), false); EXPECT_EQ(array.back(), true); EXPECT_EQ(cref.back(), true); EXPECT_EQ(array.size(), 2); EXPECT_EQ(cref.size(), 2); array.clear(); EXPECT_EQ(array.size(), 0); EXPECT_TRUE(array.empty()); for(unsigned i=0; i< array.capacity(); i++) EXPECT_TRUE(array.push_back(i & 1)); EXPECT_FALSE(array.push_back(-1)); for(unsigned i=0; i < array.capacity(); i++) { EXPECT_EQ(array[i], int(i & 1)); EXPECT_EQ(cref[i], int(i & 1)); } for(unsigned i=0; i < array.capacity(); i++) { EXPECT_EQ(array.back(), int(array.capacity() - 1 - i) & 1); EXPECT_EQ(cref.back(), int(cref.capacity() - 1 - i) & 1); EXPECT_TRUE(array.pop_back()); } }
TEST(Containers, Array) { FixedArray<20, int> array; FixedArray<20, int> &cref = array; EXPECT_TRUE(array.empty()); EXPECT_TRUE(cref.empty()); EXPECT_EQ(array.size(), 0); EXPECT_EQ(cref.size(), 0); EXPECT_TRUE(array.push_back(100)); EXPECT_FALSE(array.empty()); EXPECT_FALSE(cref.empty()); EXPECT_EQ(array.front(), 100); EXPECT_EQ(cref.front(), 100); EXPECT_TRUE(array.pop_back()); EXPECT_FALSE(array.pop_back()); EXPECT_TRUE(array.empty()); EXPECT_TRUE(cref.empty()); EXPECT_TRUE(array.push_back(200)); EXPECT_TRUE(array.push_back(300)); EXPECT_EQ(array.front(), 200); EXPECT_EQ(cref.front(), 200); EXPECT_EQ(array.back(), 300); EXPECT_EQ(cref.back(), 300); EXPECT_EQ(array.size(), 2); EXPECT_EQ(cref.size(), 2); array.clear(); EXPECT_EQ(array.size(), 0); EXPECT_TRUE(array.empty()); for(unsigned i=0; i< array.capacity(); i++) EXPECT_TRUE(array.push_back(i)); EXPECT_FALSE(array.push_back(-1)); for(unsigned i=0; i < array.capacity(); i++) { EXPECT_EQ(array[i], int(i)); EXPECT_EQ(cref[i], int(i)); } for(unsigned i=0; i < array.capacity(); i++) { EXPECT_EQ(array.back(), int(array.capacity() - 1 - i)); EXPECT_EQ(cref.back(), int(cref.capacity() - 1 - i)); EXPECT_TRUE(array.pop_back()); } }