示例#1
0
int main (int argc, char *argv[]) {
	int *numbers = random_int_array(10);
	
	print_int_array(numbers);

	printf("=====================\n");

	gsort(numbers, bubble_sort, int_natural_order);
	print_int_array(numbers);

	printf("=====================\n");

	gsort(numbers, bubble_sort, int_reverse_order);
	print_int_array(numbers);

	return 0;
}
示例#2
0
文件: edit.c 项目: OPSF/uClinux
int main(int argc, char *argv[])
{
    int ifd;
    char *outname;
    int i;
/*    char *cc = "kbgcrmywKBGCRMYW";  */
    
    errstd = stderr;
    if (argc < 2) {
        printf("Input file not specified\n");
    	exit(1);
    }
    ifd = open(argv[1], O_RDONLY);
    if (ifd<0) pdie("opening input file");
    if (argc > 2) die("Too many arguments");
    if (argc < 3) outname = "out.bmp";
    else outname = argv[2];

    compute_arrays(vga_palette, 16);
    printf("\nVGA palette:\n\n\n");
    printpalette(vga_palette, 16);
    gsort(y_yiq);
    printf("\n\nVGA pallette by luminance\n\n\n");
    printpalette(vga_palette, 16);
    
    i = get_std_headers(ifd, &fh, &bmh, &lh);
    if (i) {
    	printf("Error exit on GET:  %d\n", i);
    	exit(i);
    }
    printf("\n\n\nContained palette:\n\n");

    compute_arrays(palette, 16);
    
    gsort(y_yiq);
    printpalette(palette, 16);
    
    close(ifd);

    return 0;
}
示例#3
0
void
ut_string(void)
{
//  useclass(Array, String);
  useclass(Lesser,Equal,Greater);
  useclass(AutoRelease);
  
  OBJ buf[] = { aStr("toto","tutu","","titi") };
  OBJ srt[] = { aStr("","titi","toto","tutu") };
  OBJ arr;

  OBJ pool = gnew(AutoRelease);

  UTEST_START("String")

    UTEST(gisEqual(aStr(""),aStr("")) == True);
    UTEST(gisEqual(aStr(""),aStr("titi")) == False);
    UTEST(gisEqual(aStr("titi"),aStr("")) == False);

    UTEST(gisEqual(aStr("titi"),aStr("titi")) == True);
    UTEST(gisEqual(aStr("tutu"),aStr("titi")) == False);

    UTEST(gcompare(aStr(""),aStr("")) == Equal);
    UTEST(gcompare(aStr(""),aStr("titi")) == Lesser);
    UTEST(gcompare(aStr("titi"),aStr("")) == Greater);

    UTEST(gcompare(aStr("tata"),aStr("titi")) == Lesser);
    UTEST(gcompare(aStr("titi"),aStr("titi")) == Equal);
    UTEST(gcompare(aStr("tutu"),aStr("titi")) == Greater);

    UTEST(gcompare(aStr("titis"),aStr("tutu")) == Lesser);
    UTEST(gcompare(aStr("tutu"),aStr("titis")) == Greater);
    UTEST(gcompare(aStr("titis"),aStr("titi")) == Greater);
    UTEST(gcompare(aStr("titi"),aStr("titis")) == Lesser);

    arr = aArrayRef(buf, COS_ARRLEN(buf));

    UTEST(gfind(arr, aStr("toto")) != Nil);
    UTEST(gfind(arr, aStr("tutu")) != Nil);
    UTEST(gfind(arr, aStr("titi")) != Nil);
    UTEST(gfind(arr, aStr("tata")) == Nil);
    UTEST(gfind(arr, aStr("titis")) == Nil);
    UTEST(gfind(arr, aStr("totos")) == Nil);
    UTEST(gfind(arr, aStr("tutus")) == Nil);
    UTEST(gfind(arr, aStr("")) != Nil);
    
    UTEST(gfind(arr, aFun(gisEqual, aStr("titi"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("toto"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("tutu"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("tata"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("titis"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("totos"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr("tutus"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr(""), __1)) != Nil);

    gsort(arr, aFun(gcompare,__1,__2));

    UTEST(gisEqual(arr, aArrayRef(srt, COS_ARRLEN(srt))) == True);

    UTEST(gfind(arr, aFun(gcompare, aStr("titi"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("toto"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("tutu"), __1)) != Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("tata"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("titis"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("totos"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr("tutus"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr(""), __1)) != Nil);

    arr = aArrayRef(0, 0);

    UTEST(gfind(arr, aStr("tata")) == Nil);
    UTEST(gfind(arr, aStr("")) == Nil);

    UTEST(gfind(arr, aFun(gisEqual, aStr("tata"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gisEqual, aStr(""), __1)) == Nil);

    UTEST(gfind(arr, aFun(gcompare, aStr("tata"), __1)) == Nil);
    UTEST(gfind(arr, aFun(gcompare, aStr(""), __1)) == Nil);

  UTEST_END

  grelease(pool);
}
示例#4
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_gsort(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    types::Double* pDblInd = NULL;

    std::wstring wstrWay     = L"d";
    std::wstring wstrProcess = L"g";

    if (in.size() < 1 || in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "gsort", 1, 3);
        return types::Function::Error;
    }

    if (_iRetCount > 2)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "gsort", 1, 2);
        return types::Function::Error;
    }

    /***** get data and perform operation *****/
    if (in.size() == 3) // get Direction
    {
        if (in[2]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "gsort", 3);
            return types::Function::Error;
        }

        wstrWay = in[2]->getAs<types::String>()->get(0);
        if (wstrWay != L"i" && wstrWay != L"d")
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: ['i' 'd'] expected.\n"), "gsort", 3);
            return types::Function::Error;
        }
    }

    if (in.size() >= 2) // get Option
    {
        if (in[1]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : A string expected.\n"), "gsort", 2);
            return types::Function::Error;
        }

        wstrProcess = in[1]->getAs<types::String>()->get(0);

        if ( wstrProcess != L"c"  &&
                wstrProcess != L"r"  &&
                wstrProcess != L"g"  &&
                wstrProcess != L"lc" &&
                wstrProcess != L"lr")
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: ['g' 'r' 'c' 'lc' 'lr'] expected.\n"), "gsort", 2);
            return types::Function::Error;
        }
    }

    // get data and perform operation for each types::
    if (in[0]->isGenericType() == false)
    {
        ast::ExecVisitor exec;
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
    }

    types::GenericType* pGTOut = in[0]->getAs<types::GenericType>();

    if (pGTOut->getDims() > 2)
    {
        ast::ExecVisitor exec;
        return Overload::call(L"%hm_gsort", in, _iRetCount, out, &exec);
    }

    if (_iRetCount == 2)
    {
        int iRowsInd = (wstrProcess == L"lc") ? 1 : pGTOut->getRows();
        int iColsInd = (wstrProcess == L"lr") ? 1 : pGTOut->getCols();

        pDblInd = new types::Double(iRowsInd, iColsInd);
    }

    if (in[0]->isDouble()) // double
    {
        types::Double* pDblIn = in[0]->getAs<types::Double>();
        // doc says : "With complex numbers, gsort can be overloaded"
        if (pDblIn->isComplex() && symbol::Context::getInstance()->getFunction(symbol::Symbol(L"%_gsort")))
        {
            if (_iRetCount == 2)
            {
                delete pDblInd;
            }

            ast::ExecVisitor exec;
            return Overload::call(L"%_gsort", in, _iRetCount, out, &exec);
        }

        types::Double* pDblOut = gsort(pDblIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pDblOut);
    }
    else if (in[0]->isSparse()) // sparse
    {
        if (_iRetCount == 2)
        {
            delete pDblInd;
        }

        ast::ExecVisitor exec;
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
    }
    else if (in[0]->isString()) // string
    {
        types::String* pStringIn = in[0]->getAs<types::String>();
        types::String* pStringOut = gsort(pStringIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pStringOut);
    }
    else if (in[0]->isInt8()) // int
    {
        types::Int8* pIIn = in[0]->getAs<types::Int8>();
        types::Int8* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt16())
    {
        types::Int16* pIIn = in[0]->getAs<types::Int16>();
        types::Int16* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt32())
    {
        types::Int32* pIIn = in[0]->getAs<types::Int32>();
        types::Int32* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt64())
    {
        types::Int64* pIIn = in[0]->getAs<types::Int64>();
        types::Int64* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt8()) // uint
    {
        types::UInt8* pIIn = in[0]->getAs<types::UInt8>();
        types::UInt8* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt16())
    {
        types::UInt16* pIIn = in[0]->getAs<types::UInt16>();
        types::UInt16* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt32())
    {
        types::UInt32* pIIn = in[0]->getAs<types::UInt32>();
        types::UInt32* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt64())
    {
        types::UInt64* pIIn = in[0]->getAs<types::UInt64>();
        types::UInt64* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else
    {
        ast::ExecVisitor exec;
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out, &exec);
    }

    /***** set result *****/
    if (_iRetCount == 2)
    {
        out.push_back(pDblInd);
    }

    return types::Function::OK;
}
示例#5
0
/*--------------------------------------------------------------------------*/
types::Function::ReturnValue sci_gsort(types::typed_list &in, int _iRetCount, types::typed_list &out)
{
    // In all cases, to later test in[0]:
    if (in.size() < 1)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): At least %d expected.\n"), "gsort", 1);
        return types::Function::Error;
    }
    // The maximal number of input args may depend on the input data type, due to specific options

    //
    // Special cases
    //
    if (in[0]->isGenericType() == false)
    {
        // custom types
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }
    types::GenericType* pGTIn = in[0]->getAs<types::GenericType>();
    if (pGTIn->getDims() > 2)
    {
        // hypermatrix
        return Overload::call(L"%hm_gsort", in, _iRetCount, out);
    }
    if (pGTIn->isSparse())
    {
        // sparse
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }
    if (pGTIn->isComplex() && symbol::Context::getInstance()->getFunction(symbol::Symbol(L"%_gsort")))
    {
        // complex is documented as being managed through overloading
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    //
    // Common case
    //

    if (in.size() > 3)
    {
        Scierror(77, _("%s: Wrong number of input argument(s): %d to %d expected.\n"), "gsort", 1, 3);
        return types::Function::Error;
    }

    if (_iRetCount > 2)
    {
        Scierror(78, _("%s: Wrong number of output argument(s): %d to %d expected.\n"), "gsort", 1, 2);
        return types::Function::Error;
    }

    // Get the sorting order
    std::wstring wstrWay = L"d";
    if (in.size() > 2)
    {
        if (in[2]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "gsort", 3);
            return types::Function::Error;
        }

        wstrWay = in[2]->getAs<types::String>()->get(0);
        if (wstrWay != L"i" && wstrWay != L"d")
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: %s expected.\n"), "gsort", 3, "'i'|'d'");
            return types::Function::Error;
        }
    }

    // Get the sorting method
    std::wstring wstrProcess = L"g";
    if (in.size() >= 2)
    {
        if (in[1]->isString() == false)
        {
            Scierror(999, _("%s: Wrong type for input argument #%d : string expected.\n"), "gsort", 2);
            return types::Function::Error;
        }

        wstrProcess = in[1]->getAs<types::String>()->get(0);

        if ( wstrProcess != L"c"  &&
                wstrProcess != L"r"  &&
                wstrProcess != L"g"  &&
                wstrProcess != L"lc" &&
                wstrProcess != L"lr")
        {
            Scierror(999, _("%s: Wrong value for input argument #%d: ['g' 'r' 'c' 'lc' 'lr'] expected.\n"), "gsort", 2);
            return types::Function::Error;
        }
    }

    // Get data and perform operation for each types::
    types::Double* pDblInd = NULL;
    if (_iRetCount == 2)
    {
        int iRowsInd = (wstrProcess == L"lc") ? 1 : pGTIn->getRows();
        int iColsInd = (wstrProcess == L"lr") ? 1 : pGTIn->getCols();

        pDblInd = new types::Double(iRowsInd, iColsInd);
    }

    if (in[0]->isDouble()) // double
    {
        types::Double* pDblIn = in[0]->getAs<types::Double>();
        types::Double* pDblOut = gsort(pDblIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pDblOut);
    }
    else if (in[0]->isString()) // string
    {
        types::String* pStringIn = in[0]->getAs<types::String>();
        types::String* pStringOut = gsort(pStringIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pStringOut);
    }
    else if (in[0]->isInt8()) // int
    {
        types::Int8* pIIn = in[0]->getAs<types::Int8>();
        types::Int8* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt16())
    {
        types::Int16* pIIn = in[0]->getAs<types::Int16>();
        types::Int16* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt32())
    {
        types::Int32* pIIn = in[0]->getAs<types::Int32>();
        types::Int32* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isInt64())
    {
        types::Int64* pIIn = in[0]->getAs<types::Int64>();
        types::Int64* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt8()) // uint
    {
        types::UInt8* pIIn = in[0]->getAs<types::UInt8>();
        types::UInt8* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt16())
    {
        types::UInt16* pIIn = in[0]->getAs<types::UInt16>();
        types::UInt16* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt32())
    {
        types::UInt32* pIIn = in[0]->getAs<types::UInt32>();
        types::UInt32* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else if (in[0]->isUInt64())
    {
        types::UInt64* pIIn = in[0]->getAs<types::UInt64>();
        types::UInt64* pIOut = gsort(pIIn, pDblInd, wstrWay, wstrProcess);
        out.push_back(pIOut);
    }
    else    // Other generic data types not supported
    {
        std::wstring wstFuncName = L"%" + in[0]->getShortTypeStr() + L"_gsort";
        return Overload::call(wstFuncName, in, _iRetCount, out);
    }

    // Returns indices when requested
    if (_iRetCount == 2)
    {
        out.push_back(pDblInd);
    }

    return types::Function::OK;
}