ccn_matrix.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stddef.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <ccn/matrix.h>
00027 #include <ccn/hashtb.h>
00028
00029 struct ccn_matrix {
00030 struct hashtb_enumerator e;
00031 };
00032
00033 struct ccn_matrix_key {
00034 uint_least64_t row;
00035 unsigned col;
00036 };
00037
00038 struct ccn_matrix *
00039 ccn_matrix_create(void)
00040 {
00041 struct ccn_matrix *m;
00042 size_t size = sizeof(intptr_t);
00043 if (size < sizeof(uint_least64_t))
00044 size = sizeof(uint_least64_t);
00045 m = calloc(1, sizeof(*m));
00046 if (m != NULL)
00047 hashtb_start(hashtb_create(sizeof(uint_least64_t), NULL), &m->e);
00048 return(m);
00049 }
00050
00051 void
00052 ccn_matrix_destroy(struct ccn_matrix **mp)
00053 {
00054 struct ccn_matrix *m = *mp;
00055 if (m != NULL) {
00056 struct hashtb *ht = m->e.ht;
00057 hashtb_end(&m->e);
00058 hashtb_destroy(&ht);
00059 free(m);
00060 *mp = NULL;
00061 }
00062 }
00063
00064
00065 intptr_t
00066 ccn_matrix_fetch(struct ccn_matrix *m, uint_least64_t row, unsigned col)
00067 {
00068 intptr_t *valp;
00069 struct ccn_matrix_key key;
00070 memset(&key, 0, sizeof(key));
00071 key.row = row;
00072 key.col = col;
00073 valp = hashtb_lookup(m->e.ht, &key, sizeof(key));
00074 return(valp == NULL ? 0 : *valp);
00075 }
00076
00077 void
00078 ccn_matrix_store(struct ccn_matrix *m, uint_least64_t row, unsigned col,
00079 intptr_t value)
00080 {
00081 intptr_t *valp;
00082 struct ccn_matrix_key key;
00083 memset(&key, 0, sizeof(key));
00084 key.row = row;
00085 key.col = col;
00086 if (hashtb_seek(&(m->e), &key, sizeof(key), 0) == -1) return;
00087 valp = m->e.data;
00088 *valp = value;
00089 }
00090
00091
00092
00093
00094
00095
00096
00097 int
00098 ccn_matrix_getbounds(struct ccn_matrix *m, struct ccn_matrix_bounds *result)
00099 {
00100 struct hashtb_enumerator *e = &(m->e);
00101 struct hashtb *ht = e->ht;
00102 intptr_t *valp;
00103 const struct ccn_matrix_key *key;
00104 int first = 1;
00105 hashtb_end(e);
00106 memset(result, 0, sizeof(*result));
00107 hashtb_start(ht, e);
00108 while (e->data != NULL) {
00109 valp = e->data;
00110 if (*valp == 0)
00111 hashtb_delete(e);
00112 else {
00113 key = e->key;
00114 if (first || key->row >= result->row_max)
00115 result->row_max = key->row + 1;
00116 if (first || key->row < result->row_min)
00117 result->row_min = key->row;
00118 if (first || key->col >= result->col_max)
00119 result->col_max = key->col + 1;
00120 if (first || key->col < result->col_min)
00121 result->col_min = key->col;
00122 first = 0;
00123 hashtb_next(e);
00124 }
00125 }
00126 return(hashtb_n(ht));
00127 }
00128
00129
00130
00131
00132
00133 int
00134 ccn_matrix_trim(struct ccn_matrix *m, const struct ccn_matrix_bounds *bounds)
00135 {
00136 return(-1);
00137 }
00138
00139
00140
00141
00142
00143 int
00144 ccn_matrix_clear(struct ccn_matrix *m, const struct ccn_matrix_bounds *bounds)
00145 {
00146 return(-1);
00147 }