source: trunk/anuga_core/source/anuga/utilities/sparse_dok.h @ 8710

Last change on this file since 8710 was 8691, checked in by steve, 12 years ago

Adding in the new c files

File size: 4.1 KB
Line 
1/*
2* Sparse Matrix class implemented in DOK format.
3*
4* The DOK (dictionary of keys) format is implemented using
5* uthash, which is a doubly linked list and also a hash table.
6* The hash table is populated by 'edge_t' objects which contain
7* a key specifying the (i,j) index of the matrix, and the value
8* at that index. If no entry exists, the value is assumed 0.
9*
10* The functions in this class which create new objects return
11* pointers to new malloced memory, which much be cleaned up.
12*
13* Padarn Wilson, ANU 2012
14*/
15
16
17#include <stdio.h>   /* gets */
18#include <stdlib.h>  /* atoi, malloc */
19#include <string.h>  /* strcpy */
20#include "math.h"
21#include "uthash.h"     /* in utilities */
22#include "sparse_csr.h"
23
24#ifndef SPARSE_DOK_H
25#define SPARSE_DOK_H
26
27// Struct edge_key_t to store the i,j position in the matrix within
28// a key of the hashtable
29typedef struct  {
30    int i;
31    int j;
32} edge_key_t;
33
34// Struct edge_t is a basic element of the hash table. By including
35// the UT_hash_handle variable, it can be used in the methods defined
36// for uthash hashtables
37typedef struct {
38    edge_key_t key;              /* key of form i , j */
39    double entry;                /* the value stored at this entry */
40    UT_hash_handle hh;         /* makes this structure hashable */
41} edge_t;
42
43// Struct sparse_dok defining a sparse matrix. Keeps track of the
44// number of entries and rows in the matrix, and stores the hashtable.
45// PADARN NOTE: For efficiency, it might be sensible to pre-allocated
46// the max number of rows/cols in the hash table, so that the hash table
47// can be made an appropriate size.
48typedef struct {
49        edge_t *edgetable;
50        int num_entries;
51        int num_rows;
52} sparse_dok;
53
54// 'Constructor' function. Returns pointer to new malloced memory, with
55// appropriate initilisation.
56sparse_dok * make_dok();
57
58// --------------- Hashtable Functions -----------------
59
60// find_dok_entry - Find pointer to hash table member with given key,
61// return NULL if member does not exist.
62edge_t *find_dok_entry(sparse_dok * edgetable,edge_key_t key);
63
64// add_dok_entry - Add new entry to the hash table with given key, holding
65// the specified value. If entry already exists value is added to current
66// value. If entry already exists and this function causes the value to become
67// zero, the entry is removed.
68void add_dok_entry(sparse_dok * edgetable,edge_key_t key, double value);
69
70// delete_dok_entry - Remove an edge from the hash table. Implicitly sets
71// the corresponding entry in the matrix to zero.
72void delete_dok_entry(sparse_dok * edgetable,edge_t *edge);
73
74// delete_all - Remove all edges from the hash table. Used to do clean up
75void delete_all(sparse_dok * edgetable);
76
77// delete_dok_matrix - Free all the memory associated with struct and
78// set pointer to Null.
79void delete_dok_matrix(sparse_dok * mat);
80
81// print_dok_entries - Print out all of the stored entries in the hash
82// table sorted by their key, along with their value.
83void print_dok_entries(sparse_dok * edgetable);
84
85// key_sort - Compare the relative size of two keys, used for sorting
86// PADARN NOTE: Does not need to be in header.
87int key_sort(edge_t *a, edge_t *b);
88
89// sort_by_key - Sort the linked list of the hash table by their key
90// values and the key_sort function.
91void sort_by_key(sparse_dok * hashtable);
92
93// --------------- Matrix Functions ---------------------
94
95// convert_to_csr_ptr - Convert the DOK format matrix into CSR format. The
96// new matrix is stored in the (already allocated) input pointer. The old
97// pointer is not freed, and should be cleaned up manually.
98void convert_to_csr_ptr(sparse_csr * new_csr,sparse_dok * hashtable);
99
100// add_sparse_dok - Perform a linear addition on two dok_matricies A=a*A+b*B.
101// The result is stored in the first input sparse_dok. The second sparse_dok is
102// not freed and should be cleaned up manually.
103// Note: No size conditions are imposed on the matricies, so potentially different
104// sized matricies can be combined.
105void add_sparse_dok(sparse_dok * dok1,double mult1,sparse_dok * dok2,double mult2);
106
107// get_dok_rows -- Return the number of rows currently stored in the matrix
108int get_dok_rows(sparse_dok * dok);
109
110#endif
Note: See TracBrowser for help on using the repository browser.