jps 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b28b7b851c5c0a8c437e28c05b21d4a5bc808a7
4
- data.tar.gz: d032f82ba7aa9b9c8759754a63ce7036f7a61fd0
3
+ metadata.gz: ecf21875171d5aa914eb76e1d8ed2e5ffa3d7d85
4
+ data.tar.gz: be6079ef1db2857848bfa48ca937cc1abc5c082d
5
5
  SHA512:
6
- metadata.gz: 73b594670ef6bb4bb5030287e12e98e9366a8e7c0c5cd5b253d7a4f8ec57a7cba76201f3ab24a2c29156cad09d7a12684797eb9519e143c0170c9b48ed696b7d
7
- data.tar.gz: 6a33cdcdcb403f3efd60b7dabaa06efe6d59c557af1aa3effd21bec29c3a43aa3064dd48248ec93fa14dd18d921641adb9555f834af10ae5f3aeabd024055484
6
+ metadata.gz: 0aed6aa02665f99f2fba4ad677d9e3d743f558b4d91d6dc4a3a33a27f260f2f64fce7240752bfe9b06ca1e5123d04743ee827631fe37c965460f5a49bc7345d8
7
+ data.tar.gz: 6dbe99b14ff7dae534feada0e0a00acc5e2abef8fd2e741df9f1f78fe7f397c56929854989c29aa10990de8d551ea584c64e1b869d1cec41cb0bc8748a922373
data/ext/jps/map_jps.cpp CHANGED
@@ -9,9 +9,12 @@
9
9
 
10
10
  using namespace std;
11
11
 
12
- Map *map = NULL;
13
- vector<JPS::Position> *path = NULL;
14
- // std::vector<std::string> out;
12
+ Map *gmap = NULL;
13
+ vector<JPS::Position> gpath;
14
+
15
+ typedef std::vector<Map *> VEC_MAPS;
16
+ typedef std::map<string, VEC_MAPS *> HASH_MAPS;
17
+ HASH_MAPS *hsMaps = NULL;
15
18
 
16
19
  inline unsigned Map::operator()(unsigned x, unsigned y) const
17
20
  {
@@ -32,16 +35,30 @@ void Map::clear()
32
35
  delete[] this->data;
33
36
  }
34
37
 
38
+ void Map::dispose()
39
+ {
40
+ int i,j;
41
+ for(j = 0; j < this->h; ++j)
42
+ {
43
+ for(i = 0; i < this->w; ++i)
44
+ {
45
+ this->data[j][i] = 0;
46
+ }
47
+ }
48
+ }
49
+
50
+
35
51
  void Map::init(int width, int height)
36
52
  {
37
53
  this->w = width;
38
54
  this->h = height;
39
55
  this->data = new int*[height];
56
+
40
57
  int i,j;
41
- for(j = 0; j < height; ++j)
58
+ for(j = 0; j < this->h; ++j)
42
59
  {
43
- this->data[j] = new int[width];
44
- for(i = 0; i < width; ++i)
60
+ this->data[j] = new int[this->w];
61
+ for(i = 0; i < this->w; ++i)
45
62
  {
46
63
  this->data[j][i] = 0;
47
64
  }
@@ -50,42 +67,38 @@ void Map::init(int width, int height)
50
67
 
51
68
  void clearMap()
52
69
  {
53
- if(!map){
54
- delete map;
55
- map = NULL;
70
+ if(gmap){
71
+ recycleMap(gmap);
72
+ gmap = NULL;
56
73
  }
57
74
  }
58
75
 
59
76
  void clearPath()
60
77
  {
61
- if(!path)
62
- {
63
- delete path;
64
- path = NULL;
65
- }
78
+ gpath.clear();
66
79
  }
67
80
 
68
81
  vector<JPS::Position> * findPath(int startX, int startY, int stopX, int stopY)
69
82
  {
70
- if(!map || !path){
83
+ if(!gmap){
71
84
  return NULL;
72
85
  }
73
86
 
74
- path->clear();
87
+ gpath.clear();
75
88
 
76
89
  //开始和结束位置不能是障碍 不然找不到路径
77
90
  //这里先设置成非障碍,查询结束后重置回去
78
- int oldStartFlag = map->data[startY][startX];
79
- int oldStopFlag = map->data[stopY][stopX];
80
- map->data[startY][startX] = 0;
81
- map->data[stopY][stopX] = 0;
91
+ int oldStartFlag = gmap->data[startY][startX];
92
+ int oldStopFlag = gmap->data[stopY][stopX];
93
+ gmap->data[startY][startX] = 0;
94
+ gmap->data[stopY][stopX] = 0;
82
95
 
83
96
  JPS::PathVector waypoints;
84
97
  JPS::PathVector result;
85
98
  waypoints.push_back(JPS::Pos(JPS::PosType(startX), startY));
86
99
  waypoints.push_back(JPS::Pos(JPS::PosType(stopX), stopY));
87
100
 
88
- JPS::Searcher<Map> search(*map);
101
+ JPS::Searcher<Map> search(*gmap);
89
102
  search.findPath(result, waypoints[0], waypoints[1], 1);
90
103
 
91
104
  int curX = startX;
@@ -104,18 +117,18 @@ vector<JPS::Position> * findPath(int startX, int startY, int stopX, int stopY)
104
117
  if(curX != x && curY != y)
105
118
  {
106
119
  JPS::Position pos1;
107
- if(map->data[curY][x] == 0)
120
+ if(gmap->data[curY][x] == 0)
108
121
  {
109
122
  pos1.x = x;
110
123
  pos1.y = curY;
111
- path->push_back(pos1);
124
+ gpath.push_back(pos1);
112
125
  curX = x;
113
126
  }
114
- else if(map->data[y][curX] == 0)
127
+ else if(gmap->data[y][curX] == 0)
115
128
  {
116
129
  pos1.x = curX;
117
130
  pos1.y = y;
118
- path->push_back(pos1);
131
+ gpath.push_back(pos1);
119
132
  curY = y;
120
133
  }
121
134
  }
@@ -123,28 +136,28 @@ vector<JPS::Position> * findPath(int startX, int startY, int stopX, int stopY)
123
136
  JPS::Position pos2;
124
137
  pos2.x = x;
125
138
  pos2.y = y;
126
- path->push_back(pos2);
139
+ gpath.push_back(pos2);
127
140
  curX = x;
128
141
  curY = y;
129
142
  }
130
143
 
131
144
  //查询结束后重置回去
132
- map->data[startY][startX] = oldStartFlag;
133
- map->data[stopY][stopX] = oldStopFlag;
134
- return path;
145
+ gmap->data[startY][startX] = oldStartFlag;
146
+ gmap->data[stopY][stopX] = oldStopFlag;
147
+ return &gpath;
135
148
  }
136
149
 
137
150
  JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
138
151
  {
139
152
  JPS::Position dstPos;
140
- if(!map){
153
+ if(!gmap){
141
154
  return dstPos;
142
155
  }
143
156
 
144
- int oldStartFlag = map->data[startY][startX];
145
- int oldStopFlag = map->data[endY][endX];
146
- map->data[startY][startX] = 0;
147
- map->data[endY][endX] = 0;
157
+ int oldStartFlag = gmap->data[startY][startX];
158
+ int oldStopFlag = gmap->data[endY][endX];
159
+ gmap->data[startY][startX] = 0;
160
+ gmap->data[endY][endX] = 0;
148
161
 
149
162
  JPS::PathVector waypoints;
150
163
 
@@ -152,7 +165,7 @@ JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
152
165
  waypoints.push_back(JPS::Pos(endX, endY));
153
166
 
154
167
  JPS::PathVector result;
155
- JPS::Searcher<Map> search(*map);
168
+ JPS::Searcher<Map> search(*gmap);
156
169
  search.findPath(result, waypoints[0], waypoints[1], 1);
157
170
 
158
171
  int curStep = 0;
@@ -173,9 +186,9 @@ JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
173
186
  if(curX != x && curY != y)
174
187
  {
175
188
  vector<int> v1;
176
- if(map->data[curY][x] == 0)
189
+ if(gmap->data[curY][x] == 0)
177
190
  curX = x;
178
- else if(map->data[y][curX] == 0)
191
+ else if(gmap->data[y][curX] == 0)
179
192
  curY = y;
180
193
  curStep++;
181
194
 
@@ -199,8 +212,8 @@ JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
199
212
  }
200
213
  }
201
214
 
202
- map->data[startY][startX] = oldStartFlag;
203
- map->data[endY][endX] = oldStopFlag;
215
+ gmap->data[startY][startX] = oldStartFlag;
216
+ gmap->data[endY][endX] = oldStopFlag;
204
217
 
205
218
  return dstPos;
206
219
 
@@ -208,23 +221,98 @@ JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step)
208
221
 
209
222
  void initMap(int width,int height)
210
223
  {
211
- if(map != NULL){
224
+ if(gmap != NULL)
212
225
  clearMap();
226
+
227
+ clearPath();
228
+
229
+ gmap = borrowMap(width, height);
230
+ }
231
+
232
+ void setMapPos(int posX, int posY,int flag)
233
+ {
234
+ if(!gmap){
235
+ return;
213
236
  }
214
- if(path != NULL){
215
- clearPath();
237
+ gmap->data[posY][posX] = flag;
238
+ }
239
+
240
+ int getMapPos(int posX, int posY)
241
+ {
242
+ if(!gmap){
243
+ return 0;
216
244
  }
217
245
 
218
- map = new Map();
219
- map->init(width, height);
246
+ if(posX < 0 || posX >= gmap->w) return 1;
247
+ if(posY < 0 || posY >= gmap->h) return 1;
220
248
 
221
- path = new vector<JPS::Position>();
249
+ return gmap->data[posY][posX];
222
250
  }
223
251
 
224
- void setMapPos(int posX, int posY,int flag)
252
+ /**********************map pool**********************/
253
+ string makeKey(int width, int height)
225
254
  {
226
- if(!map){
227
- return;
255
+ char buff[8] = {0};
256
+ sprintf(buff, "%d_%d", width, height);
257
+ std::string ret = buff;
258
+ return ret;
259
+ }
260
+
261
+ Map * borrowMap(int width, int height)
262
+ {
263
+ assert(width > 0);
264
+ assert(height > 0);
265
+
266
+ std::string key = makeKey(width, height);
267
+ if(hsMaps == NULL)
268
+ hsMaps = new HASH_MAPS();
269
+
270
+ VEC_MAPS *pvecMaps = NULL;
271
+ HASH_MAPS::iterator it = hsMaps->find(key);
272
+ if(it != hsMaps->end()){
273
+ pvecMaps = it->second;
274
+ }else{
275
+ pvecMaps = new VEC_MAPS();
276
+ (*hsMaps)[key] = pvecMaps;
228
277
  }
229
- map->data[posY][posX] = flag;
278
+
279
+ Map *ret = NULL;
280
+ if(pvecMaps->size() > 0){
281
+ ret = pvecMaps->back();
282
+ pvecMaps->pop_back();
283
+ }else{
284
+ ret = new Map();
285
+ ret->init(width, height);
286
+ pvecMaps->push_back(ret);
287
+ }
288
+
289
+ return ret;
290
+ }
291
+
292
+ void recycleMap(Map * pmap)
293
+ {
294
+ assert(pmap != NULL);
295
+ std::string key = makeKey(pmap->w, pmap->h);
296
+ HASH_MAPS::iterator it = hsMaps->find(key);
297
+ assert(it != hsMaps->end());
298
+ VEC_MAPS *pvecMaps = it->second;
299
+
300
+ pmap->dispose();
301
+ pvecMaps->push_back(pmap);
302
+ }
303
+
304
+ int getPooldMapKindSize()
305
+ {
306
+ return hsMaps->size();
230
307
  }
308
+
309
+ int getPooldMapSize(int width, int height)
310
+ {
311
+ std::string key = makeKey(width, height);
312
+ HASH_MAPS::iterator it = hsMaps->find(key);
313
+ if(it == hsMaps->end()) return 0;
314
+ return it->second->size();
315
+ }
316
+
317
+
318
+
data/ext/jps/map_jps.h CHANGED
@@ -2,11 +2,14 @@
2
2
  #define MAP_JPS_H_
3
3
  #include "jps.hh"
4
4
  #include <vector>
5
+ #include <string>
6
+ #include <map>
5
7
  #include <iostream>
6
8
  using namespace std;
7
9
  struct Map{
8
10
  void init(int width, int height);
9
11
  void clear();
12
+ void dispose();
10
13
  unsigned operator()(unsigned x, unsigned y) const;
11
14
  int w,h;
12
15
  int **data;
@@ -19,4 +22,10 @@ using namespace std;
19
22
  JPS::Position findDstPos(int startX,int startY, int endX, int endY,int step);
20
23
  void initMap(int width,int height);
21
24
  void setMapPos(int posX, int posY,int flag);
25
+ int getMapPos(int posX, int posY);
26
+
27
+ Map * borrowMap(int width, int height);
28
+ void recycleMap(Map * map);
29
+ int getPooldMapKindSize();
30
+ int getPooldMapSize(int width, int height);
22
31
  #endif
data/ext/jps/rjps.cpp CHANGED
@@ -4,16 +4,14 @@
4
4
  #include "jps.hh"
5
5
  #include <vector>
6
6
 
7
- extern vector<JPS::Position> *path;
8
- extern Map *map;
7
+ extern vector<JPS::Position> gpath;
8
+ extern Map *gmap;
9
9
 
10
10
 
11
11
  VALUE find_path(VALUE self, VALUE start_x, VALUE start_y, VALUE stop_x, VALUE stop_y)
12
12
  {
13
- if(map == NULL)
14
- return rb_str_new2("Map is null, plz init map first!");
15
- if(path == NULL)
16
- return rb_str_new2("path is null, plz init map first!");
13
+ if(gmap == NULL)
14
+ return rb_str_new2("Map is null, plz init gmap first!");
17
15
 
18
16
  int startX = NUM2INT(start_x);
19
17
  int startY = NUM2INT(start_y);
@@ -28,7 +26,7 @@ VALUE find_path(VALUE self, VALUE start_x, VALUE start_y, VALUE stop_x, VALUE st
28
26
  for(i=0; i< p->size(); ++i)
29
27
  {
30
28
  VALUE arr2 = rb_ary_new();
31
- JPS::Position ret = (*path)[i];
29
+ JPS::Position ret = gpath[i];
32
30
  rb_ary_push(arr2, INT2NUM(ret.x));
33
31
  rb_ary_push(arr2, INT2NUM(ret.y));
34
32
  rb_ary_push(arr1, arr2);
@@ -38,9 +36,9 @@ VALUE find_path(VALUE self, VALUE start_x, VALUE start_y, VALUE stop_x, VALUE st
38
36
  // return INT2FIX(p);
39
37
  }
40
38
 
41
- VALUE get_path_length(VALUE self)
39
+ VALUE get_path_length(VALUE self, VALUE path)
42
40
  {
43
- return INT2NUM(path->size());
41
+ return INT2NUM(RARRAY_LEN(path));
44
42
  }
45
43
 
46
44
  VALUE clear_map(VALUE self)
@@ -52,10 +50,8 @@ VALUE clear_map(VALUE self)
52
50
 
53
51
  VALUE find_dst_pos(VALUE self, VALUE start_x, VALUE start_y, VALUE end_x, VALUE end_y, VALUE step)
54
52
  {
55
- if(map == NULL)
56
- return rb_str_new2("Map is null, plz init map first!");
57
- if(path == NULL)
58
- return rb_str_new2("path is null, plz init map first!");
53
+ if(gmap == NULL)
54
+ return rb_str_new2("Map is null, plz init gmap first!");
59
55
 
60
56
  int startX = NUM2INT(start_x);
61
57
  int startY = NUM2INT(start_y);
@@ -63,13 +59,13 @@ VALUE find_dst_pos(VALUE self, VALUE start_x, VALUE start_y, VALUE end_x, VALUE
63
59
  int endY = NUM2INT(end_y);
64
60
  int sp = NUM2INT(step);
65
61
 
66
- if(startX < 0 || startX >= map->w)
62
+ if(startX < 0 || startX >= gmap->w)
67
63
  return rb_str_new2("startX is overflow!");
68
- if(startY < 0 || startY >= map->w)
64
+ if(startY < 0 || startY >= gmap->w)
69
65
  return rb_str_new2("startY is overflow!");
70
- if(endX < 0 || endX >= map->w)
66
+ if(endX < 0 || endX >= gmap->w)
71
67
  return rb_str_new2("endX is overflow!");
72
- if(endY < 0 || endY >= map->w)
68
+ if(endY < 0 || endY >= gmap->w)
73
69
  return rb_str_new2("endY is overflow!");
74
70
 
75
71
  JPS::Position ret = findDstPos(startX, startY, endX, endY, sp);
@@ -84,7 +80,7 @@ VALUE find_dst_pos(VALUE self, VALUE start_x, VALUE start_y, VALUE end_x, VALUE
84
80
  VALUE get_path_point(VALUE self, VALUE idx)
85
81
  {
86
82
  int index = NUM2INT(idx);
87
- JPS::Position ret = (*path)[index];
83
+ JPS::Position ret = gpath[index];
88
84
  VALUE point = rb_ary_new();
89
85
  rb_ary_push(point, INT2NUM(ret.x));
90
86
  rb_ary_push(point, INT2NUM(ret.y));
@@ -93,8 +89,8 @@ VALUE get_path_point(VALUE self, VALUE idx)
93
89
 
94
90
  VALUE set_map_pos(VALUE self, VALUE posX, VALUE posY, VALUE flag)
95
91
  {
96
- if(map == NULL)
97
- return rb_str_new2("Map is null, plz init map first!");
92
+ if(gmap == NULL)
93
+ return rb_str_new2("Map is null, plz init gmap first!");
98
94
  int x = NUM2INT(posX);
99
95
  int y = NUM2INT(posY);
100
96
  int fg = NUM2INT(flag);
@@ -103,6 +99,18 @@ VALUE set_map_pos(VALUE self, VALUE posX, VALUE posY, VALUE flag)
103
99
  return INT2NUM(0);
104
100
  }
105
101
 
102
+ VALUE get_map_pos(VALUE self, VALUE posX, VALUE posY)
103
+ {
104
+ if(gmap == NULL)
105
+ return rb_str_new2("Map is null, plz init gmap first!");
106
+
107
+ int x = NUM2INT(posX);
108
+ int y = NUM2INT(posY);
109
+ int fg = getMapPos(x, y);
110
+
111
+ return INT2NUM(fg);
112
+ }
113
+
106
114
  VALUE init_map(VALUE self, VALUE width, VALUE height)
107
115
  {
108
116
  int w = NUM2INT(width);
@@ -111,16 +119,32 @@ VALUE init_map(VALUE self, VALUE width, VALUE height)
111
119
  return INT2NUM(0);
112
120
  }
113
121
 
122
+ VALUE get_pooled_map_kind_size(VALUE self)
123
+ {
124
+ int si = getPooldMapKindSize();
125
+ return INT2NUM(si);
126
+ }
127
+
128
+ VALUE get_pooled_map_size(VALUE self, VALUE width, VALUE height)
129
+ {
130
+ int w = NUM2INT(width);
131
+ int h = NUM2INT(height);
132
+ int si = getPooldMapSize(w, h);
133
+ return INT2NUM(si);
134
+ }
114
135
 
115
136
  extern "C" void Init_rjps()
116
137
  {
117
138
  VALUE jps = rb_define_module("Jps");
118
139
  rb_define_module_function(jps, "find_path", RUBY_METHOD_FUNC(find_path), 4);
119
140
  rb_define_module_function(jps, "init_map", RUBY_METHOD_FUNC(init_map), 2);
120
- rb_define_module_function(jps, "get_path_length", RUBY_METHOD_FUNC(get_path_length), 0);
141
+ rb_define_module_function(jps, "get_path_length", RUBY_METHOD_FUNC(get_path_length), 1);
121
142
  rb_define_module_function(jps, "get_path_point", RUBY_METHOD_FUNC(get_path_point), 1);
122
143
  rb_define_module_function(jps, "set_map_pos", RUBY_METHOD_FUNC(set_map_pos), 3);
144
+ rb_define_module_function(jps, "get_map_pos", RUBY_METHOD_FUNC(get_map_pos), 2);
123
145
  rb_define_module_function(jps, "clear_map", RUBY_METHOD_FUNC(clear_map), 0);
124
146
  rb_define_module_function(jps, "find_dst_pos", RUBY_METHOD_FUNC(find_dst_pos), 5);
147
+ rb_define_module_function(jps, "get_pooled_map_kind_size", RUBY_METHOD_FUNC(get_pooled_map_kind_size), 0);
148
+ rb_define_module_function(jps, "get_pooled_map_size", RUBY_METHOD_FUNC(get_pooled_map_size), 2);
125
149
  }
126
150
 
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jps
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ljf
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-18 00:00:00.000000000 Z
11
+ date: 2022-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: jps automated test application
14
- email: xxxxxxx@xxxnets.com
14
+ email: duewnjie@gmail.com
15
15
  executables: []
16
16
  extensions:
17
17
  - ext/jps/extconf.rb
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  version: '0'
43
43
  requirements: []
44
44
  rubyforge_project:
45
- rubygems_version: 2.6.14
45
+ rubygems_version: 2.5.2.3
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: jps