/**************************************************************** * * Function: psMin * Input: int *A Pointer to input array * int *P Pointer to Prefix array * int *S Pointer to Suffix array * int n Size of the arrays * * Output: void * * Description: Runs the prefix and suffix algorthim. Also * sets the number of threads available to OpenMP. * Each Loop iteration a copy of A is made to B where the work * is done on. As the minArray writes to B and saves the result * to B[0] due to the balanced tree method. * *****************************************************************/ void psMin(int *A, int *P, int *S, int n){ int i; for(i=0; i<n; i++){ P[i] = minArray(A, i+1); /*find prefix min*/ S[i] = minArray(&A[i], n-i); /*find suffix min*/ } }
shared_ptr <JSONExport::JSONObject> serializeAccessor(JSONAccessor* accessor, void *context) { shared_ptr <JSONObject> accessorObject = shared_ptr<JSONObject>(new JSONObject()); accessorObject->setUnsignedInt32("byteStride", (unsigned int)accessor->getByteStride()); accessorObject->setUnsignedInt32("byteOffset", (unsigned int)accessor->getByteOffset()); accessorObject->setUnsignedInt32("elementsPerValue", (unsigned int)accessor->getElementsPerVertexAttribute()); accessorObject->setUnsignedInt32("count", (unsigned int)accessor->getCount()); accessorObject->setString("elementType", JSONUtils::getStringForType(accessor->getElementType())); JSONBuffer* buffer = context ? (JSONBuffer*)context : accessor->getBuffer().get(); accessorObject->setString("buffer", buffer->getID()); const double* min = accessor->getMin(); if (min) { shared_ptr <JSONExport::JSONArray> minArray(new JSONExport::JSONArray()); accessorObject->setValue("min", minArray); for (size_t i = 0 ; i < accessor->getElementsPerVertexAttribute() ; i++) { minArray->appendValue(shared_ptr <JSONExport::JSONNumber> (new JSONExport::JSONNumber(min[i]))); } } const double* max = accessor->getMax(); if (max) { shared_ptr <JSONExport::JSONArray> maxArray(new JSONExport::JSONArray()); accessorObject->setValue("max", maxArray); for (size_t i = 0 ; i < accessor->getElementsPerVertexAttribute() ; i++) { maxArray->appendValue(shared_ptr <JSONExport::JSONNumber> (new JSONExport::JSONNumber(max[i]))); } } return accessorObject; }
void runAmazonInterview() { int A[8] = { 3, 9, 5, 4, 1, 8, 11, 10 }; minArray(A, 8, 5); // brute force method O(nk) }
shared_ptr <GLTF::JSONObject> serializeMeshAttribute(GLTFMeshAttribute* meshAttribute, void *context) { shared_ptr <JSONObject> meshAttributeObject = shared_ptr<JSONObject>(new JSONObject()); meshAttributeObject->setUnsignedInt32("byteStride", (unsigned int)meshAttribute->getByteStride()); meshAttributeObject->setUnsignedInt32("byteOffset", (unsigned int)meshAttribute->getByteOffset()); meshAttributeObject->setUnsignedInt32("componentsPerAttribute", (unsigned int)meshAttribute->getElementsPerVertexAttribute()); meshAttributeObject->setUnsignedInt32("count", (unsigned int)meshAttribute->getCount()); meshAttributeObject->setString("componentType", GLTFUtils::getStringForGLType(meshAttribute->getComponentType())); void** buffers = (void**)context; GLTFBufferView *bufferView = context ? (GLTFBufferView*)buffers[0] : meshAttribute->getBufferView().get(); meshAttributeObject->setString("bufferView", bufferView->getID()); const double* min = meshAttribute->getMin(); if (min) { shared_ptr <GLTF::JSONArray> minArray(new GLTF::JSONArray()); meshAttributeObject->setValue("min", minArray); for (size_t i = 0 ; i < meshAttribute->getElementsPerVertexAttribute() ; i++) { minArray->appendValue(shared_ptr <GLTF::JSONNumber> (new GLTF::JSONNumber(min[i]))); } } const double* max = meshAttribute->getMax(); if (max) { shared_ptr <GLTF::JSONArray> maxArray(new GLTF::JSONArray()); meshAttributeObject->setValue("max", maxArray); for (size_t i = 0 ; i < meshAttribute->getElementsPerVertexAttribute() ; i++) { maxArray->appendValue(shared_ptr <GLTF::JSONNumber> (new GLTF::JSONNumber(max[i]))); } } return meshAttributeObject; }
int main() { int isArray[] = {58,75,22,48}; // 假設我們會給的是一個固定的整數陣列 int size = sizeof(isArray) / sizeof(isArray[0]); //利用sizeof operator抓出整個陣列的大小,和陣列其中一個元素的大小,兩者相除就可以得到陣列的元素個數 int min = minArray(isArray,size-1); //將陣列與它的大小放進去,印出,並且要注意的是,c用索引值來找陣列中的元素,故size-1 printf("%d\n",min); return 0; }
void Analysis::collect(const Mat& feature) { int nl = feature.rows; int nc = feature.cols; float max, min; pair<int, int> s; maxArray(feature, max, s); minArray(feature, min, s); printf("max: %f, min: %f\n", max, min, sum(feature)[0]); }
/**************************************************************** * * Function: psMin * Input: void* args pointer to data struct * * Output: void* * * Description: Runs the prefix and suffix algorthim.Main Loop * is parallelized and so each thread is given where is should * start and end its work. These values are given through the * void* args which is the pointer to the struct of type data * which contains all the information. At the end the thread * waits at the barrier until all the other threads who are * running are done * *****************************************************************/ void* psMin(void* args){ int i; int *A, *P, *S, n, end, start, id; data *input = (data*)args; A = input->A; P = input->P; S = input->S; n = input->n; start = input->start; end = input->end; id = input->id; for(i=start; i<end; i++){ P[i] = minArray(A, i+1); S[i] = minArray(&A[i], n-i); } pthread_barrier_wait(&barrier); return NULL; }
double* Color::getHSL() const { double max, min, c; double rgb[3], * hsl = new double[3]; for (int i = 0; i < 3; i++) rgb[i] = (double)color[i] / 255; max = maxArray(rgb, 3); min = minArray(rgb, 3); c = max - min; if (c == 0) hsl[0] = 0; else if (max == rgb[RGB_RED]) { hsl[0] = (rgb[RGB_GREEN] - rgb[RGB_BLUE]) / c; while (hsl[0] > 6) hsl[0] = hsl[0] - 6; } else if (max == rgb[RGB_GREEN]) hsl[0] = (rgb[RGB_BLUE] - rgb[RGB_RED]) / c + 2.0; else if (max == rgb[RGB_BLUE]) hsl[0] = (rgb[RGB_RED] - rgb[RGB_GREEN]) / c + 4.0; hsl[0] = 60 * hsl[0]; hsl[2] = (max + min) / 2; if (c == 0) hsl[1] = 0; /* this part does not work */ //else // hsl[1] = c / (1.0 - abs(2.0 * hsl[2] - 1.0)); else if (hsl[2] < .5) hsl[1] = c / (max + min); else hsl[1] = c / (2.0 - max - min); return hsl; }
void test3() { double s = minArray(n, a); printf("minArray=%lf\n", s); }
int minArray(int anArray[],int size) { if(size == 0) return anArray[0];//base case:如果陣列只有一個元素,那自然最小的就是它了,並且他的索引值為0 return fmin(anArray[size],minArray(anArray,size-1));//調用math.h的fmin來找兩者最小,利用對anArray[size]的變動來實現遞迴 }