Exemple #1
0
int ssd_reverse(Quark *q)
{
    ss_data *ssd = ssd_get_data(q);
    int nrows, ncols, i, j, k;

    if (!ssd) {
        return RETURN_FAILURE;
    }
    
    nrows = ssd_get_nrows(q);
    ncols = ssd_get_ncols(q);

    for (k = 0; k < ncols; k++) {
        ss_column *col = &ssd->cols[k];
        if (col->format == FFORMAT_STRING) {
            char **s = col->data;
            for (i = 0; i < nrows/2; i++) {
                j = (nrows - 1) - i;
                sswap(&s[i], &s[j]);
            }
        } else {
            double *x = col->data;
            for (i = 0; i < nrows/2; i++) {
                j = (nrows - 1) - i;
                fswap(&x[i], &x[j]);
            }
        }
    }
    quark_dirtystate_set(q, TRUE);
    
    return RETURN_SUCCESS;
}
Exemple #2
0
static GVarType get_proc(const void *obj,
    const char *name, GVarData *prop, void *udata)
{
    Quark *q = (Quark *) obj;
    Grace *grace = grace_from_quark(q);
    DataColumn col;
    int column;
    
    if (!q) {
        return GVarNil;
    }
    
    if (strings_are_equal(name, "idstr")) {
        prop->str = copy_string(NULL, QIDSTR(q));
        return GVarStr;
    } else
    if (strings_are_equal(name, "active")) {
        prop->boolval = quark_is_active(q);
        return GVarBool;
    }
    
    switch (quark_fid_get(q)) {
    case QFlavorSSD:
        if (strings_are_equal(name, "nrows")) {
            prop->num = ssd_get_nrows(q);
            return GVarNum;
        } else
        if (strings_are_equal(name, "ncols")) {
            prop->num = ssd_get_ncols(q);
            return GVarNum;
        } else
        if ((column = ssd_get_column_by_name(q, name)) >= 0) {
            prop->arr = ssd_get_darray(q, column);
            return GVarArr;
        } else {
            return GVarNil;
        }
        break;
    case QFlavorSet:
        if (strings_are_equal(name, "length")) {
            prop->num = set_get_length(q);
            return GVarNum;
        } else
        if ((col = get_dataset_col_by_name(grace, name)) != DATA_BAD) {
            prop->arr = set_get_darray(q, col);
            return GVarArr;
        } else {
            return GVarNil;
        }
        break;
    default:
        return GVarNil;
        break;
    }
}
Exemple #3
0
int ssd_is_numeric(const Quark *q)
{
    unsigned int i, ncols = ssd_get_ncols(q);
    for (i = 0; i < ncols; i++) {
        int fformat = ssd_get_col_format(q, i);
        if (fformat == FFORMAT_STRING || fformat == FFORMAT_UNKNOWN) {
            return FALSE;
        }
    }
    
    return TRUE;
}
Exemple #4
0
static int enterCB(TableEvent *event)
{
    SSDataUI *ui = (SSDataUI *) event->anydata;

    int ncols = ssd_get_ncols(ui->q);
    
    if (event->col >= 0 && event->col <= ncols) {
        TableDeselectAllCells(ui->mw);
        return TRUE;
    } else {
        return FALSE;
    }
}
Exemple #5
0
/* 
 * Coalesce two SSDs. fromq will be deleted, preceded by transferring of all
 * its children to toq
 */
int ssd_coalesce(Quark *toq, Quark *fromq)
{
    unsigned int nrows, ncols, i, j;
    ss_data *ssd;
    AMem *amem  = toq->amem;
    coalesce_hook_t p;
       
    nrows = ssd_get_nrows(fromq);
    if (nrows > ssd_get_nrows(toq)) {
        if (ssd_set_nrows(toq, nrows) != RETURN_SUCCESS) {
            return RETURN_FAILURE;
        }
    }

    /* original number of columns */
    ncols = ssd_get_ncols(toq);
    
    ssd = ssd_get_data(fromq);
    for (i = 0; i < ssd->ncols; i++) {
        ss_column *col = &ssd->cols[i];
        
        ss_column *col_new = ssd_add_col(toq, col->format);
        if (!col_new) {
            return RETURN_FAILURE;
        }
        
        col_new->label = amem_strdup(amem, col->label);
        
        if (col->format == FFORMAT_STRING) {
            for (j = 0; j < nrows; j++) {
                ((char **) col_new->data)[j] =
                    amem_strdup(amem, ((char **) col->data)[j]);
            }
        } else {
            memcpy(col_new->data, col->data, nrows*SIZEOF_DOUBLE);
        }
    }
    
    p.toq = toq;
    p.nshift = ncols;
    quark_traverse(fromq, coalesce_hook, &p);
    
    quark_free(fromq);
    
    return RETURN_SUCCESS;
}
Exemple #6
0
int ssd_transpose(Quark *q)
{
    ss_data *ssd_new, *ssd_old;
    unsigned int ncols, nrows, i, j;
    
    if (!ssd_is_numeric(q)) {
        return RETURN_FAILURE;
    }
    
    ncols = ssd_get_ncols(q);
    nrows = ssd_get_nrows(q);
    if (!ncols || !nrows) {
        return RETURN_FAILURE;
    }
    
    ssd_new = ssd_data_new(q->amem);
    if (!ssd_new) {
        return RETURN_FAILURE;
    }
    
    /* swap the data */
    ssd_old = q->data;
    q->data = ssd_new;
    
    if (ssd_set_ncols(q, nrows, NULL) != RETURN_SUCCESS ||
        ssd_set_nrows(q, ncols)       != RETURN_SUCCESS) {
        
        ssd_data_free(q->amem, ssd_new);
        q->data = ssd_old;
        return RETURN_FAILURE;
    }
    
    for (i = 0; i < ncols; i++) {
        for (j = 0; j < nrows; j++) {
            ssd_set_value(q, i, j, ((double *)ssd_old->cols[i].data)[j]);
        }
    }

    quark_dirtystate_set(q, TRUE);

    ssd_data_free(q->amem, ssd_old);
    
    return RETURN_SUCCESS;
}
Exemple #7
0
void update_ssd_ui(SSDataUI *ui, Quark *q)
{
    if (ui && q) {
        int i, nc, nr, new_nc, new_nr, ncols, nrows, nfixed_cols;
        int delta_nc, delta_nr;
        int *maxlengths;
        char **rowlabels, **collabels;
        
        if (ui->q != q) {
            TableDeselectAllCells(ui->mw);
        }
        
        ui->q = q;
        
        ncols = ssd_get_ncols(q);
        nrows = ssd_get_nrows(q);
        
        new_nc = ncols + EXTRA_SS_COLS;
        new_nr = nrows + EXTRA_SS_ROWS;
        
        if (ssd_is_indexed(q)) {
            nfixed_cols = 1;
        } else {
            nfixed_cols = 0;
        }

        nr = TableGetNrows(ui->mw);
        nc = TableGetNcols(ui->mw);

        delta_nr = new_nr - nr;
        delta_nc = new_nc - nc;

        if (delta_nr > 0) {
            TableAddRows(ui->mw, delta_nr);
        } else if (delta_nr < 0) {
            TableDeleteRows(ui->mw, -delta_nr);
        }

        rowlabels = xmalloc(new_nr*sizeof(char *));
        for (i = 0; i < new_nr; i++) {
            char buf[32];
            sprintf(buf, "%d", i + 1);
            rowlabels[i] = copy_string(NULL, buf);
        }
        TableSetRowLabels(ui->mw, rowlabels);
        for (i = 0; i < new_nr; i++) {
            xfree(rowlabels[i]);
        }
        xfree(rowlabels);

        maxlengths = xmalloc(new_nc*SIZEOF_INT);
        collabels = xmalloc(new_nc*sizeof(char *));

        for (i = 0; i < new_nc; i++) {
            ss_column *col = ssd_get_col(q, i);
            if (col && col->format == FFORMAT_STRING) {
                maxlengths[i] = STRING_CELL_WIDTH;
            } else {
                maxlengths[i] = 2*CELL_WIDTH;
            }
            if (col && !string_is_empty(col->label)) {
                collabels[i] = copy_string(NULL, col->label);
            } else {
                unsigned int coli;
                char buf[32];
                
                coli = i;
                sprintf(buf, "%c", coli%26 + 'A');
                while ((coli /= 26)) {
                    memmove(&buf[1], buf, strlen(buf) + 1);
                    buf[0] = coli%26 + 'A' - 1;
                }
                
                collabels[i] = copy_string(NULL, buf);
            }
        }

        if (delta_nc > 0) {
            TableAddCols(ui->mw, delta_nc);
        } else if (delta_nc < 0) {
            TableDeleteCols(ui->mw, -delta_nc);
        }

        TableSetColMaxlengths(ui->mw, maxlengths);
        TableSetColLabels(ui->mw, collabels);
        TableSetFixedCols(ui->mw, nfixed_cols);
        TableUpdateVisibleRowsCols(ui->mw);

        xfree(maxlengths);
        for (i = 0; i < new_nc; i++) {
            xfree(collabels[i]);
        }
        xfree(collabels);
        
        UpdateColChoice(ui->col_sel, q);
    }
}
Exemple #8
0
static int leaveCB(TableEvent *event)
{
    SSDataUI *ui = (SSDataUI *) event->anydata;

    int nrows = ssd_get_nrows(ui->q);
    int ncols = ssd_get_ncols(ui->q);
    int format;
    double value;
    
    int changed = FALSE;
    
    GraceApp *gapp = gapp_from_quark(ui->q);
    
    if (event->row < 0 || event->col < 0 || event->col > ncols) {
        return TRUE;
    }
    
    if (event->row >= nrows && !string_is_empty(event->value)) {
        if (ssd_set_nrows(ui->q, event->row + 1) == RETURN_SUCCESS) {
            changed = TRUE;
        }
    }
    
    if (event->col == ncols && !string_is_empty(event->value)) {
        if (parse_date_or_number(get_parent_project(ui->q),
            event->value, FALSE, get_date_hint(gapp), &value) == RETURN_SUCCESS) {
            format = FFORMAT_NUMBER;
        } else {
            format = FFORMAT_STRING;
        }
        if (ssd_add_col(ui->q, format)) {
            ncols++;
            changed = TRUE;
        }
    }
    
    if (event->col < ncols) {
        char *old_value = get_cell_content(ui, event->row, event->col, &format);
        if (!strings_are_equal(old_value, event->value)) {
            switch (format) {
            case FFORMAT_STRING:
                if (ssd_set_string(ui->q, event->row, event->col, event->value) ==
                    RETURN_SUCCESS) {
                    quark_dirtystate_set(ui->q, TRUE);
                    changed = TRUE;
                }
                break;    
            default:
                if (graal_eval_expr(grace_get_graal(gapp->grace),
                    event->value, &value, gproject_get_top(gapp->gp)) == RETURN_SUCCESS) {

                    unsigned int prec;
                    char buf[32];
                    double val;

                    prec = project_get_prec(get_parent_project(ui->q));
                    sprintf(buf, "%.*g", prec, value);

                    if (parse_date_or_number(get_parent_project(ui->q),
                        buf, FALSE, get_date_hint(gapp), &val) == RETURN_SUCCESS) {

                        if (ssd_set_value(ui->q, event->row, event->col, val) == RETURN_SUCCESS) {
                            quark_dirtystate_set(ui->q, TRUE);
                            changed = TRUE;
                        }
                    }
                } else {
                    errmsg("Can't parse input value");
                    return FALSE;
                }
                break;
            }
        }
    }
    
    if (changed) {
        snapshot_and_update(gapp->gp, FALSE);
    }

    return TRUE;
}