本站源代码
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

610 Zeilen
9.1KB

  1. package roaring
  2. func equal(a, b []uint16) bool {
  3. if len(a) != len(b) {
  4. return false
  5. }
  6. for i := range a {
  7. if a[i] != b[i] {
  8. return false
  9. }
  10. }
  11. return true
  12. }
  13. func difference(set1 []uint16, set2 []uint16, buffer []uint16) int {
  14. if 0 == len(set2) {
  15. for k := 0; k < len(set1); k++ {
  16. buffer[k] = set1[k]
  17. }
  18. return len(set1)
  19. }
  20. if 0 == len(set1) {
  21. return 0
  22. }
  23. pos := 0
  24. k1 := 0
  25. k2 := 0
  26. buffer = buffer[:cap(buffer)]
  27. s1 := set1[k1]
  28. s2 := set2[k2]
  29. for {
  30. if s1 < s2 {
  31. buffer[pos] = s1
  32. pos++
  33. k1++
  34. if k1 >= len(set1) {
  35. break
  36. }
  37. s1 = set1[k1]
  38. } else if s1 == s2 {
  39. k1++
  40. k2++
  41. if k1 >= len(set1) {
  42. break
  43. }
  44. s1 = set1[k1]
  45. if k2 >= len(set2) {
  46. for ; k1 < len(set1); k1++ {
  47. buffer[pos] = set1[k1]
  48. pos++
  49. }
  50. break
  51. }
  52. s2 = set2[k2]
  53. } else { // if (val1>val2)
  54. k2++
  55. if k2 >= len(set2) {
  56. for ; k1 < len(set1); k1++ {
  57. buffer[pos] = set1[k1]
  58. pos++
  59. }
  60. break
  61. }
  62. s2 = set2[k2]
  63. }
  64. }
  65. return pos
  66. }
  67. func exclusiveUnion2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
  68. if 0 == len(set2) {
  69. buffer = buffer[:len(set1)]
  70. copy(buffer, set1[:])
  71. return len(set1)
  72. }
  73. if 0 == len(set1) {
  74. buffer = buffer[:len(set2)]
  75. copy(buffer, set2[:])
  76. return len(set2)
  77. }
  78. pos := 0
  79. k1 := 0
  80. k2 := 0
  81. s1 := set1[k1]
  82. s2 := set2[k2]
  83. buffer = buffer[:cap(buffer)]
  84. for {
  85. if s1 < s2 {
  86. buffer[pos] = s1
  87. pos++
  88. k1++
  89. if k1 >= len(set1) {
  90. for ; k2 < len(set2); k2++ {
  91. buffer[pos] = set2[k2]
  92. pos++
  93. }
  94. break
  95. }
  96. s1 = set1[k1]
  97. } else if s1 == s2 {
  98. k1++
  99. k2++
  100. if k1 >= len(set1) {
  101. for ; k2 < len(set2); k2++ {
  102. buffer[pos] = set2[k2]
  103. pos++
  104. }
  105. break
  106. }
  107. if k2 >= len(set2) {
  108. for ; k1 < len(set1); k1++ {
  109. buffer[pos] = set1[k1]
  110. pos++
  111. }
  112. break
  113. }
  114. s1 = set1[k1]
  115. s2 = set2[k2]
  116. } else { // if (val1>val2)
  117. buffer[pos] = s2
  118. pos++
  119. k2++
  120. if k2 >= len(set2) {
  121. for ; k1 < len(set1); k1++ {
  122. buffer[pos] = set1[k1]
  123. pos++
  124. }
  125. break
  126. }
  127. s2 = set2[k2]
  128. }
  129. }
  130. return pos
  131. }
  132. func union2by2(set1 []uint16, set2 []uint16, buffer []uint16) int {
  133. pos := 0
  134. k1 := 0
  135. k2 := 0
  136. if 0 == len(set2) {
  137. buffer = buffer[:len(set1)]
  138. copy(buffer, set1[:])
  139. return len(set1)
  140. }
  141. if 0 == len(set1) {
  142. buffer = buffer[:len(set2)]
  143. copy(buffer, set2[:])
  144. return len(set2)
  145. }
  146. s1 := set1[k1]
  147. s2 := set2[k2]
  148. buffer = buffer[:cap(buffer)]
  149. for {
  150. if s1 < s2 {
  151. buffer[pos] = s1
  152. pos++
  153. k1++
  154. if k1 >= len(set1) {
  155. copy(buffer[pos:], set2[k2:])
  156. pos += len(set2) - k2
  157. break
  158. }
  159. s1 = set1[k1]
  160. } else if s1 == s2 {
  161. buffer[pos] = s1
  162. pos++
  163. k1++
  164. k2++
  165. if k1 >= len(set1) {
  166. copy(buffer[pos:], set2[k2:])
  167. pos += len(set2) - k2
  168. break
  169. }
  170. if k2 >= len(set2) {
  171. copy(buffer[pos:], set1[k1:])
  172. pos += len(set1) - k1
  173. break
  174. }
  175. s1 = set1[k1]
  176. s2 = set2[k2]
  177. } else { // if (set1[k1]>set2[k2])
  178. buffer[pos] = s2
  179. pos++
  180. k2++
  181. if k2 >= len(set2) {
  182. copy(buffer[pos:], set1[k1:])
  183. pos += len(set1) - k1
  184. break
  185. }
  186. s2 = set2[k2]
  187. }
  188. }
  189. return pos
  190. }
  191. func union2by2Cardinality(set1 []uint16, set2 []uint16) int {
  192. pos := 0
  193. k1 := 0
  194. k2 := 0
  195. if 0 == len(set2) {
  196. return len(set1)
  197. }
  198. if 0 == len(set1) {
  199. return len(set2)
  200. }
  201. s1 := set1[k1]
  202. s2 := set2[k2]
  203. for {
  204. if s1 < s2 {
  205. pos++
  206. k1++
  207. if k1 >= len(set1) {
  208. pos += len(set2) - k2
  209. break
  210. }
  211. s1 = set1[k1]
  212. } else if s1 == s2 {
  213. pos++
  214. k1++
  215. k2++
  216. if k1 >= len(set1) {
  217. pos += len(set2) - k2
  218. break
  219. }
  220. if k2 >= len(set2) {
  221. pos += len(set1) - k1
  222. break
  223. }
  224. s1 = set1[k1]
  225. s2 = set2[k2]
  226. } else { // if (set1[k1]>set2[k2])
  227. pos++
  228. k2++
  229. if k2 >= len(set2) {
  230. pos += len(set1) - k1
  231. break
  232. }
  233. s2 = set2[k2]
  234. }
  235. }
  236. return pos
  237. }
  238. func intersection2by2(
  239. set1 []uint16,
  240. set2 []uint16,
  241. buffer []uint16) int {
  242. if len(set1)*64 < len(set2) {
  243. return onesidedgallopingintersect2by2(set1, set2, buffer)
  244. } else if len(set2)*64 < len(set1) {
  245. return onesidedgallopingintersect2by2(set2, set1, buffer)
  246. } else {
  247. return localintersect2by2(set1, set2, buffer)
  248. }
  249. }
  250. func intersection2by2Cardinality(
  251. set1 []uint16,
  252. set2 []uint16) int {
  253. if len(set1)*64 < len(set2) {
  254. return onesidedgallopingintersect2by2Cardinality(set1, set2)
  255. } else if len(set2)*64 < len(set1) {
  256. return onesidedgallopingintersect2by2Cardinality(set2, set1)
  257. } else {
  258. return localintersect2by2Cardinality(set1, set2)
  259. }
  260. }
  261. func intersects2by2(
  262. set1 []uint16,
  263. set2 []uint16) bool {
  264. // could be optimized if one set is much larger than the other one
  265. if (0 == len(set1)) || (0 == len(set2)) {
  266. return false
  267. }
  268. k1 := 0
  269. k2 := 0
  270. s1 := set1[k1]
  271. s2 := set2[k2]
  272. mainwhile:
  273. for {
  274. if s2 < s1 {
  275. for {
  276. k2++
  277. if k2 == len(set2) {
  278. break mainwhile
  279. }
  280. s2 = set2[k2]
  281. if s2 >= s1 {
  282. break
  283. }
  284. }
  285. }
  286. if s1 < s2 {
  287. for {
  288. k1++
  289. if k1 == len(set1) {
  290. break mainwhile
  291. }
  292. s1 = set1[k1]
  293. if s1 >= s2 {
  294. break
  295. }
  296. }
  297. } else {
  298. // (set2[k2] == set1[k1])
  299. return true
  300. }
  301. }
  302. return false
  303. }
  304. func localintersect2by2(
  305. set1 []uint16,
  306. set2 []uint16,
  307. buffer []uint16) int {
  308. if (0 == len(set1)) || (0 == len(set2)) {
  309. return 0
  310. }
  311. k1 := 0
  312. k2 := 0
  313. pos := 0
  314. buffer = buffer[:cap(buffer)]
  315. s1 := set1[k1]
  316. s2 := set2[k2]
  317. mainwhile:
  318. for {
  319. if s2 < s1 {
  320. for {
  321. k2++
  322. if k2 == len(set2) {
  323. break mainwhile
  324. }
  325. s2 = set2[k2]
  326. if s2 >= s1 {
  327. break
  328. }
  329. }
  330. }
  331. if s1 < s2 {
  332. for {
  333. k1++
  334. if k1 == len(set1) {
  335. break mainwhile
  336. }
  337. s1 = set1[k1]
  338. if s1 >= s2 {
  339. break
  340. }
  341. }
  342. } else {
  343. // (set2[k2] == set1[k1])
  344. buffer[pos] = s1
  345. pos++
  346. k1++
  347. if k1 == len(set1) {
  348. break
  349. }
  350. s1 = set1[k1]
  351. k2++
  352. if k2 == len(set2) {
  353. break
  354. }
  355. s2 = set2[k2]
  356. }
  357. }
  358. return pos
  359. }
  360. func localintersect2by2Cardinality(
  361. set1 []uint16,
  362. set2 []uint16) int {
  363. if (0 == len(set1)) || (0 == len(set2)) {
  364. return 0
  365. }
  366. k1 := 0
  367. k2 := 0
  368. pos := 0
  369. s1 := set1[k1]
  370. s2 := set2[k2]
  371. mainwhile:
  372. for {
  373. if s2 < s1 {
  374. for {
  375. k2++
  376. if k2 == len(set2) {
  377. break mainwhile
  378. }
  379. s2 = set2[k2]
  380. if s2 >= s1 {
  381. break
  382. }
  383. }
  384. }
  385. if s1 < s2 {
  386. for {
  387. k1++
  388. if k1 == len(set1) {
  389. break mainwhile
  390. }
  391. s1 = set1[k1]
  392. if s1 >= s2 {
  393. break
  394. }
  395. }
  396. } else {
  397. // (set2[k2] == set1[k1])
  398. pos++
  399. k1++
  400. if k1 == len(set1) {
  401. break
  402. }
  403. s1 = set1[k1]
  404. k2++
  405. if k2 == len(set2) {
  406. break
  407. }
  408. s2 = set2[k2]
  409. }
  410. }
  411. return pos
  412. }
  413. func advanceUntil(
  414. array []uint16,
  415. pos int,
  416. length int,
  417. min uint16) int {
  418. lower := pos + 1
  419. if lower >= length || array[lower] >= min {
  420. return lower
  421. }
  422. spansize := 1
  423. for lower+spansize < length && array[lower+spansize] < min {
  424. spansize *= 2
  425. }
  426. var upper int
  427. if lower+spansize < length {
  428. upper = lower + spansize
  429. } else {
  430. upper = length - 1
  431. }
  432. if array[upper] == min {
  433. return upper
  434. }
  435. if array[upper] < min {
  436. // means
  437. // array
  438. // has no
  439. // item
  440. // >= min
  441. // pos = array.length;
  442. return length
  443. }
  444. // we know that the next-smallest span was too small
  445. lower += (spansize >> 1)
  446. mid := 0
  447. for lower+1 != upper {
  448. mid = (lower + upper) >> 1
  449. if array[mid] == min {
  450. return mid
  451. } else if array[mid] < min {
  452. lower = mid
  453. } else {
  454. upper = mid
  455. }
  456. }
  457. return upper
  458. }
  459. func onesidedgallopingintersect2by2(
  460. smallset []uint16,
  461. largeset []uint16,
  462. buffer []uint16) int {
  463. if 0 == len(smallset) {
  464. return 0
  465. }
  466. buffer = buffer[:cap(buffer)]
  467. k1 := 0
  468. k2 := 0
  469. pos := 0
  470. s1 := largeset[k1]
  471. s2 := smallset[k2]
  472. mainwhile:
  473. for {
  474. if s1 < s2 {
  475. k1 = advanceUntil(largeset, k1, len(largeset), s2)
  476. if k1 == len(largeset) {
  477. break mainwhile
  478. }
  479. s1 = largeset[k1]
  480. }
  481. if s2 < s1 {
  482. k2++
  483. if k2 == len(smallset) {
  484. break mainwhile
  485. }
  486. s2 = smallset[k2]
  487. } else {
  488. buffer[pos] = s2
  489. pos++
  490. k2++
  491. if k2 == len(smallset) {
  492. break
  493. }
  494. s2 = smallset[k2]
  495. k1 = advanceUntil(largeset, k1, len(largeset), s2)
  496. if k1 == len(largeset) {
  497. break mainwhile
  498. }
  499. s1 = largeset[k1]
  500. }
  501. }
  502. return pos
  503. }
  504. func onesidedgallopingintersect2by2Cardinality(
  505. smallset []uint16,
  506. largeset []uint16) int {
  507. if 0 == len(smallset) {
  508. return 0
  509. }
  510. k1 := 0
  511. k2 := 0
  512. pos := 0
  513. s1 := largeset[k1]
  514. s2 := smallset[k2]
  515. mainwhile:
  516. for {
  517. if s1 < s2 {
  518. k1 = advanceUntil(largeset, k1, len(largeset), s2)
  519. if k1 == len(largeset) {
  520. break mainwhile
  521. }
  522. s1 = largeset[k1]
  523. }
  524. if s2 < s1 {
  525. k2++
  526. if k2 == len(smallset) {
  527. break mainwhile
  528. }
  529. s2 = smallset[k2]
  530. } else {
  531. pos++
  532. k2++
  533. if k2 == len(smallset) {
  534. break
  535. }
  536. s2 = smallset[k2]
  537. k1 = advanceUntil(largeset, k1, len(largeset), s2)
  538. if k1 == len(largeset) {
  539. break mainwhile
  540. }
  541. s1 = largeset[k1]
  542. }
  543. }
  544. return pos
  545. }
  546. func binarySearch(array []uint16, ikey uint16) int {
  547. low := 0
  548. high := len(array) - 1
  549. for low+16 <= high {
  550. middleIndex := int(uint32(low+high) >> 1)
  551. middleValue := array[middleIndex]
  552. if middleValue < ikey {
  553. low = middleIndex + 1
  554. } else if middleValue > ikey {
  555. high = middleIndex - 1
  556. } else {
  557. return middleIndex
  558. }
  559. }
  560. for ; low <= high; low++ {
  561. val := array[low]
  562. if val >= ikey {
  563. if val == ikey {
  564. return low
  565. }
  566. break
  567. }
  568. }
  569. return -(low + 1)
  570. }
上海开阖软件有限公司 沪ICP备12045867号-1