/[ascend]/trunk/ascend/general/pairlist.c
ViewVC logotype

Contents of /trunk/ascend/general/pairlist.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2018 - (show annotations) (download) (as text)
Wed Apr 29 03:38:10 2009 UTC (15 years ago) by jpye
File MIME type: text/x-csrc
File size: 3326 byte(s)
Fixed compile for new header file locations <ascend/compiler/xxx.h> etc.
1 /*
2 * Ascend pointer pairs module.
3 * by Benjamin Andrew Allan
4 * Created: 10/2006
5 *
6 * This file is part of the Ascend Language Interpreter.
7 *
8 * Copyright (C) 2006 Benjamin Andrew Allan
9 *
10 * The Ascend Language Interpreter is free software; you can redistribute
11 * it and/or modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * The Ascend Language Interpreter is distributed in hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with the program; if not, write to the Free Software Foundation,
22 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
23 * COPYING.
24 *
25 */
26
27 #include <stdarg.h>
28 #include <ascend/utilities/ascConfig.h>
29 #include <ascend/utilities/ascPanic.h>
30 #include <ascend/utilities/ascMalloc.h>
31 #include "pairlist.h"
32 #include "list.h"
33
34 struct pairlist_t {
35 struct gl_list_t *keys;
36 struct gl_list_t *vals;
37 };
38
39
40 struct pairlist_t *
41 pairlist_create(unsigned long capacity)
42 {
43 struct pairlist_t * pl = (struct pairlist_t *)ascmalloc(sizeof(struct pairlist_t));
44 pl->keys = gl_create(capacity);
45 pl->vals = gl_create(capacity);
46 return pl;
47 }
48
49 void *
50 pairlist_keyAt(struct pairlist_t * pl, unsigned long eindex)
51 {
52 assert(pl != NULL);
53 return gl_fetch(pl->keys, eindex);
54 }
55
56 void *
57 pairlist_valueAt(struct pairlist_t * pl, unsigned long eindex)
58 {
59 assert(pl != NULL);
60 return gl_fetch(pl->vals, eindex);
61 }
62
63 unsigned long
64 pairlist_contains(struct pairlist_t * pl, void *key)
65 {
66 unsigned long eindex;
67 assert(pl != NULL);
68 eindex = gl_ptr_search(pl->keys,key,FALSE);
69 return eindex;
70 }
71
72 unsigned long
73 pairlist_append(struct pairlist_t * pl, void *key, void * value)
74 {
75 assert(pl != NULL);
76 gl_append_ptr(pl->keys, key);
77 gl_append_ptr(pl->vals, value);
78 return gl_length(pl->keys);
79 }
80
81 unsigned long pairlist_append_unique(struct pairlist_t * pl, void *key, void * value)
82 {
83 unsigned long eindex;
84 assert(pl != NULL);
85 eindex = pairlist_contains(pl, key);
86 if(eindex==0){
87 pairlist_append(pl,key,value);
88 }
89 return gl_length(pl->keys);
90 }
91
92 void *pairlist_set(struct pairlist_t *pl, void *key, void *value){
93 unsigned long eindex;
94 void *oldvalue = NULL;
95 assert(pl != NULL);
96 eindex = pairlist_contains(pl, key);
97 if(eindex){
98 oldvalue = gl_fetch(pl->vals, eindex);
99 gl_store(pl->vals, eindex, value);
100 }else{
101 pairlist_append(pl,key,value);
102 }
103 return oldvalue;
104 }
105
106 void pairlist_clear(struct pairlist_t * pl)
107 {
108 gl_reset(pl->keys);
109 gl_reset(pl->vals);
110 }
111
112 void pairlist_destroy(struct pairlist_t * pl)
113 {
114 gl_destroy(pl->keys);
115 gl_destroy(pl->vals);
116 pl->keys = pl->vals = NULL;
117 ascfree(pl);
118 }
119
120 struct gl_list_t *pairlist_values_and_destroy(struct pairlist_t *pl){
121 struct gl_list_t *vals = pl->vals;
122 gl_destroy(pl->keys);
123 pl->keys = pl->vals = NULL;
124 ascfree(pl);
125 return vals;
126 }
127
128
129 long pairlist_length(struct pairlist_t * pl)
130 {
131 assert(pl != NULL);
132 return gl_length(pl->keys);
133 }
134 void pairlist_print(FILE *fp, struct pairlist_t * pl)
135 {
136 (void) fp; (void) pl;
137 /* FIXME. pairlist_print impl */
138 }
139

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22