1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*********************************************************************************************
Lutefisk is software for de novo sequencing of peptides from tandem mass spectra.
Copyright (C) 1995 Richard S. Johnson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Contact:
Richard S Johnson
4650 Forest Ave SE
Mercer Island, WA 98040
jsrichar@alum.mit.edu
*********************************************************************************************/
/* ------------------------------------------------------------------------------------
// ListRoutines.c -- General list routines. Adapted from an idea from Ben Halpern.
// Written by JAT 1995;
// ------------------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include "ListRoutines.h"
/*------------------------------------------------------------------------------------
// CREATE NEW LIST -- Allocate and initialize a list objects of size sizeofobject.
// For tfoolist *foolist, use: foolist = CreateNewList(sizeof(tfoo), 10 , 10);
// A pointer to the list is returned. If the list could not be created then NULL is returned.
*/
void *CreateNewList(INT_4 sizeofobject, INT_4 initialNum, INT_4 growNum) {
tlist *list = nil;
list = (tlist *) malloc(sizeof(tlist));
if ( nil == list ) return nil;
list->numObjects = 0;
list->limit = initialNum;
list->sizeofobject = sizeofobject;
list->growNum = growNum;
list->object = (char*) malloc( (size_t)(initialNum * sizeofobject) );
if ( nil == list->object ) {
free( list );
return nil;
}
return list;
}
/*------------------------------------------------------------------------------------
// COPY LIST -- Make a copy of the list.
*/
void *CopyList(void *voidlist)
{
tlist *inList = nil;
tlist *outList = nil;
inList = (tlist *) voidlist;
outList = (tlist *) CreateNewList(inList->sizeofobject, inList->numObjects, inList->growNum);
if ( nil == outList ) return nil;
outList->numObjects = inList->numObjects;
memcpy(&(outList->object[0]), &(inList->object[0]),
(size_t)(outList->sizeofobject * outList->numObjects));
return outList;
}
/*------------------------------------------------------------------------------------
// ADD TO LIST -- Add an object of the appropriate size to the list. For tfoolist
// *foolist which was obtained with CreateNewList, and tfoo *foo,
// use: if ( !AddToList(foo, foolist) ) goto finishUp; The function returns true if
// the object was successfully added to the list and false if it was not.
*/
char AddToList(void *voidobject, void *voidlist) {
char *newobjects;
toObject *object;
tlist *list;
INT_4 sizeoflist;
object = (toObject *) voidobject;
list = (tlist *) voidlist;
sizeoflist = list->numObjects * list->sizeofobject;
if (list->numObjects >= list->limit) {
/* Boost the size of the list if we are going to overflow. */
newobjects = (char*) malloc((list->limit + list->growNum) * list->sizeofobject);
if ( newobjects ) {
memcpy(newobjects, list->object, (size_t)sizeoflist);
list->limit += list->growNum;
free( list->object );
list->object = newobjects;
}
else return false;
}
memcpy(&(list->object[0]) + (size_t)sizeoflist, object, (size_t)(list->sizeofobject));
list->numObjects++;
return true;
}
/*------------------------------------------------------------------------------------
// REMOVE FROM LIST -- Remove the object of the given index from the list.
*/
void RemoveFromList(INT_4 index, void *voidlist) {
tlist *list;
INT_4 sizeofobjectstomove;
INT_4 sourceoffset, targetoffset;
list = (tlist *) voidlist;
targetoffset = index * list->sizeofobject;
sourceoffset = targetoffset + list->sizeofobject;
sizeofobjectstomove = list->numObjects * list->sizeofobject - sourceoffset;
memmove(list->object + targetoffset, list->object + sourceoffset, (size_t)sizeofobjectstomove);
list->numObjects--;
}
/*------------------------------------------------------------------------------------
// TRIM LIST -- Free up unused memory and reset limit accordingly.
*/
void TrimList(void *voidlist) {
tlist *list;
INT_4 sizeoflist;
list = (tlist *) voidlist;
sizeoflist = list->numObjects * list->sizeofobject;
list->object = (char *) realloc(list->object, (size_t)sizeoflist );
list->limit = list->numObjects;
}
/*------------------------------------------------------------------------------------
// DISPOSE LIST -- Dispose of the list.
*/
void DisposeList(void *voiddeadlist) {
tlist *deadlist;
if ( nil == voiddeadlist ) return; /* Don't try to dispose of it if it doesn't exist.
Nasty things could happen. */
deadlist = (tlist *) voiddeadlist;
free( deadlist->object );
free( deadlist );
}
|