00001 /* 00002 ccp4_array.h: header file for resizable array implementation. 00003 Copyright (C) 2002 Kevin Cowtan 00004 00005 This library is free software and is distributed under the terms and 00006 conditions of the CCP4 licence agreement as `Part 0' (Annex 2) 00007 software, which is version 2.1 of the GNU Lesser General Public 00008 Licence (LGPL) with the following additional clause: 00009 00010 `You may also combine or link a "work that uses the Library" to 00011 produce a work containing portions of the Library, and distribute 00012 that work under terms of your choice, provided that you give 00013 prominent notice with each copy of the work that the specified 00014 version of the Library is used in it, and that you include or 00015 provide public access to the complete corresponding 00016 machine-readable source code for the Library including whatever 00017 changes were used in the work. (i.e. If you make changes to the 00018 Library you must distribute those, but you do not need to 00019 distribute source or object code to those portions of the work 00020 not covered by this licence.)' 00021 00022 Note that this clause grants an additional right and does not impose 00023 any additional restriction, and so does not affect compatibility 00024 with the GNU General Public Licence (GPL). If you wish to negotiate 00025 other terms, please contact the maintainer. 00026 00027 You can redistribute it and/or modify the library under the terms of 00028 the GNU Lesser General Public License as published by the Free Software 00029 Foundation; either version 2.1 of the License, or (at your option) any 00030 later version. 00031 00032 This library is distributed in the hope that it will be useful, but 00033 WITHOUT ANY WARRANTY; without even the implied warranty of 00034 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00035 Lesser General Public License for more details. 00036 00037 You should have received a copy of the CCP4 licence and/or GNU 00038 Lesser General Public License along with this library; if not, write 00039 to the CCP4 Secretary, Daresbury Laboratory, Warrington WA4 4AD, UK. 00040 The GNU Lesser General Public can also be obtained by writing to the 00041 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00042 MA 02111-1307 USA 00043 */ 00044 00050 /* 00051 CCP4 resizable array implementation. 00052 00053 This defines an object and methods which looks just like a simple C 00054 array, but can be resized at will without incurring excessive 00055 overheads. 00056 00057 A pointer to the desired type is created. Array elements are accessed 00058 from this pointer as normal. Other operations depend on macros which 00059 extract the stored type from the type of the pointer. 00060 00061 The array is managed with a header, which is positioned before the 00062 beginning of the array. The malloc'ed memory area starts at the 00063 beginning of this header. However the pointer to the header is not 00064 stored, but rather derived from the array pointer whenever it is 00065 required. 00066 00067 Arrays have a size and a capacity. The size is the number of elements 00068 in use, and the capacity is the number of elements available before a 00069 new memory allocation is required. When new memory is required, an 00070 excess is reqested to allow the array to grow further before 00071 performing another malloc. 00072 00073 If the precise amount of memory is known, the capacity can be 00074 controlled directly using the 'reserve' macro. 00075 00076 Example: to handle an array of type mytype: 00077 00078 \code 00079 int i; 00080 mytype x,y; 00081 mytype *array; 00082 00083 ccp4array_new(array); 00084 00085 ccp4array_append_n(array, x, 3); 00086 00087 for ( i = 0; i < 3; i++ ) y = array[i]; 00088 00089 ccp4array_free(array); 00090 \endcode 00091 */ 00092 00093 #ifndef __CCP4_ARRAY_INC 00094 #define __CCP4_ARRAY_INC 00095 00096 #ifdef __cplusplus 00097 extern "C" { 00098 #endif 00099 00100 #include <stdlib.h> 00101 #include <string.h> 00102 static char rcsidha[] = "$Id: ccp4__array_8h-source.html,v 1.8 2004/07/05 14:36:54 mdw Exp $"; 00103 00105 typedef const void *ccp4_constptr; 00107 typedef char *ccp4_byteptr; 00109 typedef void *ccp4_ptr; 00110 00112 typedef struct ccp4array_base_ { 00113 int size, capacity; 00114 } ccp4array_base; 00115 00121 #define ccp4array_new(v) ccp4array_new_((ccp4_ptr*)(&v)) 00122 00129 #define ccp4array_new_size(v,s) ccp4array_new_size_((ccp4_ptr*)(&v),s,sizeof(*v)) 00130 00139 #define ccp4array_resize(v,s) ccp4array_resize_((ccp4_ptr*)(&v),s,sizeof(*v)) 00140 00149 #define ccp4array_reserve(v,s) ccp4array_reserve_((ccp4_ptr*)(&v),s,sizeof(*v)) 00150 00157 #define ccp4array_append(v,d) ccp4array_append_((ccp4_ptr*)(&v),(ccp4_constptr)(&d),sizeof(*v)) 00158 00166 #define ccp4array_append_n(v,d,n) ccp4array_append_n_((ccp4_ptr*)(&v),(ccp4_constptr)(&d),n,sizeof(*v)) 00167 00175 #define ccp4array_append_list(v,l,n) ccp4array_append_list_((ccp4_ptr*)(&v),(ccp4_constptr)l,n,sizeof(*v)) 00176 00184 #define ccp4array_insert(v,i,d) ccp4array_insert_((ccp4_ptr*)(&v),i,(ccp4_constptr)(&d),sizeof(*v)) 00185 00192 #define ccp4array_delete_ordered(v,i) ccp4array_delete_ordered_((ccp4_ptr*)(&v),i,sizeof(*v)) 00193 00199 #define ccp4array_delete(v,i) ccp4array_delete_((ccp4_ptr*)(&v),i,sizeof(*v)) 00200 00205 #define ccp4array_delete_last(v) ccp4array_delete_last_((ccp4_ptr*)(&v),sizeof(*v)) 00206 00211 #define ccp4array_size(v) ccp4array_size_((ccp4_constptr*)(&v)) 00212 00217 #define ccp4array_free(v) ccp4array_free_((ccp4_ptr*)(&v)) 00218 00222 ccp4_ptr ccp4array_new_(ccp4_ptr *p); 00226 ccp4_ptr ccp4array_new_size_(ccp4_ptr *p, const int size, const size_t reclen); 00230 void ccp4array_resize_(ccp4_ptr *p, const int size, const size_t reclen); 00234 void ccp4array_reserve_(ccp4_ptr *p, const int size, const size_t reclen); 00238 void ccp4array_append_(ccp4_ptr *p, ccp4_constptr data, const size_t reclen); 00242 void ccp4array_append_n_(ccp4_ptr *p, ccp4_constptr data, const int n, const size_t reclen); 00246 void ccp4array_append_list_(ccp4_ptr *p, ccp4_constptr data, const int n, const size_t reclen); 00250 void ccp4array_insert_(ccp4_ptr *p, const int i, ccp4_constptr data, const size_t reclen); 00254 void ccp4array_delete_ordered_(ccp4_ptr *p, const int i, const size_t reclen); 00258 void ccp4array_delete_(ccp4_ptr *p, const int i, const size_t reclen); 00262 void ccp4array_delete_last_(ccp4_ptr *p, const size_t reclen); 00266 int ccp4array_size_(ccp4_constptr *p); 00270 void ccp4array_free_(ccp4_ptr *p); 00271 00272 #ifdef __cplusplus 00273 } 00274 #endif 00275 00276 #endif /* __CCP4_ARRAY_INC */