File: nlist.cpp

package info (click to toggle)
plink 1.07%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,040 kB
  • sloc: cpp: 69,728; makefile: 123; sh: 12
file content (264 lines) | stat: -rw-r--r-- 5,202 bytes parent folder | download | duplicates (7)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264


//////////////////////////////////////////////////////////////////
//                                                              //
//           PLINK (c) 2005-2009 Shaun Purcell                  //
//                                                              //
// This file is distributed under the GNU General Public        //
// License, Version 2.  Please see the file COPYING for more    //
// details                                                      //
//                                                              //
//////////////////////////////////////////////////////////////////

#include <string>
#include <vector>
#include <algorithm>

#include "nlist.h"

vector<string> NList::deparseStringList(string input)
{
  return tokenize(input);
}

vector<int> NList::deparseNumberList(string input)
{

  // We have a string that should contain integers, possibily
  // separated by "," and "-" characters

  // Use 1-N coding up until the last moment, instead of 
  // standard 0..N-1 coding

  firstWord = "1";
  lastWord = int2str(maxcat);

  vector<string> tok = tokenize(input);
  vector<int> nums;
  for (int i=0; i<tok.size(); i++)
    {
      int j;
      if ( tok[i] == range_string )
	nums.push_back(-1);
      else
	{
	  if ( ! from_string<int>( j, tok[i] , std::dec))
	    continue;
	  nums.push_back(j);
	}
    }
  return expandNumberList(nums);
}


vector<int> NList::deparseStringList(string input, map<string,int> * mapping)
{
  
  // Assume map contains a 0..N-1 coding in the map
  
  map<string,int>::iterator im = mapping->begin();
  while ( im != mapping->end() )
    {
      int i = im->second;
      
      if ( i == 0 )
	firstWord = im->first;
      
      if ( i == maxcat - 1 ) 
	lastWord = im->first;

      ++im;
    }


  ////////////////////////////////////////////////////////////////
  // Convert string codes to numbers, then call deparseNumberList
  
  // But incrememnt to 1..N coding here, i.e. so that it is 
  // similar process to the human-friendly 1..N coding for a
  // numeric list

  vector<string> tok = tokenize(input);
  vector<int> nums;
  for (int i=0; i<tok.size(); i++)
    {
      map<string,int>::iterator im = mapping->find(tok[i]);
      if ( im != mapping->end() )
	nums.push_back(im->second + 1);
      else if ( tok[i] == range_string )
	nums.push_back(-1);
      else 
	error("Cannot find value: " + tok[i] + "\n");
    }
  return expandNumberList(nums);  
}



vector<int> NList::expandNumberList(vector<int> & nlist)
{

  // Convert 
  vector<int> n;
  
  //////////////////
  // Check ranges

  for (int i=0; i<nlist.size(); i++)
    {
      if ( nlist[i] == -1 ) 
	{
	  if ( nlist[i-1] > nlist[i+1] )
	    {
	      int tmp = nlist[i+1];
	      nlist[i+1] = nlist[i-1];
	      nlist[i-1] = tmp;
	    }
	}
    }
  

  //////////////////
  // Expand ranges
  
  for (int i=0; i<nlist.size(); i++)
    {
      
      if ( nlist[i]>0 ) 
 	{
	  // Only add valid codes
	  if ( nlist[i] <= maxcat )
	    n.push_back(nlist[i]);	  
 	}
      else
	{
	  int start = nlist[i-1]+1;
 	  int end = nlist[i+1]-1;
	  if ( end > maxcat )
	    end = maxcat;

 	  for (int j=start; j<=end; j++)
 	    n.push_back(j);
 	}
    }

  // Sort and uniquify
  stable_sort(n.begin(),n.end());
  vector<int>::iterator ne = unique(n.begin(),n.end());
  n.erase(ne, n.end());

  // Shift to 0..N-1 coding
  for (int i=0; i<n.size(); i++)
    --n[i];


  // Or is it a negative complement?
  if ( negmode )
    {

      vector<bool> k(maxcat,false);
      for (int i=0; i<n.size(); i++)
	k[ n[i] ] = true;
      
      vector<int> n2 = n;
      n.clear();
      
      for (int i=0; i<maxcat; i++)
	{
	  if ( ! k[i] ) 
	    {
	      n.push_back(i);
	    }
	}
    }
 
  return n;

}


vector<string> NList::tokenize(string s)
{

  vector<string> t;

  string word = "";
  bool range_set = false;
  bool number_given = false;
  
  for (int i=0; i<s.length(); i++)
    {      

      // Skip spaces
      if (s[i] == ' ') 
	continue;
      
      if ( s[i] == delimit_char || s[i] == range_char )
	{

	  // If we've built up a meaningful word,
	  // we can add it now

	  if ( word != "" )
	    {
	      t.push_back(word);
	      word = "";
	      number_given = true;
	      range_set = false;
	    }
	  
	  
	  // Start a range? 
	  if ( s[i]== range_char )
	    {

	      // Can't do twice
	      if (range_set)
		error("Invalid list: " + s + "\n");
	      
	      range_set = true;
	      
	      // But was a start point given? Make one 
	      // if not

	      if ( ! number_given )
		{
		  t.push_back("1");
		  word="";
		}
	      
	      number_given = false;
	      
	      t.push_back(range_string);
	      
	    }
	  
	  // And if the "-" is also the last 
	  // character, we need an end point
	  
	  if ( s[i]== range_char && i == s.length()-1 )
	    t.push_back( lastWord );
	  
	  // Here, fill in here
	  if ( s[i]== delimit_char )
	    {
	      number_given = false;
	      if ( range_set )
		{
		  range_set = false;
		  t.push_back( lastWord );
		}	  
	    } 
	}
      else if ( i == s.length()-1 )
	{
	  // Is this a last number?
	  t.push_back(word+s[i]);
	  number_given = true;
	}
      else
	word += s[i];
    }

  return t;
}