C как получить доступ к элементам структуры, не используя -> повторно? - PullRequest
0 голосов
/ 12 марта 2020
    struct Heap {
        int capacity;
        int heapSize;
        int *tree;     // the heap binary tree
        int *pos;       // pos[i] is the position of values[i] in items
        float *p;  // priority value of each heap element
    };

    void initHeap(struct Heap *heap, int capacity) {
        heap->capacity = capacity;
        heap->heapSize = 0;
        heap->tree = malloc(sizeof(int)*(capacity+1));
        heap->pos = malloc(sizeof(int)*(capacity+1));
        heap->p = malloc(sizeof(float)*(capacity+1));
    }

    void betterInit(struct Heap *heap, int capacity) {
        with (heap) { // doesn't exist
            capacity = capacity;
            heapSize = 0;
            tree = malloc(sizeof(int)*(capacity+1));
            pos = malloc(sizeof(int)*(capacity+1));
            p = malloc(sizeof(float)*(capacity+1));
        }
    }
// update heap after a value is increased
void upHeap(struct Heap *heap, int i) {
    int *tree = heap->tree, *pos = heap->pos;
    float *p = heap->p;
    int c, r;

    c = pos[i];         // position of element i-th in heap
    while (true) {
        r = parent(c);
        if (r==0 || p[tree[r]] >= p[i]) break; // if c is root, or priority(parent(c)) is > priority(c)
        pos[tree[r]] = c;  // pull the parent down to c
        tree[c] = tree[r];
        c = r;
    }
    tree[c] = i;
    pos[i] = c;
}

Итак, 1-й initHeap выглядит длинным, потому что мне приходится писать heap-> много раз. И я sh, чтобы он выглядел короче.

Одно из решений - написать:

int *tree = heap->tree;
int *pos = heap->pos;
float *p = heap->p;

, а затем использовать tree, pos, p. Есть ли еще способы?

Ответы [ 3 ]

7 голосов
/ 12 марта 2020

Вы можете инициализировать структуру, используя обозначенный C99 инициализатор синтаксис:

void initHeap(struct Heap *heap, int capacity) {
    *heap = (struct Heap){
        .capacity = capacity,
        .heapSize = 0,
        .tree = malloc(sizeof(int)*(capacity+1)),
        .pos = malloc(sizeof(int)*(capacity+1)),
        .p = malloc(sizeof(float)*(capacity+1))
    };
}
1 голос
/ 12 марта 2020

А как насчет старых добрых макросов?

struct Heap {
    int capacity;
    int heapSize;
    int *tree;     // the heap binary tree
    int *pos;       // pos[i] is the position of values[i] in items
    float *p;  // priority value of each heap element
};

#define _(x) heap->x
void initHeap(struct Heap *heap, int capacity) {
    _(capacity) = capacity;
    _(heapSize) = 0;
    _(tree) = malloc(sizeof(int)*(capacity+1));
    _(pos) = malloc(sizeof(int)*(capacity+1));
    _(p) = malloc(sizeof(float)*(capacity+1));
}
/* More struct Heap related functions */
#undef _

Если использовать дисциплинированный подход и иметь краткий код в реализации этой библиотеки, я не считаю это плохой идеей. И, в качестве бонуса, он работает также для частичных обновлений и при импорте в компиляторы C ++ (я знаю, это не выглядит очень значительным).

В качестве дополнительного комментария, почему capacity+1 в вызовах malloc()

0 голосов
/ 12 марта 2020

Как насчет использования точки "." ? Например,

struct Heap {
    int capacity;
    int heapSize;
    int *tree;     // the heap binary tree
    int *pos;       // pos[i] is the position of values[i] in items
    float *p;  // priority value of each heap element
}H; //to make abbreviation for Heap

, затем используйте

H.capacity = capacity;
H.heapsize = 0;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...