comparison liboctave/sparse-util.cc @ 9469:c6edba80dfae

sanity checks for loading sparse matrices
author John W. Eaton <jwe@octave.org>
date Wed, 29 Jul 2009 12:15:27 -0400
parents eb63fbe60fab
children 4c0cdbe0acca
comparison
equal deleted inserted replaced
9468:5af462716bff 9469:c6edba80dfae
1 /* 1 /*
2 2
3 Copyright (C) 2005, 2007, 2008 David Bateman 3 Copyright (C) 2005, 2007, 2008, 2009 David Bateman
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Andy Adler 4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Andy Adler
5 5
6 This file is part of Octave. 6 This file is part of Octave.
7 7
8 Octave is free software; you can redistribute it and/or modify it 8 Octave is free software; you can redistribute it and/or modify it
56 va_end (args); 56 va_end (args);
57 return ret; 57 return ret;
58 } 58 }
59 59
60 60
61 bool
62 sparse_indices_ok (octave_idx_type *r, octave_idx_type *c,
63 octave_idx_type nrows, octave_idx_type ncols,
64 octave_idx_type nnz)
65 {
66 if (nnz > 0)
67 {
68 if (c[0] != 0)
69 {
70 (*current_liboctave_error_handler)
71 ("invalid sparse matrix: cidx[0] must be zero");
72 return false;
73 }
74
75 octave_idx_type jold = 0;
76
77 for (octave_idx_type j = 1; j < ncols+1; j++)
78 {
79 if (c[j] < c[j-1])
80 {
81 (*current_liboctave_error_handler)
82 ("invalid sparse matrix: cidx elements must appear in ascending order");
83 return false;
84 }
85
86 if (c[j] > nnz)
87 {
88 (*current_liboctave_error_handler)
89 ("invalid sparse matrix: cidx[%d] = %d exceeds number of nonzero elements", j, c[j]+1);
90 return false;
91 }
92
93 if (c[j] != jold)
94 {
95 for (octave_idx_type i = jold+1; i < c[j]; i++)
96 {
97 if (r[i] < r[i-1])
98 {
99 (*current_liboctave_error_handler)
100 ("invalid sparse matrix: ridx elements must appear in ascending order for each column");
101 return false;
102 }
103
104 if (r[i] >= nrows)
105 {
106 (*current_liboctave_error_handler)
107 ("invalid sparse matrix: ridx[%d] = %d out of range",
108 i, r[i]+1);
109 return false;
110 }
111 }
112
113 jold = c[j];
114 }
115 }
116 }
117
118 return true;
119 }
120
61 /* 121 /*
62 ;;; Local Variables: *** 122 ;;; Local Variables: ***
63 ;;; mode: C++ *** 123 ;;; mode: C++ ***
64 ;;; End: *** 124 ;;; End: ***
65 */ 125 */