1 /*******************************************************************************
2 *
3 *       This file is part of the General Hidden Markov Model Library,
4 *       GHMM version __VERSION__, see http://ghmm.org
5 *
6 *       Filename: ghmm/ghmm/linkedlist.c
7 *       Authors:  Matthias Heinig
8 *
9 *       Copyright (C) 1998-2004 Alexander Schliep
10 *       Copyright (C) 1998-2001 ZAIK/ZPR, Universitaet zu Koeln
11 *       Copyright (C) 2002-2004 Max-Planck-Institut fuer Molekulare Genetik,
12 *                               Berlin
13 *
14 *       Contact: schliep@ghmm.org
15 *
16 *       This library is free software; you can redistribute it and/or
17 *       modify it under the terms of the GNU Library General Public
18 *       License as published by the Free Software Foundation; either
19 *       version 2 of the License, or (at your option) any later version.
20 *
21 *       This library is distributed in the hope that it will be useful,
22 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
23 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 *       Library General Public License for more details.
25 *
26 *       You should have received a copy of the GNU Library General Public
27 *       License along with this library; if not, write to the Free
28 *       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 *
31 *       This file is version $Revision: 1442 $
32 *                       from $Date: 2005-10-12 13:09:41 -0400 (Wed, 12 Oct 2005) $
33 *             last change by $Author: grunau $.
34 *
35 *******************************************************************************/
36 
37 #ifdef HAVE_CONFIG_H
38 #  include "../config.h"
39 #endif
40 
41 #include <stdlib.h>
42 
43 #include "mes.h"
44 #include "ghmm_internals.h"
45 
ighmm_list_append(i_list * list,int val)46 void ighmm_list_append(i_list * list, int val){
47   i_el * last;
48   i_el * el;
49   el = ighmm_list_init_el(val);
50   if (list->first == NULL) {
51     list->first = el;
52     list->last = el;
53   }
54   else {
55     last = list->last;
56     last->next = el;
57     list->last = el;
58   }
59   list->length++;
60 }
61 
ighmm_list_insert(i_list * list,int val)62 void ighmm_list_insert(i_list * list, int val) {
63   i_el * first;
64   i_el * el;
65   el = ighmm_list_init_el(val);
66   if (list->first == NULL) {
67     list->first = el;
68     list->last = el;
69   }
70   else {
71     first = list->first;
72     el->next = first;
73     list->first = el;
74   }
75   list->length++;
76 }
77 
ighmm_list_print(i_list * list)78 void ighmm_list_print(i_list * list) {
79   i_el * el = list->first;
80   printf("LIST : ");
81   while(el != NULL) {
82     printf("%i, ", el->val);
83     el = el->next;
84   }
85   printf("\n");
86 }
87 
ighmm_list_to_array(i_list * list)88 int * ighmm_list_to_array(i_list * list) {
89 #define CUR_PROC "ighmm_list_to_array"
90   int counter = 0;
91   int * array;
92   i_el * el;
93   ARRAY_CALLOC (array, list->length);
94   el = list->first;
95   while(el != NULL) {
96     array[counter] = el->val;
97     el = el->next;
98     counter++;
99   }
100   return array;
101 STOP:     /* Label STOP from ARRAY_[CM]ALLOC */
102   m_free(array);
103   return NULL;
104 #undef CUR_PROC
105 }
106 
ighmm_list_init_list()107 i_list * ighmm_list_init_list() {
108 #define CUR_PROC "ighmm_list_init_list"
109   i_list * list;
110 
111   ARRAY_CALLOC (list, 1);
112   list->first = NULL;
113   list->last = NULL;
114   list->length = 0;
115   return list;
116 STOP:     /* Label STOP from ARRAY_[CM]ALLOC */
117   ighmm_list_free(list);
118   return NULL;
119 #undef CUR_PROC
120 }
121 
ighmm_list_init_el(int val)122 i_el * ighmm_list_init_el(int val) {
123 #define CUR_PROC "ighmm_list_init_el"
124   i_el * el;
125   ARRAY_CALLOC (el, 1);
126   el->next = NULL;
127   el->val = val;
128   return el;
129 
130 STOP:     /* Label STOP from ARRAY_[CM]ALLOC */
131   free(el);
132   return NULL;
133 #undef CUR_PROC
134 }
135 
ighmm_list_free(i_list * list)136 int ighmm_list_free(i_list * list) {
137   i_el * el;
138   i_el * next;
139   el = list->first;
140   while(el != NULL) {
141     next = el->next;
142     free(el);
143     el = next;
144   }
145   return 0;
146 }
147