ccn_matrix.c

Go to the documentation of this file.
00001 /**
00002  * @file ccn_matrix.c
00003  * @brief Support for a sparse matrix (2-D table) of nonnegative integers.
00004  * 
00005  * Part of the CCNx C Library.
00006  *
00007  * Copyright (C) 2009 Palo Alto Research Center, Inc.
00008  *
00009  * This library is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License version 2.1
00011  * as published by the Free Software Foundation.
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details. You should have received
00016  * a copy of the GNU Lesser General Public License along with this library;
00017  * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
00018  * Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020  
00021 /** Initially just use a hash table */
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); /* for alignment */
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)); /* make sure any padding is cleared */
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  * ccn_matrix_getbounds:
00093  * Fills result with a (not necessarily tight) bounding box for the
00094  * non-zero elements of m.  Returns -1 in case of error, or a non-negative
00095  * value for success.
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  * ccn_matrix_trim:
00131  * Zeros any entries outside the bounds
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  * ccn_matrix_trim:
00141  * Zeros entries inside the bounds
00142  */
00143 int
00144 ccn_matrix_clear(struct ccn_matrix *m, const struct ccn_matrix_bounds *bounds)
00145 {
00146     return(-1);
00147 }
Generated on Fri May 13 16:27:03 2011 for Content-Centric Networking in C by  doxygen 1.6.3