faiss 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/lib/faiss/version.rb +1 -1
  4. data/vendor/faiss/faiss/AutoTune.cpp +292 -291
  5. data/vendor/faiss/faiss/AutoTune.h +55 -56
  6. data/vendor/faiss/faiss/Clustering.cpp +334 -195
  7. data/vendor/faiss/faiss/Clustering.h +88 -35
  8. data/vendor/faiss/faiss/IVFlib.cpp +171 -195
  9. data/vendor/faiss/faiss/IVFlib.h +48 -51
  10. data/vendor/faiss/faiss/Index.cpp +85 -103
  11. data/vendor/faiss/faiss/Index.h +54 -48
  12. data/vendor/faiss/faiss/Index2Layer.cpp +139 -164
  13. data/vendor/faiss/faiss/Index2Layer.h +22 -22
  14. data/vendor/faiss/faiss/IndexBinary.cpp +45 -37
  15. data/vendor/faiss/faiss/IndexBinary.h +140 -132
  16. data/vendor/faiss/faiss/IndexBinaryFlat.cpp +73 -53
  17. data/vendor/faiss/faiss/IndexBinaryFlat.h +29 -24
  18. data/vendor/faiss/faiss/IndexBinaryFromFloat.cpp +46 -43
  19. data/vendor/faiss/faiss/IndexBinaryFromFloat.h +16 -15
  20. data/vendor/faiss/faiss/IndexBinaryHNSW.cpp +215 -232
  21. data/vendor/faiss/faiss/IndexBinaryHNSW.h +25 -24
  22. data/vendor/faiss/faiss/IndexBinaryHash.cpp +182 -177
  23. data/vendor/faiss/faiss/IndexBinaryHash.h +41 -34
  24. data/vendor/faiss/faiss/IndexBinaryIVF.cpp +489 -461
  25. data/vendor/faiss/faiss/IndexBinaryIVF.h +97 -68
  26. data/vendor/faiss/faiss/IndexFlat.cpp +116 -147
  27. data/vendor/faiss/faiss/IndexFlat.h +35 -46
  28. data/vendor/faiss/faiss/IndexHNSW.cpp +372 -348
  29. data/vendor/faiss/faiss/IndexHNSW.h +57 -41
  30. data/vendor/faiss/faiss/IndexIVF.cpp +474 -454
  31. data/vendor/faiss/faiss/IndexIVF.h +146 -113
  32. data/vendor/faiss/faiss/IndexIVFFlat.cpp +248 -250
  33. data/vendor/faiss/faiss/IndexIVFFlat.h +48 -51
  34. data/vendor/faiss/faiss/IndexIVFPQ.cpp +457 -516
  35. data/vendor/faiss/faiss/IndexIVFPQ.h +74 -66
  36. data/vendor/faiss/faiss/IndexIVFPQFastScan.cpp +406 -372
  37. data/vendor/faiss/faiss/IndexIVFPQFastScan.h +82 -57
  38. data/vendor/faiss/faiss/IndexIVFPQR.cpp +104 -102
  39. data/vendor/faiss/faiss/IndexIVFPQR.h +33 -28
  40. data/vendor/faiss/faiss/IndexIVFSpectralHash.cpp +125 -133
  41. data/vendor/faiss/faiss/IndexIVFSpectralHash.h +19 -21
  42. data/vendor/faiss/faiss/IndexLSH.cpp +75 -96
  43. data/vendor/faiss/faiss/IndexLSH.h +21 -26
  44. data/vendor/faiss/faiss/IndexLattice.cpp +42 -56
  45. data/vendor/faiss/faiss/IndexLattice.h +11 -16
  46. data/vendor/faiss/faiss/IndexNNDescent.cpp +231 -0
  47. data/vendor/faiss/faiss/IndexNNDescent.h +72 -0
  48. data/vendor/faiss/faiss/IndexNSG.cpp +303 -0
  49. data/vendor/faiss/faiss/IndexNSG.h +85 -0
  50. data/vendor/faiss/faiss/IndexPQ.cpp +405 -464
  51. data/vendor/faiss/faiss/IndexPQ.h +64 -67
  52. data/vendor/faiss/faiss/IndexPQFastScan.cpp +143 -170
  53. data/vendor/faiss/faiss/IndexPQFastScan.h +46 -32
  54. data/vendor/faiss/faiss/IndexPreTransform.cpp +120 -150
  55. data/vendor/faiss/faiss/IndexPreTransform.h +33 -36
  56. data/vendor/faiss/faiss/IndexRefine.cpp +115 -131
  57. data/vendor/faiss/faiss/IndexRefine.h +22 -23
  58. data/vendor/faiss/faiss/IndexReplicas.cpp +147 -153
  59. data/vendor/faiss/faiss/IndexReplicas.h +62 -56
  60. data/vendor/faiss/faiss/IndexResidual.cpp +291 -0
  61. data/vendor/faiss/faiss/IndexResidual.h +152 -0
  62. data/vendor/faiss/faiss/IndexScalarQuantizer.cpp +120 -155
  63. data/vendor/faiss/faiss/IndexScalarQuantizer.h +41 -45
  64. data/vendor/faiss/faiss/IndexShards.cpp +256 -240
  65. data/vendor/faiss/faiss/IndexShards.h +85 -73
  66. data/vendor/faiss/faiss/MatrixStats.cpp +112 -97
  67. data/vendor/faiss/faiss/MatrixStats.h +7 -10
  68. data/vendor/faiss/faiss/MetaIndexes.cpp +135 -157
  69. data/vendor/faiss/faiss/MetaIndexes.h +40 -34
  70. data/vendor/faiss/faiss/MetricType.h +7 -7
  71. data/vendor/faiss/faiss/VectorTransform.cpp +652 -474
  72. data/vendor/faiss/faiss/VectorTransform.h +61 -89
  73. data/vendor/faiss/faiss/clone_index.cpp +77 -73
  74. data/vendor/faiss/faiss/clone_index.h +4 -9
  75. data/vendor/faiss/faiss/gpu/GpuAutoTune.cpp +33 -38
  76. data/vendor/faiss/faiss/gpu/GpuAutoTune.h +11 -9
  77. data/vendor/faiss/faiss/gpu/GpuCloner.cpp +197 -170
  78. data/vendor/faiss/faiss/gpu/GpuCloner.h +53 -35
  79. data/vendor/faiss/faiss/gpu/GpuClonerOptions.cpp +12 -14
  80. data/vendor/faiss/faiss/gpu/GpuClonerOptions.h +27 -25
  81. data/vendor/faiss/faiss/gpu/GpuDistance.h +116 -112
  82. data/vendor/faiss/faiss/gpu/GpuFaissAssert.h +1 -2
  83. data/vendor/faiss/faiss/gpu/GpuIndex.h +134 -137
  84. data/vendor/faiss/faiss/gpu/GpuIndexBinaryFlat.h +76 -73
  85. data/vendor/faiss/faiss/gpu/GpuIndexFlat.h +173 -162
  86. data/vendor/faiss/faiss/gpu/GpuIndexIVF.h +67 -64
  87. data/vendor/faiss/faiss/gpu/GpuIndexIVFFlat.h +89 -86
  88. data/vendor/faiss/faiss/gpu/GpuIndexIVFPQ.h +150 -141
  89. data/vendor/faiss/faiss/gpu/GpuIndexIVFScalarQuantizer.h +101 -103
  90. data/vendor/faiss/faiss/gpu/GpuIndicesOptions.h +17 -16
  91. data/vendor/faiss/faiss/gpu/GpuResources.cpp +116 -128
  92. data/vendor/faiss/faiss/gpu/GpuResources.h +182 -186
  93. data/vendor/faiss/faiss/gpu/StandardGpuResources.cpp +433 -422
  94. data/vendor/faiss/faiss/gpu/StandardGpuResources.h +131 -130
  95. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.cpp +468 -456
  96. data/vendor/faiss/faiss/gpu/impl/InterleavedCodes.h +25 -19
  97. data/vendor/faiss/faiss/gpu/impl/RemapIndices.cpp +22 -20
  98. data/vendor/faiss/faiss/gpu/impl/RemapIndices.h +9 -8
  99. data/vendor/faiss/faiss/gpu/perf/IndexWrapper-inl.h +39 -44
  100. data/vendor/faiss/faiss/gpu/perf/IndexWrapper.h +16 -14
  101. data/vendor/faiss/faiss/gpu/perf/PerfClustering.cpp +77 -71
  102. data/vendor/faiss/faiss/gpu/perf/PerfIVFPQAdd.cpp +109 -88
  103. data/vendor/faiss/faiss/gpu/perf/WriteIndex.cpp +75 -64
  104. data/vendor/faiss/faiss/gpu/test/TestCodePacking.cpp +230 -215
  105. data/vendor/faiss/faiss/gpu/test/TestGpuIndexBinaryFlat.cpp +80 -86
  106. data/vendor/faiss/faiss/gpu/test/TestGpuIndexFlat.cpp +284 -277
  107. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFFlat.cpp +416 -416
  108. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFPQ.cpp +611 -517
  109. data/vendor/faiss/faiss/gpu/test/TestGpuIndexIVFScalarQuantizer.cpp +166 -164
  110. data/vendor/faiss/faiss/gpu/test/TestGpuMemoryException.cpp +61 -53
  111. data/vendor/faiss/faiss/gpu/test/TestUtils.cpp +274 -238
  112. data/vendor/faiss/faiss/gpu/test/TestUtils.h +73 -57
  113. data/vendor/faiss/faiss/gpu/test/demo_ivfpq_indexing_gpu.cpp +47 -50
  114. data/vendor/faiss/faiss/gpu/utils/DeviceUtils.h +79 -72
  115. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.cpp +140 -146
  116. data/vendor/faiss/faiss/gpu/utils/StackDeviceMemory.h +69 -71
  117. data/vendor/faiss/faiss/gpu/utils/StaticUtils.h +21 -16
  118. data/vendor/faiss/faiss/gpu/utils/Timer.cpp +25 -29
  119. data/vendor/faiss/faiss/gpu/utils/Timer.h +30 -29
  120. data/vendor/faiss/faiss/impl/AdditiveQuantizer.cpp +270 -0
  121. data/vendor/faiss/faiss/impl/AdditiveQuantizer.h +115 -0
  122. data/vendor/faiss/faiss/impl/AuxIndexStructures.cpp +90 -120
  123. data/vendor/faiss/faiss/impl/AuxIndexStructures.h +81 -65
  124. data/vendor/faiss/faiss/impl/FaissAssert.h +73 -58
  125. data/vendor/faiss/faiss/impl/FaissException.cpp +56 -48
  126. data/vendor/faiss/faiss/impl/FaissException.h +41 -29
  127. data/vendor/faiss/faiss/impl/HNSW.cpp +595 -611
  128. data/vendor/faiss/faiss/impl/HNSW.h +179 -200
  129. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.cpp +672 -0
  130. data/vendor/faiss/faiss/impl/LocalSearchQuantizer.h +172 -0
  131. data/vendor/faiss/faiss/impl/NNDescent.cpp +487 -0
  132. data/vendor/faiss/faiss/impl/NNDescent.h +154 -0
  133. data/vendor/faiss/faiss/impl/NSG.cpp +682 -0
  134. data/vendor/faiss/faiss/impl/NSG.h +199 -0
  135. data/vendor/faiss/faiss/impl/PolysemousTraining.cpp +484 -454
  136. data/vendor/faiss/faiss/impl/PolysemousTraining.h +52 -55
  137. data/vendor/faiss/faiss/impl/ProductQuantizer-inl.h +26 -47
  138. data/vendor/faiss/faiss/impl/ProductQuantizer.cpp +469 -459
  139. data/vendor/faiss/faiss/impl/ProductQuantizer.h +76 -87
  140. data/vendor/faiss/faiss/impl/ResidualQuantizer.cpp +448 -0
  141. data/vendor/faiss/faiss/impl/ResidualQuantizer.h +130 -0
  142. data/vendor/faiss/faiss/impl/ResultHandler.h +96 -132
  143. data/vendor/faiss/faiss/impl/ScalarQuantizer.cpp +648 -701
  144. data/vendor/faiss/faiss/impl/ScalarQuantizer.h +48 -46
  145. data/vendor/faiss/faiss/impl/ThreadedIndex-inl.h +129 -131
  146. data/vendor/faiss/faiss/impl/ThreadedIndex.h +61 -55
  147. data/vendor/faiss/faiss/impl/index_read.cpp +547 -479
  148. data/vendor/faiss/faiss/impl/index_write.cpp +497 -407
  149. data/vendor/faiss/faiss/impl/io.cpp +75 -94
  150. data/vendor/faiss/faiss/impl/io.h +31 -41
  151. data/vendor/faiss/faiss/impl/io_macros.h +40 -29
  152. data/vendor/faiss/faiss/impl/lattice_Zn.cpp +137 -186
  153. data/vendor/faiss/faiss/impl/lattice_Zn.h +40 -51
  154. data/vendor/faiss/faiss/impl/platform_macros.h +29 -8
  155. data/vendor/faiss/faiss/impl/pq4_fast_scan.cpp +77 -124
  156. data/vendor/faiss/faiss/impl/pq4_fast_scan.h +39 -48
  157. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_1.cpp +41 -52
  158. data/vendor/faiss/faiss/impl/pq4_fast_scan_search_qbs.cpp +80 -117
  159. data/vendor/faiss/faiss/impl/simd_result_handlers.h +109 -137
  160. data/vendor/faiss/faiss/index_factory.cpp +269 -218
  161. data/vendor/faiss/faiss/index_factory.h +6 -7
  162. data/vendor/faiss/faiss/index_io.h +23 -26
  163. data/vendor/faiss/faiss/invlists/BlockInvertedLists.cpp +67 -75
  164. data/vendor/faiss/faiss/invlists/BlockInvertedLists.h +22 -24
  165. data/vendor/faiss/faiss/invlists/DirectMap.cpp +96 -112
  166. data/vendor/faiss/faiss/invlists/DirectMap.h +29 -33
  167. data/vendor/faiss/faiss/invlists/InvertedLists.cpp +307 -364
  168. data/vendor/faiss/faiss/invlists/InvertedLists.h +151 -151
  169. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.cpp +29 -34
  170. data/vendor/faiss/faiss/invlists/InvertedListsIOHook.h +17 -18
  171. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.cpp +257 -293
  172. data/vendor/faiss/faiss/invlists/OnDiskInvertedLists.h +50 -45
  173. data/vendor/faiss/faiss/python/python_callbacks.cpp +23 -26
  174. data/vendor/faiss/faiss/python/python_callbacks.h +9 -16
  175. data/vendor/faiss/faiss/utils/AlignedTable.h +79 -44
  176. data/vendor/faiss/faiss/utils/Heap.cpp +40 -48
  177. data/vendor/faiss/faiss/utils/Heap.h +186 -209
  178. data/vendor/faiss/faiss/utils/WorkerThread.cpp +67 -76
  179. data/vendor/faiss/faiss/utils/WorkerThread.h +32 -33
  180. data/vendor/faiss/faiss/utils/distances.cpp +301 -310
  181. data/vendor/faiss/faiss/utils/distances.h +133 -118
  182. data/vendor/faiss/faiss/utils/distances_simd.cpp +456 -516
  183. data/vendor/faiss/faiss/utils/extra_distances-inl.h +117 -0
  184. data/vendor/faiss/faiss/utils/extra_distances.cpp +113 -232
  185. data/vendor/faiss/faiss/utils/extra_distances.h +30 -29
  186. data/vendor/faiss/faiss/utils/hamming-inl.h +260 -209
  187. data/vendor/faiss/faiss/utils/hamming.cpp +375 -469
  188. data/vendor/faiss/faiss/utils/hamming.h +62 -85
  189. data/vendor/faiss/faiss/utils/ordered_key_value.h +16 -18
  190. data/vendor/faiss/faiss/utils/partitioning.cpp +393 -318
  191. data/vendor/faiss/faiss/utils/partitioning.h +26 -21
  192. data/vendor/faiss/faiss/utils/quantize_lut.cpp +78 -66
  193. data/vendor/faiss/faiss/utils/quantize_lut.h +22 -20
  194. data/vendor/faiss/faiss/utils/random.cpp +39 -63
  195. data/vendor/faiss/faiss/utils/random.h +13 -16
  196. data/vendor/faiss/faiss/utils/simdlib.h +4 -2
  197. data/vendor/faiss/faiss/utils/simdlib_avx2.h +88 -85
  198. data/vendor/faiss/faiss/utils/simdlib_emulated.h +226 -165
  199. data/vendor/faiss/faiss/utils/simdlib_neon.h +832 -0
  200. data/vendor/faiss/faiss/utils/utils.cpp +304 -287
  201. data/vendor/faiss/faiss/utils/utils.h +53 -48
  202. metadata +20 -2
@@ -13,21 +13,20 @@
13
13
  #include <faiss/invlists/InvertedLists.h>
14
14
  #include <unordered_map>
15
15
 
16
-
17
16
  namespace faiss {
18
17
 
19
18
  // When offsets list id + offset are encoded in an uint64
20
19
  // we call this LO = list-offset
21
20
 
22
- inline uint64_t lo_build (uint64_t list_id, uint64_t offset) {
23
- return list_id << 32 | offset;
21
+ inline uint64_t lo_build(uint64_t list_id, uint64_t offset) {
22
+ return list_id << 32 | offset;
24
23
  }
25
24
 
26
- inline uint64_t lo_listno (uint64_t lo) {
25
+ inline uint64_t lo_listno(uint64_t lo) {
27
26
  return lo >> 32;
28
27
  }
29
28
 
30
- inline uint64_t lo_offset (uint64_t lo) {
29
+ inline uint64_t lo_offset(uint64_t lo) {
31
30
  return lo & 0xffffffff;
32
31
  }
33
32
 
@@ -38,36 +37,38 @@ struct DirectMap {
38
37
  typedef Index::idx_t idx_t;
39
38
 
40
39
  enum Type {
41
- NoMap = 0, // default
42
- Array = 1, // sequential ids (only for add, no add_with_ids)
43
- Hashtable = 2 // arbitrary ids
40
+ NoMap = 0, // default
41
+ Array = 1, // sequential ids (only for add, no add_with_ids)
42
+ Hashtable = 2 // arbitrary ids
44
43
  };
45
44
  Type type;
46
45
 
47
46
  /// map for direct access to the elements. Map ids to LO-encoded entries.
48
- std::vector <idx_t> array;
49
- std::unordered_map <idx_t, idx_t> hashtable;
47
+ std::vector<idx_t> array;
48
+ std::unordered_map<idx_t, idx_t> hashtable;
50
49
 
51
50
  DirectMap();
52
51
 
53
52
  /// set type and initialize
54
- void set_type (Type new_type, const InvertedLists *invlists, size_t ntotal);
53
+ void set_type(Type new_type, const InvertedLists* invlists, size_t ntotal);
55
54
 
56
55
  /// get an entry
57
- idx_t get (idx_t id) const;
56
+ idx_t get(idx_t id) const;
58
57
 
59
58
  /// for quick checks
60
- bool no () const {return type == NoMap; }
59
+ bool no() const {
60
+ return type == NoMap;
61
+ }
61
62
 
62
63
  /**
63
64
  * update the direct_map
64
65
  */
65
66
 
66
67
  /// throw if Array and ids is not NULL
67
- void check_can_add (const idx_t *ids);
68
+ void check_can_add(const idx_t* ids);
68
69
 
69
70
  /// non thread-safe version
70
- void add_single_id (idx_t id, idx_t list_no, size_t offset);
71
+ void add_single_id(idx_t id, idx_t list_no, size_t offset);
71
72
 
72
73
  /// remove all entries
73
74
  void clear();
@@ -77,44 +78,39 @@ struct DirectMap {
77
78
  */
78
79
 
79
80
  /// remove ids from the InvertedLists, possibly using the direct map
80
- size_t remove_ids(const IDSelector& sel, InvertedLists *invlists);
81
+ size_t remove_ids(const IDSelector& sel, InvertedLists* invlists);
81
82
 
82
83
  /// update entries, using the direct map
83
- void update_codes (InvertedLists *invlists,
84
- int n, const idx_t *ids,
85
- const idx_t *list_nos,
86
- const uint8_t *codes);
87
-
88
-
89
-
84
+ void update_codes(
85
+ InvertedLists* invlists,
86
+ int n,
87
+ const idx_t* ids,
88
+ const idx_t* list_nos,
89
+ const uint8_t* codes);
90
90
  };
91
91
 
92
92
  /// Thread-safe way of updating the direct_map
93
93
  struct DirectMapAdd {
94
-
95
94
  typedef Index::idx_t idx_t;
96
95
 
97
96
  using Type = DirectMap::Type;
98
97
 
99
- DirectMap &direct_map;
98
+ DirectMap& direct_map;
100
99
  DirectMap::Type type;
101
100
  size_t ntotal;
102
101
  size_t n;
103
- const idx_t *xids;
102
+ const idx_t* xids;
104
103
 
105
104
  std::vector<idx_t> all_ofs;
106
105
 
107
- DirectMapAdd (DirectMap &direct_map, size_t n, const idx_t *xids);
106
+ DirectMapAdd(DirectMap& direct_map, size_t n, const idx_t* xids);
108
107
 
109
108
  /// add vector i (with id xids[i]) at list_no and offset
110
- void add (size_t i, idx_t list_no, size_t offset);
109
+ void add(size_t i, idx_t list_no, size_t offset);
111
110
 
112
- ~DirectMapAdd ();
111
+ ~DirectMapAdd();
113
112
  };
114
113
 
115
-
116
-
117
- }
118
-
114
+ } // namespace faiss
119
115
 
120
116
  #endif
@@ -11,90 +11,80 @@
11
11
 
12
12
  #include <cstdio>
13
13
 
14
- #include <faiss/utils/utils.h>
15
14
  #include <faiss/impl/FaissAssert.h>
15
+ #include <faiss/utils/utils.h>
16
16
 
17
17
  namespace faiss {
18
18
 
19
-
20
-
21
19
  /*****************************************
22
20
  * InvertedLists implementation
23
21
  ******************************************/
24
22
 
25
- InvertedLists::InvertedLists (size_t nlist, size_t code_size):
26
- nlist (nlist), code_size (code_size)
27
- {
28
- }
23
+ InvertedLists::InvertedLists(size_t nlist, size_t code_size)
24
+ : nlist(nlist), code_size(code_size) {}
29
25
 
30
- InvertedLists::~InvertedLists ()
31
- {}
26
+ InvertedLists::~InvertedLists() {}
32
27
 
33
- InvertedLists::idx_t InvertedLists::get_single_id (
34
- size_t list_no, size_t offset) const
35
- {
36
- assert (offset < list_size (list_no));
28
+ InvertedLists::idx_t InvertedLists::get_single_id(size_t list_no, size_t offset)
29
+ const {
30
+ assert(offset < list_size(list_no));
37
31
  return get_ids(list_no)[offset];
38
32
  }
39
33
 
34
+ void InvertedLists::release_codes(size_t, const uint8_t*) const {}
40
35
 
41
- void InvertedLists::release_codes (size_t, const uint8_t *) const
42
- {}
43
-
44
- void InvertedLists::release_ids (size_t, const idx_t *) const
45
- {}
36
+ void InvertedLists::release_ids(size_t, const idx_t*) const {}
46
37
 
47
- void InvertedLists::prefetch_lists (const idx_t *, int) const
48
- {}
38
+ void InvertedLists::prefetch_lists(const idx_t*, int) const {}
49
39
 
50
- const uint8_t * InvertedLists::get_single_code (
51
- size_t list_no, size_t offset) const
52
- {
53
- assert (offset < list_size (list_no));
40
+ const uint8_t* InvertedLists::get_single_code(size_t list_no, size_t offset)
41
+ const {
42
+ assert(offset < list_size(list_no));
54
43
  return get_codes(list_no) + offset * code_size;
55
44
  }
56
45
 
57
- size_t InvertedLists::add_entry (size_t list_no, idx_t theid,
58
- const uint8_t *code)
59
- {
60
- return add_entries (list_no, 1, &theid, code);
46
+ size_t InvertedLists::add_entry(
47
+ size_t list_no,
48
+ idx_t theid,
49
+ const uint8_t* code) {
50
+ return add_entries(list_no, 1, &theid, code);
61
51
  }
62
52
 
63
- void InvertedLists::update_entry (size_t list_no, size_t offset,
64
- idx_t id, const uint8_t *code)
65
- {
66
- update_entries (list_no, offset, 1, &id, code);
53
+ void InvertedLists::update_entry(
54
+ size_t list_no,
55
+ size_t offset,
56
+ idx_t id,
57
+ const uint8_t* code) {
58
+ update_entries(list_no, offset, 1, &id, code);
67
59
  }
68
60
 
69
- void InvertedLists::reset () {
61
+ void InvertedLists::reset() {
70
62
  for (size_t i = 0; i < nlist; i++) {
71
- resize (i, 0);
63
+ resize(i, 0);
72
64
  }
73
65
  }
74
66
 
75
- void InvertedLists::merge_from (InvertedLists *oivf, size_t add_id) {
76
-
67
+ void InvertedLists::merge_from(InvertedLists* oivf, size_t add_id) {
77
68
  #pragma omp parallel for
78
69
  for (idx_t i = 0; i < nlist; i++) {
79
- size_t list_size = oivf->list_size (i);
80
- ScopedIds ids (oivf, i);
70
+ size_t list_size = oivf->list_size(i);
71
+ ScopedIds ids(oivf, i);
81
72
  if (add_id == 0) {
82
- add_entries (i, list_size, ids.get (),
83
- ScopedCodes (oivf, i).get());
73
+ add_entries(i, list_size, ids.get(), ScopedCodes(oivf, i).get());
84
74
  } else {
85
- std::vector <idx_t> new_ids (list_size);
75
+ std::vector<idx_t> new_ids(list_size);
86
76
 
87
77
  for (size_t j = 0; j < list_size; j++) {
88
- new_ids [j] = ids[j] + add_id;
78
+ new_ids[j] = ids[j] + add_id;
89
79
  }
90
- add_entries (i, list_size, new_ids.data(),
91
- ScopedCodes (oivf, i).get());
80
+ add_entries(
81
+ i, list_size, new_ids.data(), ScopedCodes(oivf, i).get());
92
82
  }
93
- oivf->resize (i, 0);
83
+ oivf->resize(i, 0);
94
84
  }
95
85
  }
96
86
 
97
- double InvertedLists::imbalance_factor () const {
87
+ double InvertedLists::imbalance_factor() const {
98
88
  std::vector<int> hist(nlist);
99
89
 
100
90
  for (size_t i = 0; i < nlist; i++) {
@@ -104,7 +94,7 @@ double InvertedLists::imbalance_factor () const {
104
94
  return faiss::imbalance_factor(nlist, hist.data());
105
95
  }
106
96
 
107
- void InvertedLists::print_stats () const {
97
+ void InvertedLists::print_stats() const {
108
98
  std::vector<int> sizes(40);
109
99
  for (size_t i = 0; i < nlist; i++) {
110
100
  for (size_t j = 0; j < sizes.size(); j++) {
@@ -121,7 +111,7 @@ void InvertedLists::print_stats () const {
121
111
  }
122
112
  }
123
113
 
124
- size_t InvertedLists::compute_ntotal () const {
114
+ size_t InvertedLists::compute_ntotal() const {
125
115
  size_t tot = 0;
126
116
  for (size_t i = 0; i < nlist; i++) {
127
117
  tot += list_size(i);
@@ -133,195 +123,183 @@ size_t InvertedLists::compute_ntotal () const {
133
123
  * ArrayInvertedLists implementation
134
124
  ******************************************/
135
125
 
136
- ArrayInvertedLists::ArrayInvertedLists (size_t nlist, size_t code_size):
137
- InvertedLists (nlist, code_size)
138
- {
139
- ids.resize (nlist);
140
- codes.resize (nlist);
141
- }
142
-
143
- size_t ArrayInvertedLists::add_entries (
144
- size_t list_no, size_t n_entry,
145
- const idx_t* ids_in, const uint8_t *code)
146
- {
147
- if (n_entry == 0) return 0;
148
- assert (list_no < nlist);
149
- size_t o = ids [list_no].size();
150
- ids [list_no].resize (o + n_entry);
151
- memcpy (&ids[list_no][o], ids_in, sizeof (ids_in[0]) * n_entry);
152
- codes [list_no].resize ((o + n_entry) * code_size);
153
- memcpy (&codes[list_no][o * code_size], code, code_size * n_entry);
126
+ ArrayInvertedLists::ArrayInvertedLists(size_t nlist, size_t code_size)
127
+ : InvertedLists(nlist, code_size) {
128
+ ids.resize(nlist);
129
+ codes.resize(nlist);
130
+ }
131
+
132
+ size_t ArrayInvertedLists::add_entries(
133
+ size_t list_no,
134
+ size_t n_entry,
135
+ const idx_t* ids_in,
136
+ const uint8_t* code) {
137
+ if (n_entry == 0)
138
+ return 0;
139
+ assert(list_no < nlist);
140
+ size_t o = ids[list_no].size();
141
+ ids[list_no].resize(o + n_entry);
142
+ memcpy(&ids[list_no][o], ids_in, sizeof(ids_in[0]) * n_entry);
143
+ codes[list_no].resize((o + n_entry) * code_size);
144
+ memcpy(&codes[list_no][o * code_size], code, code_size * n_entry);
154
145
  return o;
155
146
  }
156
147
 
157
- size_t ArrayInvertedLists::list_size(size_t list_no) const
158
- {
159
- assert (list_no < nlist);
148
+ size_t ArrayInvertedLists::list_size(size_t list_no) const {
149
+ assert(list_no < nlist);
160
150
  return ids[list_no].size();
161
151
  }
162
152
 
163
- const uint8_t * ArrayInvertedLists::get_codes (size_t list_no) const
164
- {
165
- assert (list_no < nlist);
153
+ const uint8_t* ArrayInvertedLists::get_codes(size_t list_no) const {
154
+ assert(list_no < nlist);
166
155
  return codes[list_no].data();
167
156
  }
168
157
 
169
-
170
- const InvertedLists::idx_t * ArrayInvertedLists::get_ids (size_t list_no) const
171
- {
172
- assert (list_no < nlist);
158
+ const InvertedLists::idx_t* ArrayInvertedLists::get_ids(size_t list_no) const {
159
+ assert(list_no < nlist);
173
160
  return ids[list_no].data();
174
161
  }
175
162
 
176
- void ArrayInvertedLists::resize (size_t list_no, size_t new_size)
177
- {
178
- ids[list_no].resize (new_size);
179
- codes[list_no].resize (new_size * code_size);
163
+ void ArrayInvertedLists::resize(size_t list_no, size_t new_size) {
164
+ ids[list_no].resize(new_size);
165
+ codes[list_no].resize(new_size * code_size);
180
166
  }
181
167
 
182
- void ArrayInvertedLists::update_entries (
183
- size_t list_no, size_t offset, size_t n_entry,
184
- const idx_t *ids_in, const uint8_t *codes_in)
185
- {
186
- assert (list_no < nlist);
187
- assert (n_entry + offset <= ids[list_no].size());
188
- memcpy (&ids[list_no][offset], ids_in, sizeof(ids_in[0]) * n_entry);
189
- memcpy (&codes[list_no][offset * code_size], codes_in, code_size * n_entry);
168
+ void ArrayInvertedLists::update_entries(
169
+ size_t list_no,
170
+ size_t offset,
171
+ size_t n_entry,
172
+ const idx_t* ids_in,
173
+ const uint8_t* codes_in) {
174
+ assert(list_no < nlist);
175
+ assert(n_entry + offset <= ids[list_no].size());
176
+ memcpy(&ids[list_no][offset], ids_in, sizeof(ids_in[0]) * n_entry);
177
+ memcpy(&codes[list_no][offset * code_size], codes_in, code_size * n_entry);
190
178
  }
191
179
 
192
-
193
- ArrayInvertedLists::~ArrayInvertedLists ()
194
- {}
180
+ ArrayInvertedLists::~ArrayInvertedLists() {}
195
181
 
196
182
  /*****************************************************************
197
183
  * Meta-inverted list implementations
198
184
  *****************************************************************/
199
185
 
200
-
201
- size_t ReadOnlyInvertedLists::add_entries (
202
- size_t , size_t ,
203
- const idx_t* , const uint8_t *)
204
- {
205
- FAISS_THROW_MSG ("not implemented");
186
+ size_t ReadOnlyInvertedLists::add_entries(
187
+ size_t,
188
+ size_t,
189
+ const idx_t*,
190
+ const uint8_t*) {
191
+ FAISS_THROW_MSG("not implemented");
206
192
  }
207
193
 
208
- void ReadOnlyInvertedLists::update_entries (size_t, size_t , size_t ,
209
- const idx_t *, const uint8_t *)
210
- {
211
- FAISS_THROW_MSG ("not implemented");
194
+ void ReadOnlyInvertedLists::update_entries(
195
+ size_t,
196
+ size_t,
197
+ size_t,
198
+ const idx_t*,
199
+ const uint8_t*) {
200
+ FAISS_THROW_MSG("not implemented");
212
201
  }
213
202
 
214
- void ReadOnlyInvertedLists::resize (size_t , size_t )
215
- {
216
- FAISS_THROW_MSG ("not implemented");
203
+ void ReadOnlyInvertedLists::resize(size_t, size_t) {
204
+ FAISS_THROW_MSG("not implemented");
217
205
  }
218
206
 
219
-
220
-
221
207
  /*****************************************
222
208
  * HStackInvertedLists implementation
223
209
  ******************************************/
224
210
 
225
- HStackInvertedLists::HStackInvertedLists (
226
- int nil, const InvertedLists **ils_in):
227
- ReadOnlyInvertedLists (nil > 0 ? ils_in[0]->nlist : 0,
228
- nil > 0 ? ils_in[0]->code_size : 0)
229
- {
230
- FAISS_THROW_IF_NOT (nil > 0);
211
+ HStackInvertedLists::HStackInvertedLists(int nil, const InvertedLists** ils_in)
212
+ : ReadOnlyInvertedLists(
213
+ nil > 0 ? ils_in[0]->nlist : 0,
214
+ nil > 0 ? ils_in[0]->code_size : 0) {
215
+ FAISS_THROW_IF_NOT(nil > 0);
231
216
  for (int i = 0; i < nil; i++) {
232
- ils.push_back (ils_in[i]);
233
- FAISS_THROW_IF_NOT (ils_in[i]->code_size == code_size &&
234
- ils_in[i]->nlist == nlist);
217
+ ils.push_back(ils_in[i]);
218
+ FAISS_THROW_IF_NOT(
219
+ ils_in[i]->code_size == code_size && ils_in[i]->nlist == nlist);
235
220
  }
236
221
  }
237
222
 
238
- size_t HStackInvertedLists::list_size(size_t list_no) const
239
- {
223
+ size_t HStackInvertedLists::list_size(size_t list_no) const {
240
224
  size_t sz = 0;
241
225
  for (int i = 0; i < ils.size(); i++) {
242
- const InvertedLists *il = ils[i];
243
- sz += il->list_size (list_no);
226
+ const InvertedLists* il = ils[i];
227
+ sz += il->list_size(list_no);
244
228
  }
245
229
  return sz;
246
230
  }
247
231
 
248
- const uint8_t * HStackInvertedLists::get_codes (size_t list_no) const
249
- {
250
- uint8_t *codes = new uint8_t [code_size * list_size(list_no)], *c = codes;
232
+ const uint8_t* HStackInvertedLists::get_codes(size_t list_no) const {
233
+ uint8_t *codes = new uint8_t[code_size * list_size(list_no)], *c = codes;
251
234
 
252
235
  for (int i = 0; i < ils.size(); i++) {
253
- const InvertedLists *il = ils[i];
236
+ const InvertedLists* il = ils[i];
254
237
  size_t sz = il->list_size(list_no) * code_size;
255
238
  if (sz > 0) {
256
- memcpy (c, ScopedCodes (il, list_no).get(), sz);
239
+ memcpy(c, ScopedCodes(il, list_no).get(), sz);
257
240
  c += sz;
258
241
  }
259
242
  }
260
243
  return codes;
261
244
  }
262
245
 
263
- const uint8_t * HStackInvertedLists::get_single_code (
264
- size_t list_no, size_t offset) const
265
- {
246
+ const uint8_t* HStackInvertedLists::get_single_code(
247
+ size_t list_no,
248
+ size_t offset) const {
266
249
  for (int i = 0; i < ils.size(); i++) {
267
- const InvertedLists *il = ils[i];
268
- size_t sz = il->list_size (list_no);
250
+ const InvertedLists* il = ils[i];
251
+ size_t sz = il->list_size(list_no);
269
252
  if (offset < sz) {
270
253
  // here we have to copy the code, otherwise it will crash at dealloc
271
- uint8_t * code = new uint8_t [code_size];
272
- memcpy (code, ScopedCodes (il, list_no, offset).get(), code_size);
254
+ uint8_t* code = new uint8_t[code_size];
255
+ memcpy(code, ScopedCodes(il, list_no, offset).get(), code_size);
273
256
  return code;
274
257
  }
275
258
  offset -= sz;
276
259
  }
277
- FAISS_THROW_FMT ("offset %zd unknown", offset);
260
+ FAISS_THROW_FMT("offset %zd unknown", offset);
278
261
  }
279
262
 
280
-
281
- void HStackInvertedLists::release_codes (size_t, const uint8_t *codes) const {
282
- delete [] codes;
263
+ void HStackInvertedLists::release_codes(size_t, const uint8_t* codes) const {
264
+ delete[] codes;
283
265
  }
284
266
 
285
- const Index::idx_t * HStackInvertedLists::get_ids (size_t list_no) const
286
- {
287
- idx_t *ids = new idx_t [list_size(list_no)], *c = ids;
267
+ const Index::idx_t* HStackInvertedLists::get_ids(size_t list_no) const {
268
+ idx_t *ids = new idx_t[list_size(list_no)], *c = ids;
288
269
 
289
270
  for (int i = 0; i < ils.size(); i++) {
290
- const InvertedLists *il = ils[i];
271
+ const InvertedLists* il = ils[i];
291
272
  size_t sz = il->list_size(list_no);
292
273
  if (sz > 0) {
293
- memcpy (c, ScopedIds (il, list_no).get(), sz * sizeof(idx_t));
274
+ memcpy(c, ScopedIds(il, list_no).get(), sz * sizeof(idx_t));
294
275
  c += sz;
295
276
  }
296
277
  }
297
278
  return ids;
298
279
  }
299
280
 
300
- Index::idx_t HStackInvertedLists::get_single_id (
301
- size_t list_no, size_t offset) const
302
- {
303
-
281
+ Index::idx_t HStackInvertedLists::get_single_id(size_t list_no, size_t offset)
282
+ const {
304
283
  for (int i = 0; i < ils.size(); i++) {
305
- const InvertedLists *il = ils[i];
306
- size_t sz = il->list_size (list_no);
284
+ const InvertedLists* il = ils[i];
285
+ size_t sz = il->list_size(list_no);
307
286
  if (offset < sz) {
308
- return il->get_single_id (list_no, offset);
287
+ return il->get_single_id(list_no, offset);
309
288
  }
310
289
  offset -= sz;
311
290
  }
312
- FAISS_THROW_FMT ("offset %zd unknown", offset);
291
+ FAISS_THROW_FMT("offset %zd unknown", offset);
313
292
  }
314
293
 
315
-
316
- void HStackInvertedLists::release_ids (size_t, const idx_t *ids) const {
317
- delete [] ids;
294
+ void HStackInvertedLists::release_ids(size_t, const idx_t* ids) const {
295
+ delete[] ids;
318
296
  }
319
297
 
320
- void HStackInvertedLists::prefetch_lists (const idx_t *list_nos, int nlist) const
321
- {
298
+ void HStackInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
299
+ const {
322
300
  for (int i = 0; i < ils.size(); i++) {
323
- const InvertedLists *il = ils[i];
324
- il->prefetch_lists (list_nos, nlist);
301
+ const InvertedLists* il = ils[i];
302
+ il->prefetch_lists(list_nos, nlist);
325
303
  }
326
304
  }
327
305
 
@@ -329,203 +307,184 @@ void HStackInvertedLists::prefetch_lists (const idx_t *list_nos, int nlist) cons
329
307
  * SliceInvertedLists implementation
330
308
  ******************************************/
331
309
 
332
-
333
310
  namespace {
334
311
 
335
- using idx_t = InvertedLists::idx_t;
336
-
337
- idx_t translate_list_no (const SliceInvertedLists *sil,
338
- idx_t list_no) {
339
- FAISS_THROW_IF_NOT (list_no >= 0 && list_no < sil->nlist);
340
- return list_no + sil->i0;
341
- }
342
-
343
- };
344
-
312
+ using idx_t = InvertedLists::idx_t;
345
313
 
314
+ idx_t translate_list_no(const SliceInvertedLists* sil, idx_t list_no) {
315
+ FAISS_THROW_IF_NOT(list_no >= 0 && list_no < sil->nlist);
316
+ return list_no + sil->i0;
317
+ }
346
318
 
347
- SliceInvertedLists::SliceInvertedLists (
348
- const InvertedLists *il, idx_t i0, idx_t i1):
349
- ReadOnlyInvertedLists (i1 - i0, il->code_size),
350
- il (il), i0(i0), i1(i1)
351
- {
319
+ }; // namespace
352
320
 
353
- }
321
+ SliceInvertedLists::SliceInvertedLists(
322
+ const InvertedLists* il,
323
+ idx_t i0,
324
+ idx_t i1)
325
+ : ReadOnlyInvertedLists(i1 - i0, il->code_size),
326
+ il(il),
327
+ i0(i0),
328
+ i1(i1) {}
354
329
 
355
- size_t SliceInvertedLists::list_size(size_t list_no) const
356
- {
357
- return il->list_size (translate_list_no (this, list_no));
330
+ size_t SliceInvertedLists::list_size(size_t list_no) const {
331
+ return il->list_size(translate_list_no(this, list_no));
358
332
  }
359
333
 
360
- const uint8_t * SliceInvertedLists::get_codes (size_t list_no) const
361
- {
362
- return il->get_codes (translate_list_no (this, list_no));
334
+ const uint8_t* SliceInvertedLists::get_codes(size_t list_no) const {
335
+ return il->get_codes(translate_list_no(this, list_no));
363
336
  }
364
337
 
365
- const uint8_t * SliceInvertedLists::get_single_code (
366
- size_t list_no, size_t offset) const
367
- {
368
- return il->get_single_code (translate_list_no (this, list_no), offset);
338
+ const uint8_t* SliceInvertedLists::get_single_code(
339
+ size_t list_no,
340
+ size_t offset) const {
341
+ return il->get_single_code(translate_list_no(this, list_no), offset);
369
342
  }
370
343
 
371
-
372
- void SliceInvertedLists::release_codes (
373
- size_t list_no, const uint8_t *codes) const {
374
- return il->release_codes (translate_list_no (this, list_no), codes);
344
+ void SliceInvertedLists::release_codes(size_t list_no, const uint8_t* codes)
345
+ const {
346
+ return il->release_codes(translate_list_no(this, list_no), codes);
375
347
  }
376
348
 
377
- const Index::idx_t * SliceInvertedLists::get_ids (size_t list_no) const
378
- {
379
- return il->get_ids (translate_list_no (this, list_no));
349
+ const Index::idx_t* SliceInvertedLists::get_ids(size_t list_no) const {
350
+ return il->get_ids(translate_list_no(this, list_no));
380
351
  }
381
352
 
382
- Index::idx_t SliceInvertedLists::get_single_id (
383
- size_t list_no, size_t offset) const
384
- {
385
- return il->get_single_id (translate_list_no (this, list_no), offset);
353
+ Index::idx_t SliceInvertedLists::get_single_id(size_t list_no, size_t offset)
354
+ const {
355
+ return il->get_single_id(translate_list_no(this, list_no), offset);
386
356
  }
387
357
 
388
-
389
- void SliceInvertedLists::release_ids (size_t list_no, const idx_t *ids) const {
390
- return il->release_ids (translate_list_no (this, list_no), ids);
358
+ void SliceInvertedLists::release_ids(size_t list_no, const idx_t* ids) const {
359
+ return il->release_ids(translate_list_no(this, list_no), ids);
391
360
  }
392
361
 
393
- void SliceInvertedLists::prefetch_lists (const idx_t *list_nos, int nlist) const
394
- {
362
+ void SliceInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
363
+ const {
395
364
  std::vector<idx_t> translated_list_nos;
396
365
  for (int j = 0; j < nlist; j++) {
397
366
  idx_t list_no = list_nos[j];
398
- if (list_no < 0) continue;
399
- translated_list_nos.push_back (translate_list_no (this, list_no));
367
+ if (list_no < 0)
368
+ continue;
369
+ translated_list_nos.push_back(translate_list_no(this, list_no));
400
370
  }
401
- il->prefetch_lists (translated_list_nos.data(),
402
- translated_list_nos.size());
371
+ il->prefetch_lists(translated_list_nos.data(), translated_list_nos.size());
403
372
  }
404
373
 
405
-
406
374
  /*****************************************
407
375
  * VStackInvertedLists implementation
408
376
  ******************************************/
409
377
 
410
378
  namespace {
411
379
 
412
- using idx_t = InvertedLists::idx_t;
413
-
414
- // find the invlist this number belongs to
415
- int translate_list_no (const VStackInvertedLists *vil,
416
- idx_t list_no) {
417
- FAISS_THROW_IF_NOT (list_no >= 0 && list_no < vil->nlist);
418
- int i0 = 0, i1 = vil->ils.size();
419
- const idx_t *cumsz = vil->cumsz.data();
420
- while (i0 + 1 < i1) {
421
- int imed = (i0 + i1) / 2;
422
- if (list_no >= cumsz[imed]) {
423
- i0 = imed;
424
- } else {
425
- i1 = imed;
426
- }
380
+ using idx_t = InvertedLists::idx_t;
381
+
382
+ // find the invlist this number belongs to
383
+ int translate_list_no(const VStackInvertedLists* vil, idx_t list_no) {
384
+ FAISS_THROW_IF_NOT(list_no >= 0 && list_no < vil->nlist);
385
+ int i0 = 0, i1 = vil->ils.size();
386
+ const idx_t* cumsz = vil->cumsz.data();
387
+ while (i0 + 1 < i1) {
388
+ int imed = (i0 + i1) / 2;
389
+ if (list_no >= cumsz[imed]) {
390
+ i0 = imed;
391
+ } else {
392
+ i1 = imed;
427
393
  }
428
- assert(list_no >= cumsz[i0] && list_no < cumsz[i0 + 1]);
429
- return i0;
430
394
  }
395
+ assert(list_no >= cumsz[i0] && list_no < cumsz[i0 + 1]);
396
+ return i0;
397
+ }
431
398
 
432
- idx_t sum_il_sizes (int nil, const InvertedLists **ils_in) {
433
- idx_t tot = 0;
434
- for (int i = 0; i < nil; i++) {
435
- tot += ils_in[i]->nlist;
436
- }
437
- return tot;
399
+ idx_t sum_il_sizes(int nil, const InvertedLists** ils_in) {
400
+ idx_t tot = 0;
401
+ for (int i = 0; i < nil; i++) {
402
+ tot += ils_in[i]->nlist;
438
403
  }
404
+ return tot;
405
+ }
439
406
 
440
- };
441
-
442
-
407
+ }; // namespace
443
408
 
444
- VStackInvertedLists::VStackInvertedLists (
445
- int nil, const InvertedLists **ils_in):
446
- ReadOnlyInvertedLists (sum_il_sizes(nil, ils_in),
447
- nil > 0 ? ils_in[0]->code_size : 0)
448
- {
449
- FAISS_THROW_IF_NOT (nil > 0);
450
- cumsz.resize (nil + 1);
409
+ VStackInvertedLists::VStackInvertedLists(int nil, const InvertedLists** ils_in)
410
+ : ReadOnlyInvertedLists(
411
+ sum_il_sizes(nil, ils_in),
412
+ nil > 0 ? ils_in[0]->code_size : 0) {
413
+ FAISS_THROW_IF_NOT(nil > 0);
414
+ cumsz.resize(nil + 1);
451
415
  for (int i = 0; i < nil; i++) {
452
- ils.push_back (ils_in[i]);
453
- FAISS_THROW_IF_NOT (ils_in[i]->code_size == code_size);
416
+ ils.push_back(ils_in[i]);
417
+ FAISS_THROW_IF_NOT(ils_in[i]->code_size == code_size);
454
418
  cumsz[i + 1] = cumsz[i] + ils_in[i]->nlist;
455
419
  }
456
420
  }
457
421
 
458
- size_t VStackInvertedLists::list_size(size_t list_no) const
459
- {
460
- int i = translate_list_no (this, list_no);
422
+ size_t VStackInvertedLists::list_size(size_t list_no) const {
423
+ int i = translate_list_no(this, list_no);
461
424
  list_no -= cumsz[i];
462
- return ils[i]->list_size (list_no);
425
+ return ils[i]->list_size(list_no);
463
426
  }
464
427
 
465
- const uint8_t * VStackInvertedLists::get_codes (size_t list_no) const
466
- {
467
- int i = translate_list_no (this, list_no);
428
+ const uint8_t* VStackInvertedLists::get_codes(size_t list_no) const {
429
+ int i = translate_list_no(this, list_no);
468
430
  list_no -= cumsz[i];
469
- return ils[i]->get_codes (list_no);
431
+ return ils[i]->get_codes(list_no);
470
432
  }
471
433
 
472
- const uint8_t * VStackInvertedLists::get_single_code (
473
- size_t list_no, size_t offset) const
474
- {
475
- int i = translate_list_no (this, list_no);
434
+ const uint8_t* VStackInvertedLists::get_single_code(
435
+ size_t list_no,
436
+ size_t offset) const {
437
+ int i = translate_list_no(this, list_no);
476
438
  list_no -= cumsz[i];
477
- return ils[i]->get_single_code (list_no, offset);
439
+ return ils[i]->get_single_code(list_no, offset);
478
440
  }
479
441
 
480
-
481
- void VStackInvertedLists::release_codes (
482
- size_t list_no, const uint8_t *codes) const {
483
- int i = translate_list_no (this, list_no);
442
+ void VStackInvertedLists::release_codes(size_t list_no, const uint8_t* codes)
443
+ const {
444
+ int i = translate_list_no(this, list_no);
484
445
  list_no -= cumsz[i];
485
- return ils[i]->release_codes (list_no, codes);
446
+ return ils[i]->release_codes(list_no, codes);
486
447
  }
487
448
 
488
- const Index::idx_t * VStackInvertedLists::get_ids (size_t list_no) const
489
- {
490
- int i = translate_list_no (this, list_no);
449
+ const Index::idx_t* VStackInvertedLists::get_ids(size_t list_no) const {
450
+ int i = translate_list_no(this, list_no);
491
451
  list_no -= cumsz[i];
492
- return ils[i]->get_ids (list_no);
452
+ return ils[i]->get_ids(list_no);
493
453
  }
494
454
 
495
- Index::idx_t VStackInvertedLists::get_single_id (
496
- size_t list_no, size_t offset) const
497
- {
498
- int i = translate_list_no (this, list_no);
455
+ Index::idx_t VStackInvertedLists::get_single_id(size_t list_no, size_t offset)
456
+ const {
457
+ int i = translate_list_no(this, list_no);
499
458
  list_no -= cumsz[i];
500
- return ils[i]->get_single_id (list_no, offset);
459
+ return ils[i]->get_single_id(list_no, offset);
501
460
  }
502
461
 
503
-
504
- void VStackInvertedLists::release_ids (size_t list_no, const idx_t *ids) const {
505
- int i = translate_list_no (this, list_no);
462
+ void VStackInvertedLists::release_ids(size_t list_no, const idx_t* ids) const {
463
+ int i = translate_list_no(this, list_no);
506
464
  list_no -= cumsz[i];
507
- return ils[i]->release_ids (list_no, ids);
465
+ return ils[i]->release_ids(list_no, ids);
508
466
  }
509
467
 
510
- void VStackInvertedLists::prefetch_lists (
511
- const idx_t *list_nos, int nlist) const
512
- {
513
- std::vector<int> ilno (nlist, -1);
514
- std::vector<int> n_per_il (ils.size(), 0);
468
+ void VStackInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
469
+ const {
470
+ std::vector<int> ilno(nlist, -1);
471
+ std::vector<int> n_per_il(ils.size(), 0);
515
472
  for (int j = 0; j < nlist; j++) {
516
473
  idx_t list_no = list_nos[j];
517
- if (list_no < 0) continue;
518
- int i = ilno[j] = translate_list_no (this, list_no);
474
+ if (list_no < 0)
475
+ continue;
476
+ int i = ilno[j] = translate_list_no(this, list_no);
519
477
  n_per_il[i]++;
520
478
  }
521
- std::vector<int> cum_n_per_il (ils.size() + 1, 0);
479
+ std::vector<int> cum_n_per_il(ils.size() + 1, 0);
522
480
  for (int j = 0; j < ils.size(); j++) {
523
481
  cum_n_per_il[j + 1] = cum_n_per_il[j] + n_per_il[j];
524
482
  }
525
- std::vector<idx_t> sorted_list_nos (cum_n_per_il.back());
483
+ std::vector<idx_t> sorted_list_nos(cum_n_per_il.back());
526
484
  for (int j = 0; j < nlist; j++) {
527
485
  idx_t list_no = list_nos[j];
528
- if (list_no < 0) continue;
486
+ if (list_no < 0)
487
+ continue;
529
488
  int i = ilno[j];
530
489
  list_no -= cumsz[i];
531
490
  sorted_list_nos[cum_n_per_il[i]++] = list_no;
@@ -535,158 +494,142 @@ void VStackInvertedLists::prefetch_lists (
535
494
  for (int j = 0; j < ils.size(); j++) {
536
495
  int i1 = i0 + n_per_il[j];
537
496
  if (i1 > i0) {
538
- ils[j]->prefetch_lists (sorted_list_nos.data() + i0,
539
- i1 - i0);
497
+ ils[j]->prefetch_lists(sorted_list_nos.data() + i0, i1 - i0);
540
498
  }
541
499
  i0 = i1;
542
500
  }
543
501
  }
544
502
 
545
-
546
-
547
503
  /*****************************************
548
504
  * MaskedInvertedLists implementation
549
505
  ******************************************/
550
506
 
551
-
552
- MaskedInvertedLists::MaskedInvertedLists (const InvertedLists *il0,
553
- const InvertedLists *il1):
554
- ReadOnlyInvertedLists (il0->nlist, il0->code_size),
555
- il0 (il0), il1 (il1)
556
- {
557
- FAISS_THROW_IF_NOT (il1->nlist == nlist);
558
- FAISS_THROW_IF_NOT (il1->code_size == code_size);
507
+ MaskedInvertedLists::MaskedInvertedLists(
508
+ const InvertedLists* il0,
509
+ const InvertedLists* il1)
510
+ : ReadOnlyInvertedLists(il0->nlist, il0->code_size),
511
+ il0(il0),
512
+ il1(il1) {
513
+ FAISS_THROW_IF_NOT(il1->nlist == nlist);
514
+ FAISS_THROW_IF_NOT(il1->code_size == code_size);
559
515
  }
560
516
 
561
- size_t MaskedInvertedLists::list_size(size_t list_no) const
562
- {
517
+ size_t MaskedInvertedLists::list_size(size_t list_no) const {
563
518
  size_t sz = il0->list_size(list_no);
564
519
  return sz ? sz : il1->list_size(list_no);
565
520
  }
566
521
 
567
- const uint8_t * MaskedInvertedLists::get_codes (size_t list_no) const
568
- {
522
+ const uint8_t* MaskedInvertedLists::get_codes(size_t list_no) const {
569
523
  size_t sz = il0->list_size(list_no);
570
524
  return (sz ? il0 : il1)->get_codes(list_no);
571
525
  }
572
526
 
573
- const idx_t * MaskedInvertedLists::get_ids (size_t list_no) const
574
- {
575
- size_t sz = il0->list_size (list_no);
576
- return (sz ? il0 : il1)->get_ids (list_no);
527
+ const idx_t* MaskedInvertedLists::get_ids(size_t list_no) const {
528
+ size_t sz = il0->list_size(list_no);
529
+ return (sz ? il0 : il1)->get_ids(list_no);
577
530
  }
578
531
 
579
- void MaskedInvertedLists::release_codes (
580
- size_t list_no, const uint8_t *codes) const
581
- {
582
- size_t sz = il0->list_size (list_no);
583
- (sz ? il0 : il1)->release_codes (list_no, codes);
532
+ void MaskedInvertedLists::release_codes(size_t list_no, const uint8_t* codes)
533
+ const {
534
+ size_t sz = il0->list_size(list_no);
535
+ (sz ? il0 : il1)->release_codes(list_no, codes);
584
536
  }
585
537
 
586
- void MaskedInvertedLists::release_ids (size_t list_no, const idx_t *ids) const
587
- {
588
- size_t sz = il0->list_size (list_no);
589
- (sz ? il0 : il1)->release_ids (list_no, ids);
538
+ void MaskedInvertedLists::release_ids(size_t list_no, const idx_t* ids) const {
539
+ size_t sz = il0->list_size(list_no);
540
+ (sz ? il0 : il1)->release_ids(list_no, ids);
590
541
  }
591
542
 
592
- idx_t MaskedInvertedLists::get_single_id (size_t list_no, size_t offset) const
593
- {
594
- size_t sz = il0->list_size (list_no);
595
- return (sz ? il0 : il1)->get_single_id (list_no, offset);
543
+ idx_t MaskedInvertedLists::get_single_id(size_t list_no, size_t offset) const {
544
+ size_t sz = il0->list_size(list_no);
545
+ return (sz ? il0 : il1)->get_single_id(list_no, offset);
596
546
  }
597
547
 
598
- const uint8_t * MaskedInvertedLists::get_single_code (
599
- size_t list_no, size_t offset) const
600
- {
601
- size_t sz = il0->list_size (list_no);
602
- return (sz ? il0 : il1)->get_single_code (list_no, offset);
548
+ const uint8_t* MaskedInvertedLists::get_single_code(
549
+ size_t list_no,
550
+ size_t offset) const {
551
+ size_t sz = il0->list_size(list_no);
552
+ return (sz ? il0 : il1)->get_single_code(list_no, offset);
603
553
  }
604
554
 
605
- void MaskedInvertedLists::prefetch_lists (
606
- const idx_t *list_nos, int nlist) const
607
- {
555
+ void MaskedInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
556
+ const {
608
557
  std::vector<idx_t> list0, list1;
609
558
  for (int i = 0; i < nlist; i++) {
610
559
  idx_t list_no = list_nos[i];
611
- if (list_no < 0) continue;
560
+ if (list_no < 0)
561
+ continue;
612
562
  size_t sz = il0->list_size(list_no);
613
- (sz ? list0 : list1).push_back (list_no);
563
+ (sz ? list0 : list1).push_back(list_no);
614
564
  }
615
- il0->prefetch_lists (list0.data(), list0.size());
616
- il1->prefetch_lists (list1.data(), list1.size());
565
+ il0->prefetch_lists(list0.data(), list0.size());
566
+ il1->prefetch_lists(list1.data(), list1.size());
617
567
  }
618
568
 
619
569
  /*****************************************
620
570
  * MaskedInvertedLists implementation
621
571
  ******************************************/
622
572
 
573
+ StopWordsInvertedLists::StopWordsInvertedLists(
574
+ const InvertedLists* il0,
575
+ size_t maxsize)
576
+ : ReadOnlyInvertedLists(il0->nlist, il0->code_size),
577
+ il0(il0),
578
+ maxsize(maxsize) {}
623
579
 
624
- StopWordsInvertedLists::StopWordsInvertedLists (
625
- const InvertedLists *il0, size_t maxsize):
626
- ReadOnlyInvertedLists (il0->nlist, il0->code_size),
627
- il0 (il0), maxsize (maxsize)
628
- {
629
-
630
- }
631
-
632
- size_t StopWordsInvertedLists::list_size(size_t list_no) const
633
- {
580
+ size_t StopWordsInvertedLists::list_size(size_t list_no) const {
634
581
  size_t sz = il0->list_size(list_no);
635
582
  return sz < maxsize ? sz : 0;
636
583
  }
637
584
 
638
- const uint8_t * StopWordsInvertedLists::get_codes (size_t list_no) const
639
- {
640
- return il0->list_size(list_no) < maxsize ? il0->get_codes(list_no) : nullptr;
585
+ const uint8_t* StopWordsInvertedLists::get_codes(size_t list_no) const {
586
+ return il0->list_size(list_no) < maxsize ? il0->get_codes(list_no)
587
+ : nullptr;
641
588
  }
642
589
 
643
- const idx_t * StopWordsInvertedLists::get_ids (size_t list_no) const
644
- {
590
+ const idx_t* StopWordsInvertedLists::get_ids(size_t list_no) const {
645
591
  return il0->list_size(list_no) < maxsize ? il0->get_ids(list_no) : nullptr;
646
592
  }
647
593
 
648
- void StopWordsInvertedLists::release_codes (
649
- size_t list_no, const uint8_t *codes) const
650
- {
651
- if (il0->list_size (list_no) < maxsize) {
652
- il0->release_codes (list_no, codes);
594
+ void StopWordsInvertedLists::release_codes(size_t list_no, const uint8_t* codes)
595
+ const {
596
+ if (il0->list_size(list_no) < maxsize) {
597
+ il0->release_codes(list_no, codes);
653
598
  }
654
599
  }
655
600
 
656
- void StopWordsInvertedLists::release_ids (size_t list_no, const idx_t *ids) const
657
- {
658
- if (il0->list_size (list_no) < maxsize) {
659
- il0->release_ids (list_no, ids);
601
+ void StopWordsInvertedLists::release_ids(size_t list_no, const idx_t* ids)
602
+ const {
603
+ if (il0->list_size(list_no) < maxsize) {
604
+ il0->release_ids(list_no, ids);
660
605
  }
661
606
  }
662
607
 
663
- idx_t StopWordsInvertedLists::get_single_id (size_t list_no, size_t offset) const
664
- {
665
- FAISS_THROW_IF_NOT(il0->list_size (list_no) < maxsize);
666
- return il0->get_single_id (list_no, offset);
608
+ idx_t StopWordsInvertedLists::get_single_id(size_t list_no, size_t offset)
609
+ const {
610
+ FAISS_THROW_IF_NOT(il0->list_size(list_no) < maxsize);
611
+ return il0->get_single_id(list_no, offset);
667
612
  }
668
613
 
669
- const uint8_t * StopWordsInvertedLists::get_single_code (
670
- size_t list_no, size_t offset) const
671
- {
672
- FAISS_THROW_IF_NOT(il0->list_size (list_no) < maxsize);
673
- return il0->get_single_code (list_no, offset);
614
+ const uint8_t* StopWordsInvertedLists::get_single_code(
615
+ size_t list_no,
616
+ size_t offset) const {
617
+ FAISS_THROW_IF_NOT(il0->list_size(list_no) < maxsize);
618
+ return il0->get_single_code(list_no, offset);
674
619
  }
675
620
 
676
- void StopWordsInvertedLists::prefetch_lists (
677
- const idx_t *list_nos, int nlist) const
678
- {
621
+ void StopWordsInvertedLists::prefetch_lists(const idx_t* list_nos, int nlist)
622
+ const {
679
623
  std::vector<idx_t> list0;
680
624
  for (int i = 0; i < nlist; i++) {
681
625
  idx_t list_no = list_nos[i];
682
- if (list_no < 0) continue;
626
+ if (list_no < 0)
627
+ continue;
683
628
  if (il0->list_size(list_no) < maxsize) {
684
629
  list0.push_back(list_no);
685
630
  }
686
631
  }
687
- il0->prefetch_lists (list0.data(), list0.size());
632
+ il0->prefetch_lists(list0.data(), list0.size());
688
633
  }
689
634
 
690
-
691
-
692
635
  } // namespace faiss