示例#1
0
void Heap::siftDown(vector<int>::iterator iter){
	if((iter-coredatas.begin())*2+1>=coredatas.size())return;
	vector<int>::iterator leftchild = coredatas.begin()+(iter-coredatas.begin())*2+1;

	vector<int>::iterator iter_min = leftchild;
	if((iter-coredatas.begin())*2+2<coredatas.size()){
		vector<int>::iterator rightchild = coredatas.begin()+(iter-coredatas.begin())*2+2;
		if(*rightchild<*leftchild){
			iter_min = rightchild;
		}
	}
	
	if(*iter>*iter_min){
		myswap(iter,iter_min);
		siftDown(iter_min);
	}
}
示例#2
0
/*
	函数功能:冒泡排序
	参数:
		@ptr : 数组指针
		@len : 数组长度
		@fcmp : 比较函数

*/
void bubble_sort(void * base,int numOfEelements,int sizeOfElements,int (*fcmp)(const void*op1,const void *op2))
{
    bool ok;//用于记录当前趟数中是否有逆序对,如果有ok = 0,否则ok值不变
    for(int i=0;i<numOfEelements-1;++i)//最坏情况下需要len-1趟
    {
        ok = 1;
        for(int j=numOfEelements-1;j>i;--j)//从右向左移动,根据比较函数进行动作
        {
            if(fcmp(((char*)base+j*sizeOfElements),((char*)base+(j-1)*sizeOfElements)) < 0)/*ptr[j] > ptr[j-1]时候交换*/
            {
				myswap((char*)base+j*sizeOfElements,(char*)base+(j-1)*sizeOfElements,sizeOfElements);
                ok = 0;
            }
        }
        if(ok)//如果没有逆序对,说明排序已经完成
            return;
    }

}
示例#3
0
文件: 04.c 项目: zyxstar/exam_c
int main(void){
    int a = 12;
    int b = 34;
    myswap(&a, &b);
    return 0;
}
void tele3() {
    std::vector<std::string> names;
    std::vector<std::string> secondNames;
    std::ifstream names_fin("../имена.txt");
    std::ifstream secondNames_fin("../фамилии.txt");
    std::copy(std::istream_iterator<std::string>(names_fin),
              std::istream_iterator<std::string>(),
              std::back_inserter(names));

    std::copy(std::istream_iterator<std::string>(secondNames_fin),
              std::istream_iterator<std::string>(),
              std::back_inserter(secondNames));

    srand(0);
    DataBase &db = DataBase::getInstance();
    std::vector<Row> result;
    {
        {
            TableDescription abonents, offices;
            abonents.setName("Абоненты ТЕЛЕ-3");
            abonents.setSpatialKey("Позиция", SpatialType::POINT);
            abonents.setTemporalKey("Время", TemporalType::VALID_TIME);
            AttributeDescription name, secondName;
            name.name = "Имя"; name.type = Type::STRING;
            secondName.name = "Фамилия"; secondName.type = Type::STRING;
            abonents.addColumn(name); abonents.addColumn(secondName);

            offices.setName("Офисы ТЕЛЕ-3");
            offices.setSpatialKey("Граница офиса", SpatialType::POLYGON);
            offices.setTemporalKey("Время внесения в таблицу", TemporalType::TRANSACTION_TIME);
            AttributeDescription street, officeName;
            street.name = "Адрес"; street.type = Type::STRING;
            officeName.name = "Название офиса"; officeName.type = Type::STRING;
            offices.addColumn(street); offices.addColumn(officeName);
            db.createTable(abonents);
            db.createTable(offices);
            db.showTableHeader("Абоненты ТЕЛЕ-3");
            db.showTableHeader("Офисы ТЕЛЕ-3");
        }

        for (int i = 0; i < 30; i++) {
            Row r;
            r.spatialKey.name = "Позиция";
            r.temporalKey.name = "Время";
            r.spatialKey.type = SpatialType::POINT;
            r.temporalKey.type = TemporalType::VALID_TIME;
            r.spatialKey.points.push_back({(float)(((rand() / ((double)RAND_MAX)))  * 2.0 - 1.0), (float)(((rand() / ((double)RAND_MAX)))  * 2.0 - 1.0)});
            r.temporalKey.validTimeS = Date::getRandomDate(Date(2012, 01, 01, 00, 00, 00, 0), Date(2016,12, 31, 23, 59, 59, 999999));
            r.temporalKey.validTimeE.setFromCode(r.temporalKey.validTimeS.codeDate() + (rand() / ((double)RAND_MAX)) * 60 * 999999);
            if (r.temporalKey.validTimeS.codeDate() > r.temporalKey.validTimeE.codeDate()) {
                myswap(r.temporalKey.validTimeS, r.temporalKey.validTimeE);
            }
            Attribute name, secondName;
            name.setName("Имя");
            secondName.setName("Фамилия");
            name.setValue(names[((rand() / ((double)RAND_MAX))) * names.size()]);
            secondName.setValue(secondNames[((rand() / ((double)RAND_MAX))) * secondNames.size()]);
            r.addAttribute(name);
            r.addAttribute(secondName);
            db.insertRow("Абоненты ТЕЛЕ-3", r);
        }

        db.showTable("Абоненты ТЕЛЕ-3");

        result = db.selectRow("Абоненты ТЕЛЕ-3", SELECT_ALL_ROWS()).unwrap();

        for (uint i = 0; i < result.size(); i++) {
            printf("\\(%f; %f\\) & %s - %s & %s & %s\\\\ \\hline\n", result[i].spatialKey.points[0].x, result[i].spatialKey.points[0].y,
                    result[i].temporalKey.validTimeS.toString().c_str(), result[i].temporalKey.validTimeE.toString().c_str(),
                    result[i].getAttribute(0).unwrap().getString().unwrap().c_str(), result[i].getAttribute(1).unwrap().getString().unwrap().c_str());
        }
    }
    std::vector<std::vector<float2>> polygons;
    {

        polygons.resize(2);
        polygons[0].push_back({0.0 - 1.0, 0.0});
        polygons[0].push_back({1.0 - 1.0, 0.0});
        polygons[0].push_back({1.0 - 1.0, 0.8});
        polygons[0].push_back({0.7 - 1.0, 0.8});
        polygons[0].push_back({0.7 - 1.0, 0.5});
        polygons[0].push_back({0.3 - 1.0, 0.5});
        polygons[0].push_back({0.3 - 1.0, 0.8});
        polygons[0].push_back({0.0 - 1.0, 0.8});

        polygons[1].push_back({1.0, -0.1});
        polygons[1].push_back({0.0, -0.6});
        polygons[1].push_back({-1.0, -0.1});


        for (int i = 0; i < 2; i++) {
            Row r;
            r.spatialKey.name = "Граница офиса";
            r.temporalKey.name = "Время внесения в таблицу";
            r.spatialKey.type = SpatialType::POLYGON;
            r.temporalKey.type = TemporalType::TRANSACTION_TIME;
            r.spatialKey.points = polygons[i];
            Attribute street, officeName;
            street.setName("Адрес");
            officeName.setName("Название офиса");
            street.setValue("Улица \"" + names[((rand() / ((double)RAND_MAX))) * names.size()] + "\"");
            officeName.setValue("Офис имени \"" + secondNames[((rand() / ((double)RAND_MAX))) * secondNames.size()] + "\"");
            r.addAttribute(street);
            r.addAttribute(officeName);
            db.insertRow("Офисы ТЕЛЕ-3", r);
        }
        db.showTable("Офисы ТЕЛЕ-3");

        std::vector<Row> result = db.selectRow("Офисы ТЕЛЕ-3", SELECT_ALL_ROWS()).unwrap();

        for (uint i = 0; i < result.size(); i++) {
            printf("\\{");
            for (uint j = 0; j < result[i].spatialKey.points.size(); j++) {
                printf("\\(%0.1f; %0.1f\\)", result[i].spatialKey.points[j].x, result[i].spatialKey.points[j].y);
                if (j < result[i].spatialKey.points.size() - 1) {
                    printf(", ");
                }
            }
            printf("\\} & ");
            printf("%s & ", result[i].temporalKey.transactionTime.toString().c_str());
            printf("%s & ", result[i].getAttribute(0).unwrap().getString().unwrap().c_str());
            printf("%s ", result[i].getAttribute(1).unwrap().getString().unwrap().c_str());
            /*printf("(%f; %f) & %s & %s & %s", result[i].spatialKey.points[0].x, result[i].spatialKey.points[0].y,
                    result[i].temporalKey.validTimeS.toString().c_str(), result[i].temporalKey.validTimeE.toString().c_str(),
                    result[i].getAttribute(0).getString().c_str(), result[i].getAttribute(1).getString().c_str());*/
            printf("\\\\ \\hline\n");
        }
    }

    std::unique_ptr<TempTable> officesTT = db.selectTable("Офисы ТЕЛЕ-3").unwrap();
    std::unique_ptr<TempTable> abonentsTT = db.selectTable("Абоненты ТЕЛЕ-3").unwrap();
    std::unique_ptr<TempTable> abonentsTT_filtered = db.filter(abonentsTT, tester()).unwrap();
    db.showTable(abonentsTT_filtered);
    std::unique_ptr<TempTable> output = db.polygonxpointPointsInPolygon(officesTT, abonentsTT_filtered).unwrap();
    db.showTable(output);
    std::vector<Row> output_rows;
    output_rows = db.selectRow(output, SELECT_ALL_ROWS()).unwrap();
    result = db.selectRow(abonentsTT_filtered, SELECT_ALL_ROWS()).unwrap();
    db.showTable(output_rows[0].getAttribute(2).unwrap().getSet().unwrap());

    if( !glfwInit() )
    {
        fprintf( stderr, "Failed to initialize GLFW\n" );
        exit( EXIT_FAILURE );
    }
    glewExperimental = GL_TRUE;
    glewInit();

    GLFWwindow* window = glfwCreateWindow (1000, 1000, "Hello Triangle", NULL, NULL);
    if (!window) {
        fprintf (stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
    }
    glfwMakeContextCurrent (window);
    reshape(1000, 1000);

    do{
        glClearColor(1, 1, 1, 1);
        int width, height;
        glfwGetWindowSize(window, &width, &height);
        reshape(width, height);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glPointSize(12.0);
/*
        for (int i = 0; i < result.size(); i++) {
            glBegin(GL_POINTS);
                glColor3f(0, 0, 0);
                glVertex2f(result[i].spatialKey.points[0].x, result[i].spatialKey.points[0].y);
            glEnd();
        }*/

        for (uint i = 0; i < 1; i++) {
            std::vector<Row> rows;
            auto ptr = output_rows[i].getAttribute(2).unwrap().getSet().unwrap();
            rows = db.selectRow(ptr, SELECT_ALL_ROWS()).unwrap();

            for (uint j = 0; j < rows.size(); j++) {
                glBegin(GL_POINTS);
                    glColor3f(1, 0, 0);
                    glVertex2f(rows[j].spatialKey.points[0].x, rows[j].spatialKey.points[0].y);
                glEnd();
            }
        }

        for (uint i = 0; i < polygons.size(); i++) {
            glBegin(GL_LINE_LOOP);
            for (uint j = 0; j < polygons[i].size(); j++) {
                glColor3f(0, 0, 0);
                glVertex2f(polygons[i][j].x, polygons[i][j].y);
            }
            glEnd();
        }


        glfwSwapBuffers(window);
        glfwPollEvents();

    }
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
}