1 /*  instance.c  */
2 
3 #include "../DV.h"
4 
5 /*--------------------------------------------------------------------*/
6 /*
7    -----------------------------------------------
8    return 1 if the entries are owned by the object
9    return 0 otherwise
10 
11    created -- 96jun22, cca
12    -----------------------------------------------
13 */
14 int
DV_owned(DV * dv)15 DV_owned (
16    DV   *dv
17 ) {
18 if ( dv == NULL ) {
19    fprintf(stderr, "\n fatal error in DV_owned(%p)"
20            "\n bad input\n", dv) ;
21    exit(-1) ;
22 }
23 return(dv->owned) ; }
24 
25 /*--------------------------------------------------------------------*/
26 /*
27    -----------------------
28    return the vector size
29 
30    created -- 95oct06, cca
31    -----------------------
32 */
33 int
DV_maxsize(DV * dv)34 DV_maxsize (
35    DV   *dv
36 ) {
37 if ( dv == NULL ) {
38    fprintf(stderr, "\n fatal error in DV_maxsize(%p)"
39            "\n bad input\n", dv) ;
40    exit(-1) ;
41 }
42 return(dv->maxsize) ; }
43 
44 /*--------------------------------------------------------------------*/
45 /*
46    -----------------------
47    return the vector size
48 
49    created -- 95oct06, cca
50    -----------------------
51 */
52 int
DV_size(DV * dv)53 DV_size (
54    DV   *dv
55 ) {
56 if ( dv == NULL ) {
57    fprintf(stderr, "\n fatal error in DV_size(%p)"
58            "\n bad input\n", dv) ;
59    exit(-1) ;
60 }
61 return(dv->size) ; }
62 
63 /*--------------------------------------------------------------------*/
64 /*
65    -------------------------------------------------
66    return the loc'th entry of a vector.
67    note: if loc is out of range then 0.0 is returned
68 
69    created -- 96jun29, cca
70    -------------------------------------------------
71 */
72 double
DV_entry(DV * dv,int loc)73 DV_entry (
74    DV    *dv,
75    int   loc
76 ) {
77 if ( dv == NULL || dv->vec == NULL ) {
78    fprintf(stderr, "\n fatal error in DV_entry(%p)"
79            "\n bad input\n", dv) ;
80    exit(-1) ;
81 }
82 if ( loc < 0 || loc >= dv->size ) {
83    return(0.0) ;
84 } else {
85    return(dv->vec[loc]) ;
86 }
87 }
88 
89 /*--------------------------------------------------------------------*/
90 /*
91    ----------------------------------------------
92    return a pointer to the object's entries array
93 
94    created -- 95oct06, cca
95    ----------------------------------------------
96 */
97 double *
DV_entries(DV * dv)98 DV_entries (
99    DV   *dv
100 ) {
101 if ( dv == NULL ) {
102    fprintf(stderr, "\n fatal error in DV_entries(%p)"
103            "\n bad input\n", dv) ;
104    exit(-1) ;
105 }
106 return(dv->vec) ; }
107 
108 /*--------------------------------------------------------------------*/
109 /*
110    --------------------------------------------
111    fill *psize with the vector's size
112    and *pentries with the address of the vector
113 
114    created -- 95oct06, cca
115    --------------------------------------------
116 */
117 void
DV_sizeAndEntries(DV * dv,int * psize,double ** pentries)118 DV_sizeAndEntries (
119    DV       *dv,
120    int      *psize,
121    double   **pentries
122 ) {
123 if ( dv == NULL || psize == NULL || pentries == NULL ) {
124    fprintf(stderr, "\n fatal error in DV_sizeAndEntries(%p,%p,%p)"
125            "\n bad input\n", dv, psize, pentries) ;
126    exit(-1) ;
127 }
128 *psize    = dv->size ;
129 *pentries = dv->vec  ;
130 
131 return ; }
132 
133 /*--------------------------------------------------------------------*/
134 /*
135    ---------------------------
136    set and entry in the vector
137 
138    created -- 96jul14, cca
139    ---------------------------
140 */
141 void
DV_setEntry(DV * dv,int loc,double value)142 DV_setEntry (
143    DV       *dv,
144    int      loc,
145    double   value
146 ) {
147 /*
148    ---------------
149    check the input
150    ---------------
151 */
152 if ( dv == NULL || loc < 0 ) {
153    fprintf(stderr, "\n fatal error in DV_setEntry(%p,%d,%f)"
154            "\n bad input\n", dv, loc, value) ;
155    exit(-1) ;
156 }
157 if ( loc >= dv->maxsize ) {
158    int newmaxsize = (int) 1.25*dv->maxsize ;
159    if ( newmaxsize < 10 ) {
160       newmaxsize = 10 ;
161    }
162    if ( loc >= newmaxsize ) {
163       newmaxsize = loc + 1 ;
164    }
165    DV_setMaxsize(dv, newmaxsize) ;
166 }
167 if ( loc >= dv->size ) {
168    dv->size = loc + 1 ;
169 }
170 dv->vec[loc] = value ;
171 
172 return ; }
173 
174 /*--------------------------------------------------------------------*/
175