1 /*
2
3 HyPhy - Hypothesis Testing Using Phylogenies.
4
5 Copyright (C) 1997-now
6 Core Developers:
7 Sergei L Kosakovsky Pond (sergeilkp@icloud.com)
8 Art FY Poon (apoon42@uwo.ca)
9 Steven Weaver (sweaver@temple.edu)
10
11 Module Developers:
12 Lance Hepler (nlhepler@gmail.com)
13 Martin Smith (martin.audacis@gmail.com)
14
15 Significant contributions from:
16 Spencer V Muse (muse@stat.ncsu.edu)
17 Simon DW Frost (sdf22@cam.ac.uk)
18
19 Permission is hereby granted, free of charge, to any person obtaining a
20 copy of this software and associated documentation files (the
21 "Software"), to deal in the Software without restriction, including
22 without limitation the rights to use, copy, modify, merge, publish,
23 distribute, sublicense, and/or sell copies of the Software, and to
24 permit persons to whom the Software is furnished to do so, subject to
25 the following conditions:
26
27 The above copyright notice and this permission notice shall be included
28 in all copies or substantial portions of the Software.
29
30 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
33 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
34 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
35 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
36 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37
38 */
39
40 #include "hy_strings.h"
41 #include "hy_string_buffer.h"
42 #include "parser.h"
43
44 #include <string.h>
45 #include <stdio.h>
46 #include <ctype.h>
47 #include <math.h>
48 #include <limits.h>
49
50 //______________________________________________________________
51
_AVLListXL(_SimpleList * d)52 _AVLListXL::_AVLListXL (_SimpleList* d):_AVLList(d)
53 {
54 }
55
56 //______________________________________________________________
57
GetXtra(long d) const58 BaseRef _AVLListXL::GetXtra (long d) const {
59 return xtraD.GetItem(d);
60 }
61
62
63 //______________________________________________________________
64
GetDataByKey(BaseRefConst key) const65 BaseRef _AVLListXL::GetDataByKey(BaseRefConst key) const {
66 long f = Find (key);
67 if (f < 0L) {
68 return nil;
69 }
70 return GetXtra(f);
71 }
72
73
74 //______________________________________________________________
75
SetXtra(long i,BaseRef d,bool dup)76 void _AVLListXL::SetXtra (long i, BaseRef d, bool dup) {
77 xtraD.Replace (i,d, dup);
78 }
79
80
81 //______________________________________________________________
82
toStr(unsigned long)83 BaseRef _AVLListXL::toStr (unsigned long)
84 {
85 _StringBuffer * str = new _StringBuffer (128L);
86
87 if (countitems() == 0) {
88 (*str) << "Empty Associative List";
89 } else {
90 _SimpleList hist;
91 long ls, cn;
92
93 cn = Traverser (hist,ls,root);
94
95 while (cn>=0) {
96 (*str) << (_String*)Retrieve (cn)
97 << " : ";
98 str->AppendNewInstance((_String*)GetXtra (cn)->toStr());
99 (*str) << '\n';
100
101 cn = Traverser (hist,ls);
102 }
103 }
104
105 return str;
106 }
107 //______________________________________________________________
108
PushPairCopyKey(_String const key,BaseRef data)109 _AVLListXL& _AVLListXL::PushPairCopyKey (_String const key, BaseRef data) {
110 UpdateValue (new _String (key), data, false, false);
111 return *this;
112 }
113 //______________________________________________________________
114
PushPair(_String * key,BaseRef data)115 _AVLListXL& _AVLListXL::PushPair (_String* key, BaseRef data) {
116 if (UpdateValue (key, data, false, false) >= 0) {
117 DeleteObject (key);
118 }
119 return *this;
120 }
121
122 //______________________________________________________________
123
UpdateValue(BaseRef b,BaseRef d,bool do_copy,bool copy_key)124 long _AVLListXL::UpdateValue(BaseRef b, BaseRef d, bool do_copy, bool copy_key) {
125 long exists = Find (b);
126 if (exists >= 0) {
127 SetXtra (exists, d, do_copy);
128 } else {
129 Insert (copy_key?b->makeDynamic():b,(long)d, do_copy);
130 }
131 return exists;
132 }
133
134 //______________________________________________________________
135
Clear(bool cL)136 void _AVLListXL::Clear (bool cL)
137 {
138 xtraD.Clear();
139 _AVLList::Clear(cL);
140 }
141
142
143
144 //______________________________________________________________
145
InsertData(BaseRef b,long xl,bool cp)146 long _AVLListXL::InsertData (BaseRef b, long xl, bool cp)
147 {
148 long w = (long)emptySlots.lLength - 1,
149 n;
150
151 BaseRef x = (BaseRef)xl;
152
153 if (w>=0) {
154 n = emptySlots.list_data[w];
155 emptySlots.Delete (w);
156 leftChild.list_data[n] = -1;
157 rightChild.list_data[n] = -1;
158 balanceFactor.list_data[n] = 0;
159 ((BaseRef*)xtraD.list_data)[n] = x;
160 if (cp) {
161 x->AddAReference();
162 }
163 ((BaseRef*)dataList->list_data)[n] = b;
164 } else {
165 n = dataList->lLength;
166 dataList->InsertElement (b,-1,false,false);
167 leftChild << -1;
168 rightChild << -1;
169 balanceFactor << 0;
170 xtraD << x;
171 if (!cp) {
172 x->RemoveAReference();
173 }
174 }
175 return n;
176 }
177
178
179
180 //______________________________________________________________
181
DeleteXtra(long i)182 void _AVLListXL::DeleteXtra (long i) {
183 DeleteObject (((BaseRef*)xtraD.list_data)[i]);
184 (((BaseRef*)xtraD.list_data)[i]) = nil;
185 }
186
187