4 * Copyright (c) 2008-2012 BGI-Shenzhen <soap at genomics dot org dot cn>.
6 * This file is part of SOAPdenovo.
8 * SOAPdenovo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * SOAPdenovo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with SOAPdenovo. If not, see <http://www.gnu.org/licenses/>.
25 STACK
* createStack ( int num_items
, size_t unit_size
)
27 STACK
* newStack
= ( STACK
* ) malloc ( 1 * sizeof ( STACK
) );
28 newStack
->block_list
= NULL
;
29 newStack
->items_per_block
= num_items
;
30 newStack
->item_size
= unit_size
;
35 void emptyStack ( STACK
* astack
)
37 BLOCK_STARTER
* block
;
39 if ( !astack
|| !astack
->block_list
)
44 block
= astack
->block_list
;
51 astack
->block_list
= block
;
53 astack
->index_in_block
= 0;
56 void freeStack ( STACK
* astack
)
58 BLOCK_STARTER
* ite_block
, *temp_block
;
65 ite_block
= astack
->block_list
;
69 while ( ite_block
->next
)
71 ite_block
= ite_block
->next
;
77 temp_block
= ite_block
;
78 ite_block
= ite_block
->prev
;
79 free ( ( void * ) temp_block
);
82 free ( ( void * ) astack
);
85 void stackBackup ( STACK
* astack
)
87 astack
->block_backup
= astack
->block_list
;
88 astack
->index_backup
= astack
->index_in_block
;
89 astack
->item_c_backup
= astack
->item_c
;
92 void stackRecover ( STACK
* astack
)
94 astack
->block_list
= astack
->block_backup
;
95 astack
->index_in_block
= astack
->index_backup
;
96 astack
->item_c
= astack
->item_c_backup
;
99 void * stackPop ( STACK
* astack
)
101 BLOCK_STARTER
* block
;
103 if ( !astack
|| !astack
->block_list
|| !astack
->item_c
)
109 block
= astack
->block_list
;
111 if ( astack
->index_in_block
== 1 )
115 astack
->block_list
= block
->next
;
116 astack
->index_in_block
= astack
->items_per_block
;
120 astack
->index_in_block
= 0;
124 return ( void * ) ( ( void * ) block
+ sizeof ( BLOCK_STARTER
) );
127 return ( void * ) ( ( void * ) block
+ sizeof ( BLOCK_STARTER
) + astack
->item_size
* ( --astack
->index_in_block
) );
130 void * stackPush ( STACK
* astack
)
132 BLOCK_STARTER
* block
;
141 if ( !astack
->block_list
|| ( astack
->index_in_block
== astack
->items_per_block
&& !astack
->block_list
->prev
) )
143 block
= malloc ( sizeof ( BLOCK_STARTER
) + astack
->items_per_block
* astack
->item_size
);
146 if ( astack
->block_list
)
148 astack
->block_list
->prev
= block
;
151 block
->next
= astack
->block_list
;
152 astack
->block_list
= block
;
153 astack
->index_in_block
= 1;
154 return ( void * ) ( ( void * ) block
+ sizeof ( BLOCK_STARTER
) );
156 else if ( astack
->index_in_block
== astack
->items_per_block
&& astack
->block_list
->prev
)
158 astack
->block_list
= astack
->block_list
->prev
;
159 astack
->index_in_block
= 1;
160 return ( void * ) ( ( void * ) astack
->block_list
+ sizeof ( BLOCK_STARTER
) );
163 block
= astack
->block_list
;
164 return ( void * ) ( ( void * ) block
+ sizeof ( BLOCK_STARTER
) + astack
->item_size
* astack
->index_in_block
++ );