PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/casa/Arrays/test/tArrayIteratorSTL.cc

http://casacore.googlecode.com/
C++ | 503 lines | 465 code | 8 blank | 30 comment | 91 complexity | dcaf365db617d78ed3ff92f1e3384498 MD5 | raw file
Possible License(s): GPL-2.0
  1. //# tArrayIteratorSTL.cc: Test program for the Array Iterator member class
  2. //# Copyright (C) 2002,2003
  3. //# Associated Universities, Inc. Washington DC, USA.
  4. //#
  5. //# This program is free software; you can redistribute it and/or modify it
  6. //# under the terms of the GNU General Public License as published by the Free
  7. //# Software Foundation; either version 2 of the License, or (at your option)
  8. //# any later version.
  9. //#
  10. //# This program is distributed in the hope that it will be useful, but WITHOUT
  11. //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. //# more details.
  14. //#
  15. //# You should have received a copy of the GNU General Public License along
  16. //# with this program; if not, write to the Free Software Foundation, Inc.,
  17. //# 675 Massachusetts Ave, Cambridge, MA 02139, USA.
  18. //#
  19. //# Correspondence concerning AIPS++ should be addressed as follows:
  20. //# Internet email: aips2-request@nrao.edu.
  21. //# Postal address: AIPS++ Project Office
  22. //# National Radio Astronomy Observatory
  23. //# 520 Edgemont Road
  24. //# Charlottesville, VA 22903-2475 USA
  25. //#
  26. //# $Id: tArrayIteratorSTL.cc 20668 2009-07-10 01:14:57Z Malte.Marquarding $
  27. #include <casa/Arrays/Array.h>
  28. #include <casa/Arrays/ArrayMath.h>
  29. #include <casa/OS/Timer.h>
  30. #include <casa/BasicSL/String.h>
  31. #include <casa/Utilities/Assert.h>
  32. #include <casa/Exceptions/Error.h>
  33. #include <casa/iostream.h>
  34. #include <vector>
  35. #include <iterator>
  36. using namespace casa;
  37. void testSub (Array<Int>& arr1, const IPosition& blc,
  38. const IPosition& trc, const IPosition& inc)
  39. {
  40. Array<Int> arr = arr1(blc,trc,inc);
  41. Array<Int> arrs;
  42. arrs = arr;
  43. std::vector<Int> vec(arr.begin(), arr.end());
  44. Array<Int>::const_iterator iters = arrs.begin();
  45. uInt i=0;
  46. Array<Int>::const_iterator enditer=arr.end();
  47. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  48. if (*iter != *iters) {
  49. cout << "error in iter: " << *iter << ' ' << *iters << endl;
  50. }
  51. if (vec[i] != *iters) {
  52. cout << "error in vec: " << vec[i] << ' ' << *iters << endl;
  53. }
  54. if (arrs.data()[i] != *iters) {
  55. cout << "error in data(): " << arrs.data()[i] << ' ' << *iters << endl;
  56. }
  57. iters++;
  58. i++;
  59. }
  60. AlwaysAssert (i == arr.size(), AipsError);
  61. AlwaysAssert (std::distance(arr.begin(), arr.end()) == Int(arr.size()),
  62. AipsError);
  63. }
  64. void testIt()
  65. {
  66. Array<Int> arr(IPosition(3,4,5,6));
  67. indgen(arr);
  68. {
  69. Int i=0;
  70. Array<Int>::const_iterator enditer=arr.end();
  71. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  72. if (*iter != i) {
  73. cout << "error: " << *iter << ' ' << i << endl;
  74. }
  75. ++i;
  76. }
  77. if (uInt(i) != arr.nelements()) {
  78. cout << "error: i!=" << arr.nelements() << endl;
  79. }
  80. }
  81. testSub (arr, IPosition(3,0,0,0), IPosition(3,3,4,5), IPosition(3,1,1,1));
  82. testSub (arr, IPosition(3,0,0,0), IPosition(3,3,4,5), IPosition(3,2,1,1));
  83. testSub (arr, IPosition(3,0,0,0), IPosition(3,3,4,5), IPosition(3,1,1,2));
  84. testSub (arr, IPosition(3,3,0,0), IPosition(3,3,4,5), IPosition(3,1,1,1));
  85. testSub (arr, IPosition(3,3,4,1), IPosition(3,3,4,4), IPosition(3,1,1,1));
  86. testSub (arr, IPosition(3,1,2,1), IPosition(3,3,3,4), IPosition(3,2,1,1));
  87. // Test an empty array.
  88. {
  89. Array<Int> earr;
  90. for (Array<Int>::iterator itera1=earr.begin();
  91. itera1!=earr.end(); itera1++) {
  92. cout << "error (itera1 should be empty): " << itera1 << ' ' << endl;
  93. }
  94. for (Array<Int>::iterator itera2=earr.begin();
  95. itera2!=earr.end(); itera2++) {
  96. cout << "error (itera2 should be empty): " << itera2 << ' ' << endl;
  97. }
  98. for (Array<Int>::contiter itera3=earr.cbegin();
  99. itera3!=earr.cend(); itera3++) {
  100. cout << "error (itera3 should be empty): " << itera3 << ' ' << endl;
  101. }
  102. for (Array<Int>::contiter itera4=earr.cbegin();
  103. itera4!=earr.cend(); itera4++) {
  104. cout << "error (itera4 should be empty): " << itera4 << ' ' << endl;
  105. }
  106. }
  107. {
  108. IPosition eshp;
  109. Array<Int> earr(eshp);
  110. for (Array<Int>::iterator iterb1=earr.begin();
  111. iterb1!=earr.end(); iterb1++) {
  112. cout << "error (iterb1 should be empty): " << iterb1 << ' ' << endl;
  113. }
  114. for (Array<Int>::iterator iterb2=earr.begin();
  115. iterb2!=earr.end(); iterb2++) {
  116. cout << "error (iterb2 should be empty): " << iterb2 << ' ' << endl;
  117. }
  118. for (Array<Int>::contiter iterb3=earr.cbegin();
  119. iterb3!=earr.cend(); iterb3++) {
  120. cout << "error (iterb3 should be empty): " << iterb3 << ' ' << endl;
  121. }
  122. for (Array<Int>::contiter iterb4=earr.cbegin();
  123. iterb4!=earr.cend(); iterb4++) {
  124. cout << "error (iterb4 should be empty): " << iterb4 << ' ' << endl;
  125. }
  126. }
  127. }
  128. int main()
  129. {
  130. testIt();
  131. const Int nelem = 1000000;
  132. const Int nstep = 100;
  133. //const Int nstep = 1;
  134. {
  135. Array<Int> arr(IPosition(1,nelem));
  136. indgen(arr);
  137. Timer tim;
  138. for (Int j=0; j<nstep; j++) {
  139. Int inx=0;
  140. for (Array<Int>::const_iterator iter=arr.begin(); iter!=arr.end(); ++iter) {
  141. if (*iter != inx) {
  142. cout << "err" << endl;
  143. }
  144. inx++;
  145. }
  146. }
  147. tim.show("read full, end() ");
  148. }
  149. {
  150. Array<Int> arr(IPosition(1,nelem));
  151. indgen(arr);
  152. Timer tim;
  153. for (Int j=0; j<nstep; j++) {
  154. Int inx=0;
  155. Array<Int>::const_iterator enditer=arr.end();
  156. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  157. if (*iter != inx) {
  158. cout << "err" << endl;
  159. }
  160. inx++;
  161. }
  162. }
  163. tim.show("read full; enditer ");
  164. }
  165. {
  166. Array<Int> arr(IPosition(1,nelem));
  167. indgen(arr);
  168. Timer tim;
  169. for (Int j=0; j<nstep; j++) {
  170. Int inx=0;
  171. for (Array<Int>::const_contiter iter=arr.cbegin(); iter!=arr.cend(); ++iter) {
  172. if (*iter != inx) {
  173. cout << "err" << endl;
  174. }
  175. inx++;
  176. }
  177. }
  178. tim.show("read full, contiter()");
  179. }
  180. {
  181. Array<Int> bl(IPosition(1,nelem));
  182. indgen(bl);
  183. Timer tim;
  184. for (Int j=0; j<nstep; j++) {
  185. Int inx=0;
  186. Bool deleteIt;
  187. const Int* str = bl.getStorage(deleteIt);
  188. for (Int i=0; i<nelem; ++i) {
  189. if (str[i] != inx) {
  190. cout << "err" << endl;
  191. }
  192. inx++;
  193. }
  194. bl.freeStorage(str, deleteIt);
  195. }
  196. tim.show("read full, getStorage");
  197. }
  198. {
  199. Array<Int> bl(IPosition(1,nelem));
  200. indgen(bl);
  201. Timer tim;
  202. for (Int j=0; j<nstep; j++) {
  203. Int inx=0;
  204. Int* ptr = bl.data();
  205. for (Int i=0; i<nelem; ++i) {
  206. if (ptr[i] != inx) {
  207. cout << "err" << endl;
  208. }
  209. inx++;
  210. }
  211. }
  212. tim.show("read full, data()[i] ");
  213. }
  214. {
  215. Array<Int> bl(IPosition(1,nelem));
  216. indgen(bl);
  217. Timer tim;
  218. for (Int j=0; j<nstep; j++) {
  219. Int inx=0;
  220. Bool deleteIt;
  221. const Int* str = bl.getStorage(deleteIt);
  222. const Bool contig = bl.contiguousStorage();
  223. for (Int i=0; i<nelem; ++i) {
  224. if (str[i] != inx) {
  225. cout << "err" << endl;
  226. }
  227. inx++;
  228. // This test is always false. It is there to mimic the
  229. // contig test in ArraySTLIterator.
  230. if (!contig) {
  231. inx++;
  232. }
  233. }
  234. bl.freeStorage(str, deleteIt);
  235. }
  236. tim.show("read full, getSt+test");
  237. }
  238. {
  239. Array<Int> bl1(IPosition(2,1000,1000));
  240. Array<Int> bl = bl1(IPosition(2,50,50), bl1.shape()-50);
  241. indgen(bl);
  242. Timer tim;
  243. for (Int j=0; j<nstep; j++) {
  244. Int inx=0;
  245. Bool deleteIt;
  246. uInt n = bl.nelements();
  247. const Int* str = bl.getStorage(deleteIt);
  248. for (uInt i=0; i<n; ++i) {
  249. if (str[i] != inx) {
  250. cout << "err" << endl;
  251. }
  252. inx++;
  253. }
  254. bl.freeStorage(str, deleteIt);
  255. }
  256. tim.show("read part, getStorage");
  257. }
  258. {
  259. Array<Int> bl1(IPosition(2,1000,1000));
  260. Array<Int> arr = bl1(IPosition(2,50,50), bl1.shape()-50);
  261. indgen(arr);
  262. Timer tim;
  263. for (Int j=0; j<nstep; j++) {
  264. Int inx=0;
  265. Array<Int>::const_iterator enditer=arr.end();
  266. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  267. if (*iter != inx) {
  268. cout << "err" << endl;
  269. }
  270. inx++;
  271. }
  272. }
  273. tim.show("read part, enditer ");
  274. }
  275. {
  276. Array<Int> bl1(IPosition(2,1000,1000));
  277. Array<Int> bl = bl1(IPosition(2,50,50), bl1.shape()-50, IPosition(2,2,2));
  278. indgen(bl);
  279. Timer tim;
  280. for (Int j=0; j<nstep; j++) {
  281. Int inx=0;
  282. Bool deleteIt;
  283. uInt n = bl.nelements();
  284. const Int* str = bl.getStorage(deleteIt);
  285. for (uInt i=0; i<n; ++i) {
  286. if (str[i] != inx) {
  287. cout << "err" << endl;
  288. }
  289. inx++;
  290. }
  291. bl.freeStorage(str, deleteIt);
  292. }
  293. tim.show("read incr, getStorage");
  294. }
  295. {
  296. Array<Int> bl1(IPosition(2,1000,1000));
  297. Array<Int> arr = bl1(IPosition(2,50,50), bl1.shape()-50, IPosition(2,2,2));
  298. indgen(arr);
  299. Timer tim;
  300. for (Int j=0; j<nstep; j++) {
  301. Int inx=0;
  302. Array<Int>::const_iterator enditer=arr.end();
  303. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  304. if (*iter != inx) {
  305. cout << "err" << endl;
  306. }
  307. inx++;
  308. }
  309. }
  310. tim.show("read incr, enditer ");
  311. }
  312. {
  313. Array<Int> bl1(IPosition(2,1000,1000));
  314. Array<Int> bl = bl1(IPosition(2,50,0), IPosition(2,50,999));
  315. indgen(bl);
  316. Timer tim;
  317. for (Int j=0; j<nstep; j++) {
  318. Int inx=0;
  319. Bool deleteIt;
  320. uInt n = bl.nelements();
  321. const Int* str = bl.getStorage(deleteIt);
  322. for (uInt i=0; i<n; ++i) {
  323. if (str[i] != inx) {
  324. cout << "err" << endl;
  325. }
  326. inx++;
  327. }
  328. bl.freeStorage(str, deleteIt);
  329. }
  330. tim.show("read small getStorage");
  331. }
  332. {
  333. Array<Int> bl1(IPosition(2,1000,1000));
  334. Array<Int> arr = bl1(IPosition(2,50,0), IPosition(2,50,999));
  335. indgen(arr);
  336. Timer tim;
  337. for (Int j=0; j<nstep; j++) {
  338. Int inx=0;
  339. Array<Int>::const_iterator enditer=arr.end();
  340. for (Array<Int>::const_iterator iter=arr.begin(); iter!=enditer; ++iter) {
  341. if (*iter != inx) {
  342. cout << "err" << endl;
  343. }
  344. inx++;
  345. }
  346. }
  347. tim.show("read small enditer ");
  348. }
  349. {
  350. Array<Int> arr(IPosition(1,nelem));
  351. Timer tim;
  352. for (Int j=0; j<nstep; j++) {
  353. Int inx=0;
  354. for (Array<Int>::iterator iter=arr.begin(); iter!=arr.end(); ++iter) {
  355. *iter = inx;
  356. inx++;
  357. }
  358. }
  359. tim.show("write full; end() ");
  360. }
  361. {
  362. Array<Int> arr(IPosition(1,nelem));
  363. Timer tim;
  364. for (Int j=0; j<nstep; j++) {
  365. Int inx=0;
  366. Array<Int>::iterator enditer;
  367. enditer = arr.end();
  368. for (Array<Int>::iterator iter=arr.begin(); iter!=enditer; ++iter) {
  369. *iter = inx;
  370. inx++;
  371. }
  372. }
  373. tim.show("write full, enditer ");
  374. }
  375. {
  376. Array<Int> arr(IPosition(1,nelem));
  377. Timer tim;
  378. for (Int j=0; j<nstep; j++) {
  379. Int inx=0;
  380. for (Array<Int>::contiter iter=arr.cbegin(); iter!=arr.cend(); ++iter) {
  381. *iter = inx;
  382. inx++;
  383. }
  384. }
  385. tim.show("write full, contiter()");
  386. }
  387. {
  388. Array<Int> bl(IPosition(1,nelem));
  389. Timer tim;
  390. for (Int j=0; j<nstep; j++) {
  391. Int inx=0;
  392. Bool deleteIt;
  393. Int* str = bl.getStorage(deleteIt);
  394. for (Int i=0; i<nelem; ++i) {
  395. str[i] = inx;
  396. inx++;
  397. }
  398. bl.putStorage(str, deleteIt);
  399. }
  400. tim.show("write full, getStorage");
  401. }
  402. {
  403. Array<Int> bl1(IPosition(2,1000,1000));
  404. Array<Int> bl = bl1(IPosition(2,50,50), bl1.shape()-50);
  405. Timer tim;
  406. for (Int j=0; j<nstep; j++) {
  407. Int inx=0;
  408. Bool deleteIt;
  409. uInt n = bl.nelements();
  410. Int* str = bl.getStorage(deleteIt);
  411. for (uInt i=0; i<n; ++i) {
  412. str[i] = inx;
  413. inx++;
  414. }
  415. bl.putStorage(str, deleteIt);
  416. }
  417. tim.show("write part, getStorage");
  418. }
  419. {
  420. Array<Int> bl1(IPosition(2,1000,1000));
  421. Array<Int> arr = bl1(IPosition(2,50,50), bl1.shape()-50);
  422. Timer tim;
  423. for (Int j=0; j<nstep; j++) {
  424. Int inx=0;
  425. Array<Int>::iterator enditer=arr.end();
  426. for (Array<Int>::iterator iter=arr.begin(); iter!=enditer; ++iter) {
  427. *iter = inx;
  428. inx++;
  429. }
  430. }
  431. tim.show("write part, enditer ");
  432. }
  433. {
  434. Array<Int> bl1(IPosition(2,1000,1000));
  435. Array<Int> bl = bl1(IPosition(2,50,50), bl1.shape()-50, IPosition(2,2,2));
  436. Timer tim;
  437. for (Int j=0; j<nstep; j++) {
  438. Int inx=0;
  439. Bool deleteIt;
  440. uInt n = bl.nelements();
  441. Int* str = bl.getStorage(deleteIt);
  442. for (uInt i=0; i<n; ++i) {
  443. str[i] = inx;
  444. inx++;
  445. }
  446. bl.putStorage(str, deleteIt);
  447. }
  448. tim.show("write incr, getStorage");
  449. }
  450. {
  451. Array<Int> bl1(IPosition(2,1000,1000));
  452. Array<Int> arr = bl1(IPosition(2,50,50), bl1.shape()-50, IPosition(2,2,2));
  453. Timer tim;
  454. for (Int j=0; j<nstep; j++) {
  455. Int inx=0;
  456. Array<Int>::iterator enditer=arr.end();
  457. for (Array<Int>::iterator iter=arr.begin(); iter!=enditer; ++iter) {
  458. *iter = inx;
  459. inx++;
  460. }
  461. }
  462. tim.show("write incr, enditer ");
  463. }
  464. {
  465. Array<Int> bl1(IPosition(2,1000,1000));
  466. Array<Int> bl = bl1(IPosition(2,50,0), IPosition(2,50,999));
  467. Timer tim;
  468. for (Int j=0; j<nstep; j++) {
  469. Int inx=0;
  470. Bool deleteIt;
  471. uInt n = bl.nelements();
  472. Int* str = bl.getStorage(deleteIt);
  473. for (uInt i=0; i<n; ++i) {
  474. str[i] = inx;
  475. inx++;
  476. }
  477. bl.putStorage(str, deleteIt);
  478. }
  479. tim.show("write small getStorage");
  480. }
  481. {
  482. Array<Int> bl1(IPosition(2,1000,1000));
  483. Array<Int> arr = bl1(IPosition(2,50,0), IPosition(2,50,999));
  484. Timer tim;
  485. for (Int j=0; j<nstep; j++) {
  486. Int inx=0;
  487. Array<Int>::iterator enditer=arr.end();
  488. for (Array<Int>::iterator iter=arr.begin(); iter!=enditer; ++iter) {
  489. *iter = inx;
  490. inx++;
  491. }
  492. }
  493. tim.show("write small enditer ");
  494. }
  495. }