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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* segemehl - a read aligner
* Copyright (C) 2008-2017 Steve Hoffmann and Christian Otto
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/**
* vstack.c
* implementation of a simple stack for objects of defined size
*
* @author Christian Otto
* @email christian@bioinf.uni-leipzig.de
* @company Bioinformatics, University of Leipzig
* @date Fri Oct 10 11:37:36 CEST 2008
*/
/*
* SVN
* Revision of last commit: $Rev: 72 $
* Author: $Author: steve $
* Date: $Date: 2008-10-28 18:14:42 +0100 (Tue, 28 Oct 2008) $
* Id: $Id$
* Url: $URL$
*/
#include <stdio.h>
#include <stdlib.h>
#include "basic-types.h"
#include "debug.h"
#include "vstack.h"
/*----------------------------- bl_vstackInit ----------------------------------
*
* @brief init vstack
* @author Christian Otto
*
*/
void bl_vstackInit(VStack *s, Lint allocelem, size_t sizeofelem){
if (allocelem <= 0){
DBG("vstack.c: Attempt to initialize a vstack of size %d. Exit forced.\n",
allocelem);
exit(-1);
}
if (sizeofelem <= 0){
DBG("vstack.c: Attempt to initialize a vstack with sizeofelem %d.\
Exit forced.\n", sizeofelem);
exit(-1);
}
s->stackspace = malloc(allocelem * sizeofelem);
if (s->stackspace == NULL){
DBG("vstack.c: Memory allocation failed. Exit forced.\n", NULL);
exit(-1);
}
s->allocelem = allocelem;
s->top = -1;
s->sizeofelem = sizeofelem;
}
/*--------------------------- bl_vstackDestruct --------------------------------
*
* @brief destruct vstack,
* remove method for elems as parameter possible
* @author Christian Otto
*
*/
void bl_vstackDestruct(VStack *s, void (*rmv)(void*)){
Lint i;
char *p;
if (rmv != NULL){
p = (char *) s->stackspace;
for(i = 0; i <= s->top; i++){
rmv(p + (i * s->sizeofelem));
}
}
free(s->stackspace);
s->allocelem = 0;
s->top = 0;
s->sizeofelem = 0;
}
/*---------------------------- bl_vstackIsEmpty --------------------------------
*
* @brief returns if the vstack is empty
* @author Christian Otto
*
*/
BOOL bl_vstackIsEmpty(VStack *s){
return (s->top < 0);
}
/*----------------------------- bl_vstackPush ----------------------------------
*
* @brief pushs elements on the top of the vstack
* @author Christian Otto
*
*/
void bl_vstackPush(VStack *s, void *elem){
char *p;
if (s->top >= s->allocelem - 1){
s->stackspace = realloc(s->stackspace,
s->sizeofelem * (s->allocelem + BASEINC));
if (s->stackspace == NULL || BASEINC <= 0){
DBG("vstack.c: Memory reallocation failed. Exit forced.\n", NULL);
exit(-1);
}
s->allocelem += BASEINC;
}
s->top++;
p = (char *) s->stackspace;
memmove(p + (s->top * s->sizeofelem), elem, s->sizeofelem);
}
/*------------------------------ bl_vstackTop ----------------------------------
*
* @brief returns top of the vstack as pointer
* @author Christian Otto
*
*/
void* bl_vstackTop(VStack *s){
char *p;
if (bl_vstackIsEmpty(s)){
return NULL;
}
p = (char *) s->stackspace;
return (p + (s->top * s->sizeofelem));
}
/*------------------------------ bl_vstackTopN ---------------------------------
*
* @brief returns Nth highest object of the vstack
* with N = 0,..,numofelems - 1
* @author Christian Otto
*
*/
void* bl_vstackTopN(VStack *s, Lint n){
char *p;
if (bl_vstackIsEmpty(s) || n < 0 || n > s->top){
return NULL;
}
p = (char *) s->stackspace;
return (p + (s->top - n) * s->sizeofelem);
}
/*------------------------------ bl_vstackPop ----------------------------------
*
* @brief pops the top of the vstack as copy
* and removes it from the vstack
* @author Christian Otto
*
*/
void* bl_vstackPop(VStack *s, void (*rmv)(void*)){
char *p, *elem;
if (bl_vstackIsEmpty(s)){
return NULL;
}
p = (char *) s->stackspace;
elem = (char *) malloc(s->sizeofelem);
memmove(elem, p + (s->top * s->sizeofelem), s->sizeofelem);
if (rmv != NULL){
rmv(p + (s->top * s->sizeofelem));
}
s->top--;
return elem;
}
/*------------------------------ bl_vstackSize ---------------------------------
*
* @brief returns number of elements on the vstack
* @author Christian Otto
*
*/
Lint bl_vstackSize(VStack *s){
return (s->top + 1);
}
|