sparsebit.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  1. /*
  2. * Sparse bit array
  3. *
  4. * Copyright (C) 2018, Google LLC.
  5. * Copyright (C) 2018, Red Hat, Inc. (code style cleanup and fuzzing driver)
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2.
  8. *
  9. * This library provides functions to support a memory efficient bit array,
  10. * with an index size of 2^64. A sparsebit array is allocated through
  11. * the use sparsebit_alloc() and free'd via sparsebit_free(),
  12. * such as in the following:
  13. *
  14. * struct sparsebit *s;
  15. * s = sparsebit_alloc();
  16. * sparsebit_free(&s);
  17. *
  18. * The struct sparsebit type resolves down to a struct sparsebit.
  19. * Note that, sparsebit_free() takes a pointer to the sparsebit
  20. * structure. This is so that sparsebit_free() is able to poison
  21. * the pointer (e.g. set it to NULL) to the struct sparsebit before
  22. * returning to the caller.
  23. *
  24. * Between the return of sparsebit_alloc() and the call of
  25. * sparsebit_free(), there are multiple query and modifying operations
  26. * that can be performed on the allocated sparsebit array. All of
  27. * these operations take as a parameter the value returned from
  28. * sparsebit_alloc() and most also take a bit index. Frequently
  29. * used routines include:
  30. *
  31. * ---- Query Operations
  32. * sparsebit_is_set(s, idx)
  33. * sparsebit_is_clear(s, idx)
  34. * sparsebit_any_set(s)
  35. * sparsebit_first_set(s)
  36. * sparsebit_next_set(s, prev_idx)
  37. *
  38. * ---- Modifying Operations
  39. * sparsebit_set(s, idx)
  40. * sparsebit_clear(s, idx)
  41. * sparsebit_set_num(s, idx, num);
  42. * sparsebit_clear_num(s, idx, num);
  43. *
  44. * A common operation, is to itterate over all the bits set in a test
  45. * sparsebit array. This can be done via code with the following structure:
  46. *
  47. * sparsebit_idx_t idx;
  48. * if (sparsebit_any_set(s)) {
  49. * idx = sparsebit_first_set(s);
  50. * do {
  51. * ...
  52. * idx = sparsebit_next_set(s, idx);
  53. * } while (idx != 0);
  54. * }
  55. *
  56. * The index of the first bit set needs to be obtained via
  57. * sparsebit_first_set(), because sparsebit_next_set(), needs
  58. * the index of the previously set. The sparsebit_idx_t type is
  59. * unsigned, so there is no previous index before 0 that is available.
  60. * Also, the call to sparsebit_first_set() is not made unless there
  61. * is at least 1 bit in the array set. This is because sparsebit_first_set()
  62. * aborts if sparsebit_first_set() is called with no bits set.
  63. * It is the callers responsibility to assure that the
  64. * sparsebit array has at least a single bit set before calling
  65. * sparsebit_first_set().
  66. *
  67. * ==== Implementation Overview ====
  68. * For the most part the internal implementation of sparsebit is
  69. * opaque to the caller. One important implementation detail that the
  70. * caller may need to be aware of is the spatial complexity of the
  71. * implementation. This implementation of a sparsebit array is not
  72. * only sparse, in that it uses memory proportional to the number of bits
  73. * set. It is also efficient in memory usage when most of the bits are
  74. * set.
  75. *
  76. * At a high-level the state of the bit settings are maintained through
  77. * the use of a binary-search tree, where each node contains at least
  78. * the following members:
  79. *
  80. * typedef uint64_t sparsebit_idx_t;
  81. * typedef uint64_t sparsebit_num_t;
  82. *
  83. * sparsebit_idx_t idx;
  84. * uint32_t mask;
  85. * sparsebit_num_t num_after;
  86. *
  87. * The idx member contains the bit index of the first bit described by this
  88. * node, while the mask member stores the setting of the first 32-bits.
  89. * The setting of the bit at idx + n, where 0 <= n < 32, is located in the
  90. * mask member at 1 << n.
  91. *
  92. * Nodes are sorted by idx and the bits described by two nodes will never
  93. * overlap. The idx member is always aligned to the mask size, i.e. a
  94. * multiple of 32.
  95. *
  96. * Beyond a typical implementation, the nodes in this implementation also
  97. * contains a member named num_after. The num_after member holds the
  98. * number of bits immediately after the mask bits that are contiguously set.
  99. * The use of the num_after member allows this implementation to efficiently
  100. * represent cases where most bits are set. For example, the case of all
  101. * but the last two bits set, is represented by the following two nodes:
  102. *
  103. * node 0 - idx: 0x0 mask: 0xffffffff num_after: 0xffffffffffffffc0
  104. * node 1 - idx: 0xffffffffffffffe0 mask: 0x3fffffff num_after: 0
  105. *
  106. * ==== Invariants ====
  107. * This implementation usses the following invariants:
  108. *
  109. * + Node are only used to represent bits that are set.
  110. * Nodes with a mask of 0 and num_after of 0 are not allowed.
  111. *
  112. * + Sum of bits set in all the nodes is equal to the value of
  113. * the struct sparsebit_pvt num_set member.
  114. *
  115. * + The setting of at least one bit is always described in a nodes
  116. * mask (mask >= 1).
  117. *
  118. * + A node with all mask bits set only occurs when the last bit
  119. * described by the previous node is not equal to this nodes
  120. * starting index - 1. All such occurences of this condition are
  121. * avoided by moving the setting of the nodes mask bits into
  122. * the previous nodes num_after setting.
  123. *
  124. * + Node starting index is evenly divisible by the number of bits
  125. * within a nodes mask member.
  126. *
  127. * + Nodes never represent a range of bits that wrap around the
  128. * highest supported index.
  129. *
  130. * (idx + MASK_BITS + num_after - 1) <= ((sparsebit_idx_t) 0) - 1)
  131. *
  132. * As a consequence of the above, the num_after member of a node
  133. * will always be <=:
  134. *
  135. * maximum_index - nodes_starting_index - number_of_mask_bits
  136. *
  137. * + Nodes within the binary search tree are sorted based on each
  138. * nodes starting index.
  139. *
  140. * + The range of bits described by any two nodes do not overlap. The
  141. * range of bits described by a single node is:
  142. *
  143. * start: node->idx
  144. * end (inclusive): node->idx + MASK_BITS + node->num_after - 1;
  145. *
  146. * Note, at times these invariants are temporarily violated for a
  147. * specific portion of the code. For example, when setting a mask
  148. * bit, there is a small delay between when the mask bit is set and the
  149. * value in the struct sparsebit_pvt num_set member is updated. Other
  150. * temporary violations occur when node_split() is called with a specified
  151. * index and assures that a node where its mask represents the bit
  152. * at the specified index exists. At times to do this node_split()
  153. * must split an existing node into two nodes or create a node that
  154. * has no bits set. Such temporary violations must be corrected before
  155. * returning to the caller. These corrections are typically performed
  156. * by the local function node_reduce().
  157. */
  158. #include "test_util.h"
  159. #include "sparsebit.h"
  160. #include <limits.h>
  161. #include <assert.h>
  162. #define DUMP_LINE_MAX 100 /* Does not include indent amount */
  163. typedef uint32_t mask_t;
  164. #define MASK_BITS (sizeof(mask_t) * CHAR_BIT)
  165. struct node {
  166. struct node *parent;
  167. struct node *left;
  168. struct node *right;
  169. sparsebit_idx_t idx; /* index of least-significant bit in mask */
  170. sparsebit_num_t num_after; /* num contiguously set after mask */
  171. mask_t mask;
  172. };
  173. struct sparsebit {
  174. /*
  175. * Points to root node of the binary search
  176. * tree. Equal to NULL when no bits are set in
  177. * the entire sparsebit array.
  178. */
  179. struct node *root;
  180. /*
  181. * A redundant count of the total number of bits set. Used for
  182. * diagnostic purposes and to change the time complexity of
  183. * sparsebit_num_set() from O(n) to O(1).
  184. * Note: Due to overflow, a value of 0 means none or all set.
  185. */
  186. sparsebit_num_t num_set;
  187. };
  188. /* Returns the number of set bits described by the settings
  189. * of the node pointed to by nodep.
  190. */
  191. static sparsebit_num_t node_num_set(struct node *nodep)
  192. {
  193. return nodep->num_after + __builtin_popcount(nodep->mask);
  194. }
  195. /* Returns a pointer to the node that describes the
  196. * lowest bit index.
  197. */
  198. static struct node *node_first(struct sparsebit *s)
  199. {
  200. struct node *nodep;
  201. for (nodep = s->root; nodep && nodep->left; nodep = nodep->left)
  202. ;
  203. return nodep;
  204. }
  205. /* Returns a pointer to the node that describes the
  206. * lowest bit index > the index of the node pointed to by np.
  207. * Returns NULL if no node with a higher index exists.
  208. */
  209. static struct node *node_next(struct sparsebit *s, struct node *np)
  210. {
  211. struct node *nodep = np;
  212. /*
  213. * If current node has a right child, next node is the left-most
  214. * of the right child.
  215. */
  216. if (nodep->right) {
  217. for (nodep = nodep->right; nodep->left; nodep = nodep->left)
  218. ;
  219. return nodep;
  220. }
  221. /*
  222. * No right child. Go up until node is left child of a parent.
  223. * That parent is then the next node.
  224. */
  225. while (nodep->parent && nodep == nodep->parent->right)
  226. nodep = nodep->parent;
  227. return nodep->parent;
  228. }
  229. /* Searches for and returns a pointer to the node that describes the
  230. * highest index < the index of the node pointed to by np.
  231. * Returns NULL if no node with a lower index exists.
  232. */
  233. static struct node *node_prev(struct sparsebit *s, struct node *np)
  234. {
  235. struct node *nodep = np;
  236. /*
  237. * If current node has a left child, next node is the right-most
  238. * of the left child.
  239. */
  240. if (nodep->left) {
  241. for (nodep = nodep->left; nodep->right; nodep = nodep->right)
  242. ;
  243. return (struct node *) nodep;
  244. }
  245. /*
  246. * No left child. Go up until node is right child of a parent.
  247. * That parent is then the next node.
  248. */
  249. while (nodep->parent && nodep == nodep->parent->left)
  250. nodep = nodep->parent;
  251. return (struct node *) nodep->parent;
  252. }
  253. /* Allocates space to hold a copy of the node sub-tree pointed to by
  254. * subtree and duplicates the bit settings to the newly allocated nodes.
  255. * Returns the newly allocated copy of subtree.
  256. */
  257. static struct node *node_copy_subtree(struct node *subtree)
  258. {
  259. struct node *root;
  260. /* Duplicate the node at the root of the subtree */
  261. root = calloc(1, sizeof(*root));
  262. if (!root) {
  263. perror("calloc");
  264. abort();
  265. }
  266. root->idx = subtree->idx;
  267. root->mask = subtree->mask;
  268. root->num_after = subtree->num_after;
  269. /* As needed, recursively duplicate the left and right subtrees */
  270. if (subtree->left) {
  271. root->left = node_copy_subtree(subtree->left);
  272. root->left->parent = root;
  273. }
  274. if (subtree->right) {
  275. root->right = node_copy_subtree(subtree->right);
  276. root->right->parent = root;
  277. }
  278. return root;
  279. }
  280. /* Searches for and returns a pointer to the node that describes the setting
  281. * of the bit given by idx. A node describes the setting of a bit if its
  282. * index is within the bits described by the mask bits or the number of
  283. * contiguous bits set after the mask. Returns NULL if there is no such node.
  284. */
  285. static struct node *node_find(struct sparsebit *s, sparsebit_idx_t idx)
  286. {
  287. struct node *nodep;
  288. /* Find the node that describes the setting of the bit at idx */
  289. for (nodep = s->root; nodep;
  290. nodep = nodep->idx > idx ? nodep->left : nodep->right) {
  291. if (idx >= nodep->idx &&
  292. idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
  293. break;
  294. }
  295. return nodep;
  296. }
  297. /* Entry Requirements:
  298. * + A node that describes the setting of idx is not already present.
  299. *
  300. * Adds a new node to describe the setting of the bit at the index given
  301. * by idx. Returns a pointer to the newly added node.
  302. *
  303. * TODO(lhuemill): Degenerate cases causes the tree to get unbalanced.
  304. */
  305. static struct node *node_add(struct sparsebit *s, sparsebit_idx_t idx)
  306. {
  307. struct node *nodep, *parentp, *prev;
  308. /* Allocate and initialize the new node. */
  309. nodep = calloc(1, sizeof(*nodep));
  310. if (!nodep) {
  311. perror("calloc");
  312. abort();
  313. }
  314. nodep->idx = idx & -MASK_BITS;
  315. /* If no nodes, set it up as the root node. */
  316. if (!s->root) {
  317. s->root = nodep;
  318. return nodep;
  319. }
  320. /*
  321. * Find the parent where the new node should be attached
  322. * and add the node there.
  323. */
  324. parentp = s->root;
  325. while (true) {
  326. if (idx < parentp->idx) {
  327. if (!parentp->left) {
  328. parentp->left = nodep;
  329. nodep->parent = parentp;
  330. break;
  331. }
  332. parentp = parentp->left;
  333. } else {
  334. assert(idx > parentp->idx + MASK_BITS + parentp->num_after - 1);
  335. if (!parentp->right) {
  336. parentp->right = nodep;
  337. nodep->parent = parentp;
  338. break;
  339. }
  340. parentp = parentp->right;
  341. }
  342. }
  343. /*
  344. * Does num_after bits of previous node overlap with the mask
  345. * of the new node? If so set the bits in the new nodes mask
  346. * and reduce the previous nodes num_after.
  347. */
  348. prev = node_prev(s, nodep);
  349. while (prev && prev->idx + MASK_BITS + prev->num_after - 1 >= nodep->idx) {
  350. unsigned int n1 = (prev->idx + MASK_BITS + prev->num_after - 1)
  351. - nodep->idx;
  352. assert(prev->num_after > 0);
  353. assert(n1 < MASK_BITS);
  354. assert(!(nodep->mask & (1 << n1)));
  355. nodep->mask |= (1 << n1);
  356. prev->num_after--;
  357. }
  358. return nodep;
  359. }
  360. /* Returns whether all the bits in the sparsebit array are set. */
  361. bool sparsebit_all_set(struct sparsebit *s)
  362. {
  363. /*
  364. * If any nodes there must be at least one bit set. Only case
  365. * where a bit is set and total num set is 0, is when all bits
  366. * are set.
  367. */
  368. return s->root && s->num_set == 0;
  369. }
  370. /* Clears all bits described by the node pointed to by nodep, then
  371. * removes the node.
  372. */
  373. static void node_rm(struct sparsebit *s, struct node *nodep)
  374. {
  375. struct node *tmp;
  376. sparsebit_num_t num_set;
  377. num_set = node_num_set(nodep);
  378. assert(s->num_set >= num_set || sparsebit_all_set(s));
  379. s->num_set -= node_num_set(nodep);
  380. /* Have both left and right child */
  381. if (nodep->left && nodep->right) {
  382. /*
  383. * Move left children to the leftmost leaf node
  384. * of the right child.
  385. */
  386. for (tmp = nodep->right; tmp->left; tmp = tmp->left)
  387. ;
  388. tmp->left = nodep->left;
  389. nodep->left = NULL;
  390. tmp->left->parent = tmp;
  391. }
  392. /* Left only child */
  393. if (nodep->left) {
  394. if (!nodep->parent) {
  395. s->root = nodep->left;
  396. nodep->left->parent = NULL;
  397. } else {
  398. nodep->left->parent = nodep->parent;
  399. if (nodep == nodep->parent->left)
  400. nodep->parent->left = nodep->left;
  401. else {
  402. assert(nodep == nodep->parent->right);
  403. nodep->parent->right = nodep->left;
  404. }
  405. }
  406. nodep->parent = nodep->left = nodep->right = NULL;
  407. free(nodep);
  408. return;
  409. }
  410. /* Right only child */
  411. if (nodep->right) {
  412. if (!nodep->parent) {
  413. s->root = nodep->right;
  414. nodep->right->parent = NULL;
  415. } else {
  416. nodep->right->parent = nodep->parent;
  417. if (nodep == nodep->parent->left)
  418. nodep->parent->left = nodep->right;
  419. else {
  420. assert(nodep == nodep->parent->right);
  421. nodep->parent->right = nodep->right;
  422. }
  423. }
  424. nodep->parent = nodep->left = nodep->right = NULL;
  425. free(nodep);
  426. return;
  427. }
  428. /* Leaf Node */
  429. if (!nodep->parent) {
  430. s->root = NULL;
  431. } else {
  432. if (nodep->parent->left == nodep)
  433. nodep->parent->left = NULL;
  434. else {
  435. assert(nodep == nodep->parent->right);
  436. nodep->parent->right = NULL;
  437. }
  438. }
  439. nodep->parent = nodep->left = nodep->right = NULL;
  440. free(nodep);
  441. return;
  442. }
  443. /* Splits the node containing the bit at idx so that there is a node
  444. * that starts at the specified index. If no such node exists, a new
  445. * node at the specified index is created. Returns the new node.
  446. *
  447. * idx must start of a mask boundary.
  448. */
  449. static struct node *node_split(struct sparsebit *s, sparsebit_idx_t idx)
  450. {
  451. struct node *nodep1, *nodep2;
  452. sparsebit_idx_t offset;
  453. sparsebit_num_t orig_num_after;
  454. assert(!(idx % MASK_BITS));
  455. /*
  456. * Is there a node that describes the setting of idx?
  457. * If not, add it.
  458. */
  459. nodep1 = node_find(s, idx);
  460. if (!nodep1)
  461. return node_add(s, idx);
  462. /*
  463. * All done if the starting index of the node is where the
  464. * split should occur.
  465. */
  466. if (nodep1->idx == idx)
  467. return nodep1;
  468. /*
  469. * Split point not at start of mask, so it must be part of
  470. * bits described by num_after.
  471. */
  472. /*
  473. * Calculate offset within num_after for where the split is
  474. * to occur.
  475. */
  476. offset = idx - (nodep1->idx + MASK_BITS);
  477. orig_num_after = nodep1->num_after;
  478. /*
  479. * Add a new node to describe the bits starting at
  480. * the split point.
  481. */
  482. nodep1->num_after = offset;
  483. nodep2 = node_add(s, idx);
  484. /* Move bits after the split point into the new node */
  485. nodep2->num_after = orig_num_after - offset;
  486. if (nodep2->num_after >= MASK_BITS) {
  487. nodep2->mask = ~(mask_t) 0;
  488. nodep2->num_after -= MASK_BITS;
  489. } else {
  490. nodep2->mask = (1 << nodep2->num_after) - 1;
  491. nodep2->num_after = 0;
  492. }
  493. return nodep2;
  494. }
  495. /* Iteratively reduces the node pointed to by nodep and its adjacent
  496. * nodes into a more compact form. For example, a node with a mask with
  497. * all bits set adjacent to a previous node, will get combined into a
  498. * single node with an increased num_after setting.
  499. *
  500. * After each reduction, a further check is made to see if additional
  501. * reductions are possible with the new previous and next nodes. Note,
  502. * a search for a reduction is only done across the nodes nearest nodep
  503. * and those that became part of a reduction. Reductions beyond nodep
  504. * and the adjacent nodes that are reduced are not discovered. It is the
  505. * responsibility of the caller to pass a nodep that is within one node
  506. * of each possible reduction.
  507. *
  508. * This function does not fix the temporary violation of all invariants.
  509. * For example it does not fix the case where the bit settings described
  510. * by two or more nodes overlap. Such a violation introduces the potential
  511. * complication of a bit setting for a specific index having different settings
  512. * in different nodes. This would then introduce the further complication
  513. * of which node has the correct setting of the bit and thus such conditions
  514. * are not allowed.
  515. *
  516. * This function is designed to fix invariant violations that are introduced
  517. * by node_split() and by changes to the nodes mask or num_after members.
  518. * For example, when setting a bit within a nodes mask, the function that
  519. * sets the bit doesn't have to worry about whether the setting of that
  520. * bit caused the mask to have leading only or trailing only bits set.
  521. * Instead, the function can call node_reduce(), with nodep equal to the
  522. * node address that it set a mask bit in, and node_reduce() will notice
  523. * the cases of leading or trailing only bits and that there is an
  524. * adjacent node that the bit settings could be merged into.
  525. *
  526. * This implementation specifically detects and corrects violation of the
  527. * following invariants:
  528. *
  529. * + Node are only used to represent bits that are set.
  530. * Nodes with a mask of 0 and num_after of 0 are not allowed.
  531. *
  532. * + The setting of at least one bit is always described in a nodes
  533. * mask (mask >= 1).
  534. *
  535. * + A node with all mask bits set only occurs when the last bit
  536. * described by the previous node is not equal to this nodes
  537. * starting index - 1. All such occurences of this condition are
  538. * avoided by moving the setting of the nodes mask bits into
  539. * the previous nodes num_after setting.
  540. */
  541. static void node_reduce(struct sparsebit *s, struct node *nodep)
  542. {
  543. bool reduction_performed;
  544. do {
  545. reduction_performed = false;
  546. struct node *prev, *next, *tmp;
  547. /* 1) Potential reductions within the current node. */
  548. /* Nodes with all bits cleared may be removed. */
  549. if (nodep->mask == 0 && nodep->num_after == 0) {
  550. /*
  551. * About to remove the node pointed to by
  552. * nodep, which normally would cause a problem
  553. * for the next pass through the reduction loop,
  554. * because the node at the starting point no longer
  555. * exists. This potential problem is handled
  556. * by first remembering the location of the next
  557. * or previous nodes. Doesn't matter which, because
  558. * once the node at nodep is removed, there will be
  559. * no other nodes between prev and next.
  560. *
  561. * Note, the checks performed on nodep against both
  562. * both prev and next both check for an adjacent
  563. * node that can be reduced into a single node. As
  564. * such, after removing the node at nodep, doesn't
  565. * matter whether the nodep for the next pass
  566. * through the loop is equal to the previous pass
  567. * prev or next node. Either way, on the next pass
  568. * the one not selected will become either the
  569. * prev or next node.
  570. */
  571. tmp = node_next(s, nodep);
  572. if (!tmp)
  573. tmp = node_prev(s, nodep);
  574. node_rm(s, nodep);
  575. nodep = NULL;
  576. nodep = tmp;
  577. reduction_performed = true;
  578. continue;
  579. }
  580. /*
  581. * When the mask is 0, can reduce the amount of num_after
  582. * bits by moving the initial num_after bits into the mask.
  583. */
  584. if (nodep->mask == 0) {
  585. assert(nodep->num_after != 0);
  586. assert(nodep->idx + MASK_BITS > nodep->idx);
  587. nodep->idx += MASK_BITS;
  588. if (nodep->num_after >= MASK_BITS) {
  589. nodep->mask = ~0;
  590. nodep->num_after -= MASK_BITS;
  591. } else {
  592. nodep->mask = (1u << nodep->num_after) - 1;
  593. nodep->num_after = 0;
  594. }
  595. reduction_performed = true;
  596. continue;
  597. }
  598. /*
  599. * 2) Potential reductions between the current and
  600. * previous nodes.
  601. */
  602. prev = node_prev(s, nodep);
  603. if (prev) {
  604. sparsebit_idx_t prev_highest_bit;
  605. /* Nodes with no bits set can be removed. */
  606. if (prev->mask == 0 && prev->num_after == 0) {
  607. node_rm(s, prev);
  608. reduction_performed = true;
  609. continue;
  610. }
  611. /*
  612. * All mask bits set and previous node has
  613. * adjacent index.
  614. */
  615. if (nodep->mask + 1 == 0 &&
  616. prev->idx + MASK_BITS == nodep->idx) {
  617. prev->num_after += MASK_BITS + nodep->num_after;
  618. nodep->mask = 0;
  619. nodep->num_after = 0;
  620. reduction_performed = true;
  621. continue;
  622. }
  623. /*
  624. * Is node adjacent to previous node and the node
  625. * contains a single contiguous range of bits
  626. * starting from the beginning of the mask?
  627. */
  628. prev_highest_bit = prev->idx + MASK_BITS - 1 + prev->num_after;
  629. if (prev_highest_bit + 1 == nodep->idx &&
  630. (nodep->mask | (nodep->mask >> 1)) == nodep->mask) {
  631. /*
  632. * How many contiguous bits are there?
  633. * Is equal to the total number of set
  634. * bits, due to an earlier check that
  635. * there is a single contiguous range of
  636. * set bits.
  637. */
  638. unsigned int num_contiguous
  639. = __builtin_popcount(nodep->mask);
  640. assert((num_contiguous > 0) &&
  641. ((1ULL << num_contiguous) - 1) == nodep->mask);
  642. prev->num_after += num_contiguous;
  643. nodep->mask = 0;
  644. /*
  645. * For predictable performance, handle special
  646. * case where all mask bits are set and there
  647. * is a non-zero num_after setting. This code
  648. * is functionally correct without the following
  649. * conditionalized statements, but without them
  650. * the value of num_after is only reduced by
  651. * the number of mask bits per pass. There are
  652. * cases where num_after can be close to 2^64.
  653. * Without this code it could take nearly
  654. * (2^64) / 32 passes to perform the full
  655. * reduction.
  656. */
  657. if (num_contiguous == MASK_BITS) {
  658. prev->num_after += nodep->num_after;
  659. nodep->num_after = 0;
  660. }
  661. reduction_performed = true;
  662. continue;
  663. }
  664. }
  665. /*
  666. * 3) Potential reductions between the current and
  667. * next nodes.
  668. */
  669. next = node_next(s, nodep);
  670. if (next) {
  671. /* Nodes with no bits set can be removed. */
  672. if (next->mask == 0 && next->num_after == 0) {
  673. node_rm(s, next);
  674. reduction_performed = true;
  675. continue;
  676. }
  677. /*
  678. * Is next node index adjacent to current node
  679. * and has a mask with all bits set?
  680. */
  681. if (next->idx == nodep->idx + MASK_BITS + nodep->num_after &&
  682. next->mask == ~(mask_t) 0) {
  683. nodep->num_after += MASK_BITS;
  684. next->mask = 0;
  685. nodep->num_after += next->num_after;
  686. next->num_after = 0;
  687. node_rm(s, next);
  688. next = NULL;
  689. reduction_performed = true;
  690. continue;
  691. }
  692. }
  693. } while (nodep && reduction_performed);
  694. }
  695. /* Returns whether the bit at the index given by idx, within the
  696. * sparsebit array is set or not.
  697. */
  698. bool sparsebit_is_set(struct sparsebit *s, sparsebit_idx_t idx)
  699. {
  700. struct node *nodep;
  701. /* Find the node that describes the setting of the bit at idx */
  702. for (nodep = s->root; nodep;
  703. nodep = nodep->idx > idx ? nodep->left : nodep->right)
  704. if (idx >= nodep->idx &&
  705. idx <= nodep->idx + MASK_BITS + nodep->num_after - 1)
  706. goto have_node;
  707. return false;
  708. have_node:
  709. /* Bit is set if it is any of the bits described by num_after */
  710. if (nodep->num_after && idx >= nodep->idx + MASK_BITS)
  711. return true;
  712. /* Is the corresponding mask bit set */
  713. assert(idx >= nodep->idx && idx - nodep->idx < MASK_BITS);
  714. return !!(nodep->mask & (1 << (idx - nodep->idx)));
  715. }
  716. /* Within the sparsebit array pointed to by s, sets the bit
  717. * at the index given by idx.
  718. */
  719. static void bit_set(struct sparsebit *s, sparsebit_idx_t idx)
  720. {
  721. struct node *nodep;
  722. /* Skip bits that are already set */
  723. if (sparsebit_is_set(s, idx))
  724. return;
  725. /*
  726. * Get a node where the bit at idx is described by the mask.
  727. * The node_split will also create a node, if there isn't
  728. * already a node that describes the setting of bit.
  729. */
  730. nodep = node_split(s, idx & -MASK_BITS);
  731. /* Set the bit within the nodes mask */
  732. assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
  733. assert(!(nodep->mask & (1 << (idx - nodep->idx))));
  734. nodep->mask |= 1 << (idx - nodep->idx);
  735. s->num_set++;
  736. node_reduce(s, nodep);
  737. }
  738. /* Within the sparsebit array pointed to by s, clears the bit
  739. * at the index given by idx.
  740. */
  741. static void bit_clear(struct sparsebit *s, sparsebit_idx_t idx)
  742. {
  743. struct node *nodep;
  744. /* Skip bits that are already cleared */
  745. if (!sparsebit_is_set(s, idx))
  746. return;
  747. /* Is there a node that describes the setting of this bit? */
  748. nodep = node_find(s, idx);
  749. if (!nodep)
  750. return;
  751. /*
  752. * If a num_after bit, split the node, so that the bit is
  753. * part of a node mask.
  754. */
  755. if (idx >= nodep->idx + MASK_BITS)
  756. nodep = node_split(s, idx & -MASK_BITS);
  757. /*
  758. * After node_split above, bit at idx should be within the mask.
  759. * Clear that bit.
  760. */
  761. assert(idx >= nodep->idx && idx <= nodep->idx + MASK_BITS - 1);
  762. assert(nodep->mask & (1 << (idx - nodep->idx)));
  763. nodep->mask &= ~(1 << (idx - nodep->idx));
  764. assert(s->num_set > 0 || sparsebit_all_set(s));
  765. s->num_set--;
  766. node_reduce(s, nodep);
  767. }
  768. /* Recursively dumps to the FILE stream given by stream the contents
  769. * of the sub-tree of nodes pointed to by nodep. Each line of output
  770. * is prefixed by the number of spaces given by indent. On each
  771. * recursion, the indent amount is increased by 2. This causes nodes
  772. * at each level deeper into the binary search tree to be displayed
  773. * with a greater indent.
  774. */
  775. static void dump_nodes(FILE *stream, struct node *nodep,
  776. unsigned int indent)
  777. {
  778. char *node_type;
  779. /* Dump contents of node */
  780. if (!nodep->parent)
  781. node_type = "root";
  782. else if (nodep == nodep->parent->left)
  783. node_type = "left";
  784. else {
  785. assert(nodep == nodep->parent->right);
  786. node_type = "right";
  787. }
  788. fprintf(stream, "%*s---- %s nodep: %p\n", indent, "", node_type, nodep);
  789. fprintf(stream, "%*s parent: %p left: %p right: %p\n", indent, "",
  790. nodep->parent, nodep->left, nodep->right);
  791. fprintf(stream, "%*s idx: 0x%lx mask: 0x%x num_after: 0x%lx\n",
  792. indent, "", nodep->idx, nodep->mask, nodep->num_after);
  793. /* If present, dump contents of left child nodes */
  794. if (nodep->left)
  795. dump_nodes(stream, nodep->left, indent + 2);
  796. /* If present, dump contents of right child nodes */
  797. if (nodep->right)
  798. dump_nodes(stream, nodep->right, indent + 2);
  799. }
  800. static inline sparsebit_idx_t node_first_set(struct node *nodep, int start)
  801. {
  802. mask_t leading = (mask_t)1 << start;
  803. int n1 = __builtin_ctz(nodep->mask & -leading);
  804. return nodep->idx + n1;
  805. }
  806. static inline sparsebit_idx_t node_first_clear(struct node *nodep, int start)
  807. {
  808. mask_t leading = (mask_t)1 << start;
  809. int n1 = __builtin_ctz(~nodep->mask & -leading);
  810. return nodep->idx + n1;
  811. }
  812. /* Dumps to the FILE stream specified by stream, the implementation dependent
  813. * internal state of s. Each line of output is prefixed with the number
  814. * of spaces given by indent. The output is completely implementation
  815. * dependent and subject to change. Output from this function should only
  816. * be used for diagnostic purposes. For example, this function can be
  817. * used by test cases after they detect an unexpected condition, as a means
  818. * to capture diagnostic information.
  819. */
  820. static void sparsebit_dump_internal(FILE *stream, struct sparsebit *s,
  821. unsigned int indent)
  822. {
  823. /* Dump the contents of s */
  824. fprintf(stream, "%*sroot: %p\n", indent, "", s->root);
  825. fprintf(stream, "%*snum_set: 0x%lx\n", indent, "", s->num_set);
  826. if (s->root)
  827. dump_nodes(stream, s->root, indent);
  828. }
  829. /* Allocates and returns a new sparsebit array. The initial state
  830. * of the newly allocated sparsebit array has all bits cleared.
  831. */
  832. struct sparsebit *sparsebit_alloc(void)
  833. {
  834. struct sparsebit *s;
  835. /* Allocate top level structure. */
  836. s = calloc(1, sizeof(*s));
  837. if (!s) {
  838. perror("calloc");
  839. abort();
  840. }
  841. return s;
  842. }
  843. /* Frees the implementation dependent data for the sparsebit array
  844. * pointed to by s and poisons the pointer to that data.
  845. */
  846. void sparsebit_free(struct sparsebit **sbitp)
  847. {
  848. struct sparsebit *s = *sbitp;
  849. if (!s)
  850. return;
  851. sparsebit_clear_all(s);
  852. free(s);
  853. *sbitp = NULL;
  854. }
  855. /* Makes a copy of the sparsebit array given by s, to the sparsebit
  856. * array given by d. Note, d must have already been allocated via
  857. * sparsebit_alloc(). It can though already have bits set, which
  858. * if different from src will be cleared.
  859. */
  860. void sparsebit_copy(struct sparsebit *d, struct sparsebit *s)
  861. {
  862. /* First clear any bits already set in the destination */
  863. sparsebit_clear_all(d);
  864. if (s->root) {
  865. d->root = node_copy_subtree(s->root);
  866. d->num_set = s->num_set;
  867. }
  868. }
  869. /* Returns whether num consecutive bits starting at idx are all set. */
  870. bool sparsebit_is_set_num(struct sparsebit *s,
  871. sparsebit_idx_t idx, sparsebit_num_t num)
  872. {
  873. sparsebit_idx_t next_cleared;
  874. assert(num > 0);
  875. assert(idx + num - 1 >= idx);
  876. /* With num > 0, the first bit must be set. */
  877. if (!sparsebit_is_set(s, idx))
  878. return false;
  879. /* Find the next cleared bit */
  880. next_cleared = sparsebit_next_clear(s, idx);
  881. /*
  882. * If no cleared bits beyond idx, then there are at least num
  883. * set bits. idx + num doesn't wrap. Otherwise check if
  884. * there are enough set bits between idx and the next cleared bit.
  885. */
  886. return next_cleared == 0 || next_cleared - idx >= num;
  887. }
  888. /* Returns whether the bit at the index given by idx. */
  889. bool sparsebit_is_clear(struct sparsebit *s,
  890. sparsebit_idx_t idx)
  891. {
  892. return !sparsebit_is_set(s, idx);
  893. }
  894. /* Returns whether num consecutive bits starting at idx are all cleared. */
  895. bool sparsebit_is_clear_num(struct sparsebit *s,
  896. sparsebit_idx_t idx, sparsebit_num_t num)
  897. {
  898. sparsebit_idx_t next_set;
  899. assert(num > 0);
  900. assert(idx + num - 1 >= idx);
  901. /* With num > 0, the first bit must be cleared. */
  902. if (!sparsebit_is_clear(s, idx))
  903. return false;
  904. /* Find the next set bit */
  905. next_set = sparsebit_next_set(s, idx);
  906. /*
  907. * If no set bits beyond idx, then there are at least num
  908. * cleared bits. idx + num doesn't wrap. Otherwise check if
  909. * there are enough cleared bits between idx and the next set bit.
  910. */
  911. return next_set == 0 || next_set - idx >= num;
  912. }
  913. /* Returns the total number of bits set. Note: 0 is also returned for
  914. * the case of all bits set. This is because with all bits set, there
  915. * is 1 additional bit set beyond what can be represented in the return
  916. * value. Use sparsebit_any_set(), instead of sparsebit_num_set() > 0,
  917. * to determine if the sparsebit array has any bits set.
  918. */
  919. sparsebit_num_t sparsebit_num_set(struct sparsebit *s)
  920. {
  921. return s->num_set;
  922. }
  923. /* Returns whether any bit is set in the sparsebit array. */
  924. bool sparsebit_any_set(struct sparsebit *s)
  925. {
  926. /*
  927. * Nodes only describe set bits. If any nodes then there
  928. * is at least 1 bit set.
  929. */
  930. if (!s->root)
  931. return false;
  932. /*
  933. * Every node should have a non-zero mask. For now will
  934. * just assure that the root node has a non-zero mask,
  935. * which is a quick check that at least 1 bit is set.
  936. */
  937. assert(s->root->mask != 0);
  938. assert(s->num_set > 0 ||
  939. (s->root->num_after == ((sparsebit_num_t) 0) - MASK_BITS &&
  940. s->root->mask == ~(mask_t) 0));
  941. return true;
  942. }
  943. /* Returns whether all the bits in the sparsebit array are cleared. */
  944. bool sparsebit_all_clear(struct sparsebit *s)
  945. {
  946. return !sparsebit_any_set(s);
  947. }
  948. /* Returns whether all the bits in the sparsebit array are set. */
  949. bool sparsebit_any_clear(struct sparsebit *s)
  950. {
  951. return !sparsebit_all_set(s);
  952. }
  953. /* Returns the index of the first set bit. Abort if no bits are set.
  954. */
  955. sparsebit_idx_t sparsebit_first_set(struct sparsebit *s)
  956. {
  957. struct node *nodep;
  958. /* Validate at least 1 bit is set */
  959. assert(sparsebit_any_set(s));
  960. nodep = node_first(s);
  961. return node_first_set(nodep, 0);
  962. }
  963. /* Returns the index of the first cleared bit. Abort if
  964. * no bits are cleared.
  965. */
  966. sparsebit_idx_t sparsebit_first_clear(struct sparsebit *s)
  967. {
  968. struct node *nodep1, *nodep2;
  969. /* Validate at least 1 bit is cleared. */
  970. assert(sparsebit_any_clear(s));
  971. /* If no nodes or first node index > 0 then lowest cleared is 0 */
  972. nodep1 = node_first(s);
  973. if (!nodep1 || nodep1->idx > 0)
  974. return 0;
  975. /* Does the mask in the first node contain any cleared bits. */
  976. if (nodep1->mask != ~(mask_t) 0)
  977. return node_first_clear(nodep1, 0);
  978. /*
  979. * All mask bits set in first node. If there isn't a second node
  980. * then the first cleared bit is the first bit after the bits
  981. * described by the first node.
  982. */
  983. nodep2 = node_next(s, nodep1);
  984. if (!nodep2) {
  985. /*
  986. * No second node. First cleared bit is first bit beyond
  987. * bits described by first node.
  988. */
  989. assert(nodep1->mask == ~(mask_t) 0);
  990. assert(nodep1->idx + MASK_BITS + nodep1->num_after != (sparsebit_idx_t) 0);
  991. return nodep1->idx + MASK_BITS + nodep1->num_after;
  992. }
  993. /*
  994. * There is a second node.
  995. * If it is not adjacent to the first node, then there is a gap
  996. * of cleared bits between the nodes, and the first cleared bit
  997. * is the first bit within the gap.
  998. */
  999. if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
  1000. return nodep1->idx + MASK_BITS + nodep1->num_after;
  1001. /*
  1002. * Second node is adjacent to the first node.
  1003. * Because it is adjacent, its mask should be non-zero. If all
  1004. * its mask bits are set, then with it being adjacent, it should
  1005. * have had the mask bits moved into the num_after setting of the
  1006. * previous node.
  1007. */
  1008. return node_first_clear(nodep2, 0);
  1009. }
  1010. /* Returns index of next bit set within s after the index given by prev.
  1011. * Returns 0 if there are no bits after prev that are set.
  1012. */
  1013. sparsebit_idx_t sparsebit_next_set(struct sparsebit *s,
  1014. sparsebit_idx_t prev)
  1015. {
  1016. sparsebit_idx_t lowest_possible = prev + 1;
  1017. sparsebit_idx_t start;
  1018. struct node *nodep;
  1019. /* A bit after the highest index can't be set. */
  1020. if (lowest_possible == 0)
  1021. return 0;
  1022. /*
  1023. * Find the leftmost 'candidate' overlapping or to the right
  1024. * of lowest_possible.
  1025. */
  1026. struct node *candidate = NULL;
  1027. /* True iff lowest_possible is within candidate */
  1028. bool contains = false;
  1029. /*
  1030. * Find node that describes setting of bit at lowest_possible.
  1031. * If such a node doesn't exist, find the node with the lowest
  1032. * starting index that is > lowest_possible.
  1033. */
  1034. for (nodep = s->root; nodep;) {
  1035. if ((nodep->idx + MASK_BITS + nodep->num_after - 1)
  1036. >= lowest_possible) {
  1037. candidate = nodep;
  1038. if (candidate->idx <= lowest_possible) {
  1039. contains = true;
  1040. break;
  1041. }
  1042. nodep = nodep->left;
  1043. } else {
  1044. nodep = nodep->right;
  1045. }
  1046. }
  1047. if (!candidate)
  1048. return 0;
  1049. assert(candidate->mask != 0);
  1050. /* Does the candidate node describe the setting of lowest_possible? */
  1051. if (!contains) {
  1052. /*
  1053. * Candidate doesn't describe setting of bit at lowest_possible.
  1054. * Candidate points to the first node with a starting index
  1055. * > lowest_possible.
  1056. */
  1057. assert(candidate->idx > lowest_possible);
  1058. return node_first_set(candidate, 0);
  1059. }
  1060. /*
  1061. * Candidate describes setting of bit at lowest_possible.
  1062. * Note: although the node describes the setting of the bit
  1063. * at lowest_possible, its possible that its setting and the
  1064. * setting of all latter bits described by this node are 0.
  1065. * For now, just handle the cases where this node describes
  1066. * a bit at or after an index of lowest_possible that is set.
  1067. */
  1068. start = lowest_possible - candidate->idx;
  1069. if (start < MASK_BITS && candidate->mask >= (1 << start))
  1070. return node_first_set(candidate, start);
  1071. if (candidate->num_after) {
  1072. sparsebit_idx_t first_num_after_idx = candidate->idx + MASK_BITS;
  1073. return lowest_possible < first_num_after_idx
  1074. ? first_num_after_idx : lowest_possible;
  1075. }
  1076. /*
  1077. * Although candidate node describes setting of bit at
  1078. * the index of lowest_possible, all bits at that index and
  1079. * latter that are described by candidate are cleared. With
  1080. * this, the next bit is the first bit in the next node, if
  1081. * such a node exists. If a next node doesn't exist, then
  1082. * there is no next set bit.
  1083. */
  1084. candidate = node_next(s, candidate);
  1085. if (!candidate)
  1086. return 0;
  1087. return node_first_set(candidate, 0);
  1088. }
  1089. /* Returns index of next bit cleared within s after the index given by prev.
  1090. * Returns 0 if there are no bits after prev that are cleared.
  1091. */
  1092. sparsebit_idx_t sparsebit_next_clear(struct sparsebit *s,
  1093. sparsebit_idx_t prev)
  1094. {
  1095. sparsebit_idx_t lowest_possible = prev + 1;
  1096. sparsebit_idx_t idx;
  1097. struct node *nodep1, *nodep2;
  1098. /* A bit after the highest index can't be set. */
  1099. if (lowest_possible == 0)
  1100. return 0;
  1101. /*
  1102. * Does a node describing the setting of lowest_possible exist?
  1103. * If not, the bit at lowest_possible is cleared.
  1104. */
  1105. nodep1 = node_find(s, lowest_possible);
  1106. if (!nodep1)
  1107. return lowest_possible;
  1108. /* Does a mask bit in node 1 describe the next cleared bit. */
  1109. for (idx = lowest_possible - nodep1->idx; idx < MASK_BITS; idx++)
  1110. if (!(nodep1->mask & (1 << idx)))
  1111. return nodep1->idx + idx;
  1112. /*
  1113. * Next cleared bit is not described by node 1. If there
  1114. * isn't a next node, then next cleared bit is described
  1115. * by bit after the bits described by the first node.
  1116. */
  1117. nodep2 = node_next(s, nodep1);
  1118. if (!nodep2)
  1119. return nodep1->idx + MASK_BITS + nodep1->num_after;
  1120. /*
  1121. * There is a second node.
  1122. * If it is not adjacent to the first node, then there is a gap
  1123. * of cleared bits between the nodes, and the next cleared bit
  1124. * is the first bit within the gap.
  1125. */
  1126. if (nodep1->idx + MASK_BITS + nodep1->num_after != nodep2->idx)
  1127. return nodep1->idx + MASK_BITS + nodep1->num_after;
  1128. /*
  1129. * Second node is adjacent to the first node.
  1130. * Because it is adjacent, its mask should be non-zero. If all
  1131. * its mask bits are set, then with it being adjacent, it should
  1132. * have had the mask bits moved into the num_after setting of the
  1133. * previous node.
  1134. */
  1135. return node_first_clear(nodep2, 0);
  1136. }
  1137. /* Starting with the index 1 greater than the index given by start, finds
  1138. * and returns the index of the first sequence of num consecutively set
  1139. * bits. Returns a value of 0 of no such sequence exists.
  1140. */
  1141. sparsebit_idx_t sparsebit_next_set_num(struct sparsebit *s,
  1142. sparsebit_idx_t start, sparsebit_num_t num)
  1143. {
  1144. sparsebit_idx_t idx;
  1145. assert(num >= 1);
  1146. for (idx = sparsebit_next_set(s, start);
  1147. idx != 0 && idx + num - 1 >= idx;
  1148. idx = sparsebit_next_set(s, idx)) {
  1149. assert(sparsebit_is_set(s, idx));
  1150. /*
  1151. * Does the sequence of bits starting at idx consist of
  1152. * num set bits?
  1153. */
  1154. if (sparsebit_is_set_num(s, idx, num))
  1155. return idx;
  1156. /*
  1157. * Sequence of set bits at idx isn't large enough.
  1158. * Skip this entire sequence of set bits.
  1159. */
  1160. idx = sparsebit_next_clear(s, idx);
  1161. if (idx == 0)
  1162. return 0;
  1163. }
  1164. return 0;
  1165. }
  1166. /* Starting with the index 1 greater than the index given by start, finds
  1167. * and returns the index of the first sequence of num consecutively cleared
  1168. * bits. Returns a value of 0 of no such sequence exists.
  1169. */
  1170. sparsebit_idx_t sparsebit_next_clear_num(struct sparsebit *s,
  1171. sparsebit_idx_t start, sparsebit_num_t num)
  1172. {
  1173. sparsebit_idx_t idx;
  1174. assert(num >= 1);
  1175. for (idx = sparsebit_next_clear(s, start);
  1176. idx != 0 && idx + num - 1 >= idx;
  1177. idx = sparsebit_next_clear(s, idx)) {
  1178. assert(sparsebit_is_clear(s, idx));
  1179. /*
  1180. * Does the sequence of bits starting at idx consist of
  1181. * num cleared bits?
  1182. */
  1183. if (sparsebit_is_clear_num(s, idx, num))
  1184. return idx;
  1185. /*
  1186. * Sequence of cleared bits at idx isn't large enough.
  1187. * Skip this entire sequence of cleared bits.
  1188. */
  1189. idx = sparsebit_next_set(s, idx);
  1190. if (idx == 0)
  1191. return 0;
  1192. }
  1193. return 0;
  1194. }
  1195. /* Sets the bits * in the inclusive range idx through idx + num - 1. */
  1196. void sparsebit_set_num(struct sparsebit *s,
  1197. sparsebit_idx_t start, sparsebit_num_t num)
  1198. {
  1199. struct node *nodep, *next;
  1200. unsigned int n1;
  1201. sparsebit_idx_t idx;
  1202. sparsebit_num_t n;
  1203. sparsebit_idx_t middle_start, middle_end;
  1204. assert(num > 0);
  1205. assert(start + num - 1 >= start);
  1206. /*
  1207. * Leading - bits before first mask boundary.
  1208. *
  1209. * TODO(lhuemill): With some effort it may be possible to
  1210. * replace the following loop with a sequential sequence
  1211. * of statements. High level sequence would be:
  1212. *
  1213. * 1. Use node_split() to force node that describes setting
  1214. * of idx to be within the mask portion of a node.
  1215. * 2. Form mask of bits to be set.
  1216. * 3. Determine number of mask bits already set in the node
  1217. * and store in a local variable named num_already_set.
  1218. * 4. Set the appropriate mask bits within the node.
  1219. * 5. Increment struct sparsebit_pvt num_set member
  1220. * by the number of bits that were actually set.
  1221. * Exclude from the counts bits that were already set.
  1222. * 6. Before returning to the caller, use node_reduce() to
  1223. * handle the multiple corner cases that this method
  1224. * introduces.
  1225. */
  1226. for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
  1227. bit_set(s, idx);
  1228. /* Middle - bits spanning one or more entire mask */
  1229. middle_start = idx;
  1230. middle_end = middle_start + (n & -MASK_BITS) - 1;
  1231. if (n >= MASK_BITS) {
  1232. nodep = node_split(s, middle_start);
  1233. /*
  1234. * As needed, split just after end of middle bits.
  1235. * No split needed if end of middle bits is at highest
  1236. * supported bit index.
  1237. */
  1238. if (middle_end + 1 > middle_end)
  1239. (void) node_split(s, middle_end + 1);
  1240. /* Delete nodes that only describe bits within the middle. */
  1241. for (next = node_next(s, nodep);
  1242. next && (next->idx < middle_end);
  1243. next = node_next(s, nodep)) {
  1244. assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
  1245. node_rm(s, next);
  1246. next = NULL;
  1247. }
  1248. /* As needed set each of the mask bits */
  1249. for (n1 = 0; n1 < MASK_BITS; n1++) {
  1250. if (!(nodep->mask & (1 << n1))) {
  1251. nodep->mask |= 1 << n1;
  1252. s->num_set++;
  1253. }
  1254. }
  1255. s->num_set -= nodep->num_after;
  1256. nodep->num_after = middle_end - middle_start + 1 - MASK_BITS;
  1257. s->num_set += nodep->num_after;
  1258. node_reduce(s, nodep);
  1259. }
  1260. idx = middle_end + 1;
  1261. n -= middle_end - middle_start + 1;
  1262. /* Trailing - bits at and beyond last mask boundary */
  1263. assert(n < MASK_BITS);
  1264. for (; n > 0; idx++, n--)
  1265. bit_set(s, idx);
  1266. }
  1267. /* Clears the bits * in the inclusive range idx through idx + num - 1. */
  1268. void sparsebit_clear_num(struct sparsebit *s,
  1269. sparsebit_idx_t start, sparsebit_num_t num)
  1270. {
  1271. struct node *nodep, *next;
  1272. unsigned int n1;
  1273. sparsebit_idx_t idx;
  1274. sparsebit_num_t n;
  1275. sparsebit_idx_t middle_start, middle_end;
  1276. assert(num > 0);
  1277. assert(start + num - 1 >= start);
  1278. /* Leading - bits before first mask boundary */
  1279. for (idx = start, n = num; n > 0 && idx % MASK_BITS != 0; idx++, n--)
  1280. bit_clear(s, idx);
  1281. /* Middle - bits spanning one or more entire mask */
  1282. middle_start = idx;
  1283. middle_end = middle_start + (n & -MASK_BITS) - 1;
  1284. if (n >= MASK_BITS) {
  1285. nodep = node_split(s, middle_start);
  1286. /*
  1287. * As needed, split just after end of middle bits.
  1288. * No split needed if end of middle bits is at highest
  1289. * supported bit index.
  1290. */
  1291. if (middle_end + 1 > middle_end)
  1292. (void) node_split(s, middle_end + 1);
  1293. /* Delete nodes that only describe bits within the middle. */
  1294. for (next = node_next(s, nodep);
  1295. next && (next->idx < middle_end);
  1296. next = node_next(s, nodep)) {
  1297. assert(next->idx + MASK_BITS + next->num_after - 1 <= middle_end);
  1298. node_rm(s, next);
  1299. next = NULL;
  1300. }
  1301. /* As needed clear each of the mask bits */
  1302. for (n1 = 0; n1 < MASK_BITS; n1++) {
  1303. if (nodep->mask & (1 << n1)) {
  1304. nodep->mask &= ~(1 << n1);
  1305. s->num_set--;
  1306. }
  1307. }
  1308. /* Clear any bits described by num_after */
  1309. s->num_set -= nodep->num_after;
  1310. nodep->num_after = 0;
  1311. /*
  1312. * Delete the node that describes the beginning of
  1313. * the middle bits and perform any allowed reductions
  1314. * with the nodes prev or next of nodep.
  1315. */
  1316. node_reduce(s, nodep);
  1317. nodep = NULL;
  1318. }
  1319. idx = middle_end + 1;
  1320. n -= middle_end - middle_start + 1;
  1321. /* Trailing - bits at and beyond last mask boundary */
  1322. assert(n < MASK_BITS);
  1323. for (; n > 0; idx++, n--)
  1324. bit_clear(s, idx);
  1325. }
  1326. /* Sets the bit at the index given by idx. */
  1327. void sparsebit_set(struct sparsebit *s, sparsebit_idx_t idx)
  1328. {
  1329. sparsebit_set_num(s, idx, 1);
  1330. }
  1331. /* Clears the bit at the index given by idx. */
  1332. void sparsebit_clear(struct sparsebit *s, sparsebit_idx_t idx)
  1333. {
  1334. sparsebit_clear_num(s, idx, 1);
  1335. }
  1336. /* Sets the bits in the entire addressable range of the sparsebit array. */
  1337. void sparsebit_set_all(struct sparsebit *s)
  1338. {
  1339. sparsebit_set(s, 0);
  1340. sparsebit_set_num(s, 1, ~(sparsebit_idx_t) 0);
  1341. assert(sparsebit_all_set(s));
  1342. }
  1343. /* Clears the bits in the entire addressable range of the sparsebit array. */
  1344. void sparsebit_clear_all(struct sparsebit *s)
  1345. {
  1346. sparsebit_clear(s, 0);
  1347. sparsebit_clear_num(s, 1, ~(sparsebit_idx_t) 0);
  1348. assert(!sparsebit_any_set(s));
  1349. }
  1350. static size_t display_range(FILE *stream, sparsebit_idx_t low,
  1351. sparsebit_idx_t high, bool prepend_comma_space)
  1352. {
  1353. char *fmt_str;
  1354. size_t sz;
  1355. /* Determine the printf format string */
  1356. if (low == high)
  1357. fmt_str = prepend_comma_space ? ", 0x%lx" : "0x%lx";
  1358. else
  1359. fmt_str = prepend_comma_space ? ", 0x%lx:0x%lx" : "0x%lx:0x%lx";
  1360. /*
  1361. * When stream is NULL, just determine the size of what would
  1362. * have been printed, else print the range.
  1363. */
  1364. if (!stream)
  1365. sz = snprintf(NULL, 0, fmt_str, low, high);
  1366. else
  1367. sz = fprintf(stream, fmt_str, low, high);
  1368. return sz;
  1369. }
  1370. /* Dumps to the FILE stream given by stream, the bit settings
  1371. * of s. Each line of output is prefixed with the number of
  1372. * spaces given by indent. The length of each line is implementation
  1373. * dependent and does not depend on the indent amount. The following
  1374. * is an example output of a sparsebit array that has bits:
  1375. *
  1376. * 0x5, 0x8, 0xa:0xe, 0x12
  1377. *
  1378. * This corresponds to a sparsebit whose bits 5, 8, 10, 11, 12, 13, 14, 18
  1379. * are set. Note that a ':', instead of a '-' is used to specify a range of
  1380. * contiguous bits. This is done because '-' is used to specify command-line
  1381. * options, and sometimes ranges are specified as command-line arguments.
  1382. */
  1383. void sparsebit_dump(FILE *stream, struct sparsebit *s,
  1384. unsigned int indent)
  1385. {
  1386. size_t current_line_len = 0;
  1387. size_t sz;
  1388. struct node *nodep;
  1389. if (!sparsebit_any_set(s))
  1390. return;
  1391. /* Display initial indent */
  1392. fprintf(stream, "%*s", indent, "");
  1393. /* For each node */
  1394. for (nodep = node_first(s); nodep; nodep = node_next(s, nodep)) {
  1395. unsigned int n1;
  1396. sparsebit_idx_t low, high;
  1397. /* For each group of bits in the mask */
  1398. for (n1 = 0; n1 < MASK_BITS; n1++) {
  1399. if (nodep->mask & (1 << n1)) {
  1400. low = high = nodep->idx + n1;
  1401. for (; n1 < MASK_BITS; n1++) {
  1402. if (nodep->mask & (1 << n1))
  1403. high = nodep->idx + n1;
  1404. else
  1405. break;
  1406. }
  1407. if ((n1 == MASK_BITS) && nodep->num_after)
  1408. high += nodep->num_after;
  1409. /*
  1410. * How much room will it take to display
  1411. * this range.
  1412. */
  1413. sz = display_range(NULL, low, high,
  1414. current_line_len != 0);
  1415. /*
  1416. * If there is not enough room, display
  1417. * a newline plus the indent of the next
  1418. * line.
  1419. */
  1420. if (current_line_len + sz > DUMP_LINE_MAX) {
  1421. fputs("\n", stream);
  1422. fprintf(stream, "%*s", indent, "");
  1423. current_line_len = 0;
  1424. }
  1425. /* Display the range */
  1426. sz = display_range(stream, low, high,
  1427. current_line_len != 0);
  1428. current_line_len += sz;
  1429. }
  1430. }
  1431. /*
  1432. * If num_after and most significant-bit of mask is not
  1433. * set, then still need to display a range for the bits
  1434. * described by num_after.
  1435. */
  1436. if (!(nodep->mask & (1 << (MASK_BITS - 1))) && nodep->num_after) {
  1437. low = nodep->idx + MASK_BITS;
  1438. high = nodep->idx + MASK_BITS + nodep->num_after - 1;
  1439. /*
  1440. * How much room will it take to display
  1441. * this range.
  1442. */
  1443. sz = display_range(NULL, low, high,
  1444. current_line_len != 0);
  1445. /*
  1446. * If there is not enough room, display
  1447. * a newline plus the indent of the next
  1448. * line.
  1449. */
  1450. if (current_line_len + sz > DUMP_LINE_MAX) {
  1451. fputs("\n", stream);
  1452. fprintf(stream, "%*s", indent, "");
  1453. current_line_len = 0;
  1454. }
  1455. /* Display the range */
  1456. sz = display_range(stream, low, high,
  1457. current_line_len != 0);
  1458. current_line_len += sz;
  1459. }
  1460. }
  1461. fputs("\n", stream);
  1462. }
  1463. /* Validates the internal state of the sparsebit array given by
  1464. * s. On error, diagnostic information is printed to stderr and
  1465. * abort is called.
  1466. */
  1467. void sparsebit_validate_internal(struct sparsebit *s)
  1468. {
  1469. bool error_detected = false;
  1470. struct node *nodep, *prev = NULL;
  1471. sparsebit_num_t total_bits_set = 0;
  1472. unsigned int n1;
  1473. /* For each node */
  1474. for (nodep = node_first(s); nodep;
  1475. prev = nodep, nodep = node_next(s, nodep)) {
  1476. /*
  1477. * Increase total bits set by the number of bits set
  1478. * in this node.
  1479. */
  1480. for (n1 = 0; n1 < MASK_BITS; n1++)
  1481. if (nodep->mask & (1 << n1))
  1482. total_bits_set++;
  1483. total_bits_set += nodep->num_after;
  1484. /*
  1485. * Arbitrary choice as to whether a mask of 0 is allowed
  1486. * or not. For diagnostic purposes it is beneficial to
  1487. * have only one valid means to represent a set of bits.
  1488. * To support this an arbitrary choice has been made
  1489. * to not allow a mask of zero.
  1490. */
  1491. if (nodep->mask == 0) {
  1492. fprintf(stderr, "Node mask of zero, "
  1493. "nodep: %p nodep->mask: 0x%x",
  1494. nodep, nodep->mask);
  1495. error_detected = true;
  1496. break;
  1497. }
  1498. /*
  1499. * Validate num_after is not greater than the max index
  1500. * - the number of mask bits. The num_after member
  1501. * uses 0-based indexing and thus has no value that
  1502. * represents all bits set. This limitation is handled
  1503. * by requiring a non-zero mask. With a non-zero mask,
  1504. * MASK_BITS worth of bits are described by the mask,
  1505. * which makes the largest needed num_after equal to:
  1506. *
  1507. * (~(sparsebit_num_t) 0) - MASK_BITS + 1
  1508. */
  1509. if (nodep->num_after
  1510. > (~(sparsebit_num_t) 0) - MASK_BITS + 1) {
  1511. fprintf(stderr, "num_after too large, "
  1512. "nodep: %p nodep->num_after: 0x%lx",
  1513. nodep, nodep->num_after);
  1514. error_detected = true;
  1515. break;
  1516. }
  1517. /* Validate node index is divisible by the mask size */
  1518. if (nodep->idx % MASK_BITS) {
  1519. fprintf(stderr, "Node index not divisible by "
  1520. "mask size,\n"
  1521. " nodep: %p nodep->idx: 0x%lx "
  1522. "MASK_BITS: %lu\n",
  1523. nodep, nodep->idx, MASK_BITS);
  1524. error_detected = true;
  1525. break;
  1526. }
  1527. /*
  1528. * Validate bits described by node don't wrap beyond the
  1529. * highest supported index.
  1530. */
  1531. if ((nodep->idx + MASK_BITS + nodep->num_after - 1) < nodep->idx) {
  1532. fprintf(stderr, "Bits described by node wrap "
  1533. "beyond highest supported index,\n"
  1534. " nodep: %p nodep->idx: 0x%lx\n"
  1535. " MASK_BITS: %lu nodep->num_after: 0x%lx",
  1536. nodep, nodep->idx, MASK_BITS, nodep->num_after);
  1537. error_detected = true;
  1538. break;
  1539. }
  1540. /* Check parent pointers. */
  1541. if (nodep->left) {
  1542. if (nodep->left->parent != nodep) {
  1543. fprintf(stderr, "Left child parent pointer "
  1544. "doesn't point to this node,\n"
  1545. " nodep: %p nodep->left: %p "
  1546. "nodep->left->parent: %p",
  1547. nodep, nodep->left,
  1548. nodep->left->parent);
  1549. error_detected = true;
  1550. break;
  1551. }
  1552. }
  1553. if (nodep->right) {
  1554. if (nodep->right->parent != nodep) {
  1555. fprintf(stderr, "Right child parent pointer "
  1556. "doesn't point to this node,\n"
  1557. " nodep: %p nodep->right: %p "
  1558. "nodep->right->parent: %p",
  1559. nodep, nodep->right,
  1560. nodep->right->parent);
  1561. error_detected = true;
  1562. break;
  1563. }
  1564. }
  1565. if (!nodep->parent) {
  1566. if (s->root != nodep) {
  1567. fprintf(stderr, "Unexpected root node, "
  1568. "s->root: %p nodep: %p",
  1569. s->root, nodep);
  1570. error_detected = true;
  1571. break;
  1572. }
  1573. }
  1574. if (prev) {
  1575. /*
  1576. * Is index of previous node before index of
  1577. * current node?
  1578. */
  1579. if (prev->idx >= nodep->idx) {
  1580. fprintf(stderr, "Previous node index "
  1581. ">= current node index,\n"
  1582. " prev: %p prev->idx: 0x%lx\n"
  1583. " nodep: %p nodep->idx: 0x%lx",
  1584. prev, prev->idx, nodep, nodep->idx);
  1585. error_detected = true;
  1586. break;
  1587. }
  1588. /*
  1589. * Nodes occur in asscending order, based on each
  1590. * nodes starting index.
  1591. */
  1592. if ((prev->idx + MASK_BITS + prev->num_after - 1)
  1593. >= nodep->idx) {
  1594. fprintf(stderr, "Previous node bit range "
  1595. "overlap with current node bit range,\n"
  1596. " prev: %p prev->idx: 0x%lx "
  1597. "prev->num_after: 0x%lx\n"
  1598. " nodep: %p nodep->idx: 0x%lx "
  1599. "nodep->num_after: 0x%lx\n"
  1600. " MASK_BITS: %lu",
  1601. prev, prev->idx, prev->num_after,
  1602. nodep, nodep->idx, nodep->num_after,
  1603. MASK_BITS);
  1604. error_detected = true;
  1605. break;
  1606. }
  1607. /*
  1608. * When the node has all mask bits set, it shouldn't
  1609. * be adjacent to the last bit described by the
  1610. * previous node.
  1611. */
  1612. if (nodep->mask == ~(mask_t) 0 &&
  1613. prev->idx + MASK_BITS + prev->num_after == nodep->idx) {
  1614. fprintf(stderr, "Current node has mask with "
  1615. "all bits set and is adjacent to the "
  1616. "previous node,\n"
  1617. " prev: %p prev->idx: 0x%lx "
  1618. "prev->num_after: 0x%lx\n"
  1619. " nodep: %p nodep->idx: 0x%lx "
  1620. "nodep->num_after: 0x%lx\n"
  1621. " MASK_BITS: %lu",
  1622. prev, prev->idx, prev->num_after,
  1623. nodep, nodep->idx, nodep->num_after,
  1624. MASK_BITS);
  1625. error_detected = true;
  1626. break;
  1627. }
  1628. }
  1629. }
  1630. if (!error_detected) {
  1631. /*
  1632. * Is sum of bits set in each node equal to the count
  1633. * of total bits set.
  1634. */
  1635. if (s->num_set != total_bits_set) {
  1636. fprintf(stderr, "Number of bits set missmatch,\n"
  1637. " s->num_set: 0x%lx total_bits_set: 0x%lx",
  1638. s->num_set, total_bits_set);
  1639. error_detected = true;
  1640. }
  1641. }
  1642. if (error_detected) {
  1643. fputs(" dump_internal:\n", stderr);
  1644. sparsebit_dump_internal(stderr, s, 4);
  1645. abort();
  1646. }
  1647. }
  1648. #ifdef FUZZ
  1649. /* A simple but effective fuzzing driver. Look for bugs with the help
  1650. * of some invariants and of a trivial representation of sparsebit.
  1651. * Just use 512 bytes of /dev/zero and /dev/urandom as inputs, and let
  1652. * afl-fuzz do the magic. :)
  1653. */
  1654. #include <stdlib.h>
  1655. #include <assert.h>
  1656. struct range {
  1657. sparsebit_idx_t first, last;
  1658. bool set;
  1659. };
  1660. struct sparsebit *s;
  1661. struct range ranges[1000];
  1662. int num_ranges;
  1663. static bool get_value(sparsebit_idx_t idx)
  1664. {
  1665. int i;
  1666. for (i = num_ranges; --i >= 0; )
  1667. if (ranges[i].first <= idx && idx <= ranges[i].last)
  1668. return ranges[i].set;
  1669. return false;
  1670. }
  1671. static void operate(int code, sparsebit_idx_t first, sparsebit_idx_t last)
  1672. {
  1673. sparsebit_num_t num;
  1674. sparsebit_idx_t next;
  1675. if (first < last) {
  1676. num = last - first + 1;
  1677. } else {
  1678. num = first - last + 1;
  1679. first = last;
  1680. last = first + num - 1;
  1681. }
  1682. switch (code) {
  1683. case 0:
  1684. sparsebit_set(s, first);
  1685. assert(sparsebit_is_set(s, first));
  1686. assert(!sparsebit_is_clear(s, first));
  1687. assert(sparsebit_any_set(s));
  1688. assert(!sparsebit_all_clear(s));
  1689. if (get_value(first))
  1690. return;
  1691. if (num_ranges == 1000)
  1692. exit(0);
  1693. ranges[num_ranges++] = (struct range)
  1694. { .first = first, .last = first, .set = true };
  1695. break;
  1696. case 1:
  1697. sparsebit_clear(s, first);
  1698. assert(!sparsebit_is_set(s, first));
  1699. assert(sparsebit_is_clear(s, first));
  1700. assert(sparsebit_any_clear(s));
  1701. assert(!sparsebit_all_set(s));
  1702. if (!get_value(first))
  1703. return;
  1704. if (num_ranges == 1000)
  1705. exit(0);
  1706. ranges[num_ranges++] = (struct range)
  1707. { .first = first, .last = first, .set = false };
  1708. break;
  1709. case 2:
  1710. assert(sparsebit_is_set(s, first) == get_value(first));
  1711. assert(sparsebit_is_clear(s, first) == !get_value(first));
  1712. break;
  1713. case 3:
  1714. if (sparsebit_any_set(s))
  1715. assert(get_value(sparsebit_first_set(s)));
  1716. if (sparsebit_any_clear(s))
  1717. assert(!get_value(sparsebit_first_clear(s)));
  1718. sparsebit_set_all(s);
  1719. assert(!sparsebit_any_clear(s));
  1720. assert(sparsebit_all_set(s));
  1721. num_ranges = 0;
  1722. ranges[num_ranges++] = (struct range)
  1723. { .first = 0, .last = ~(sparsebit_idx_t)0, .set = true };
  1724. break;
  1725. case 4:
  1726. if (sparsebit_any_set(s))
  1727. assert(get_value(sparsebit_first_set(s)));
  1728. if (sparsebit_any_clear(s))
  1729. assert(!get_value(sparsebit_first_clear(s)));
  1730. sparsebit_clear_all(s);
  1731. assert(!sparsebit_any_set(s));
  1732. assert(sparsebit_all_clear(s));
  1733. num_ranges = 0;
  1734. break;
  1735. case 5:
  1736. next = sparsebit_next_set(s, first);
  1737. assert(next == 0 || next > first);
  1738. assert(next == 0 || get_value(next));
  1739. break;
  1740. case 6:
  1741. next = sparsebit_next_clear(s, first);
  1742. assert(next == 0 || next > first);
  1743. assert(next == 0 || !get_value(next));
  1744. break;
  1745. case 7:
  1746. next = sparsebit_next_clear(s, first);
  1747. if (sparsebit_is_set_num(s, first, num)) {
  1748. assert(next == 0 || next > last);
  1749. if (first)
  1750. next = sparsebit_next_set(s, first - 1);
  1751. else if (sparsebit_any_set(s))
  1752. next = sparsebit_first_set(s);
  1753. else
  1754. return;
  1755. assert(next == first);
  1756. } else {
  1757. assert(sparsebit_is_clear(s, first) || next <= last);
  1758. }
  1759. break;
  1760. case 8:
  1761. next = sparsebit_next_set(s, first);
  1762. if (sparsebit_is_clear_num(s, first, num)) {
  1763. assert(next == 0 || next > last);
  1764. if (first)
  1765. next = sparsebit_next_clear(s, first - 1);
  1766. else if (sparsebit_any_clear(s))
  1767. next = sparsebit_first_clear(s);
  1768. else
  1769. return;
  1770. assert(next == first);
  1771. } else {
  1772. assert(sparsebit_is_set(s, first) || next <= last);
  1773. }
  1774. break;
  1775. case 9:
  1776. sparsebit_set_num(s, first, num);
  1777. assert(sparsebit_is_set_num(s, first, num));
  1778. assert(!sparsebit_is_clear_num(s, first, num));
  1779. assert(sparsebit_any_set(s));
  1780. assert(!sparsebit_all_clear(s));
  1781. if (num_ranges == 1000)
  1782. exit(0);
  1783. ranges[num_ranges++] = (struct range)
  1784. { .first = first, .last = last, .set = true };
  1785. break;
  1786. case 10:
  1787. sparsebit_clear_num(s, first, num);
  1788. assert(!sparsebit_is_set_num(s, first, num));
  1789. assert(sparsebit_is_clear_num(s, first, num));
  1790. assert(sparsebit_any_clear(s));
  1791. assert(!sparsebit_all_set(s));
  1792. if (num_ranges == 1000)
  1793. exit(0);
  1794. ranges[num_ranges++] = (struct range)
  1795. { .first = first, .last = last, .set = false };
  1796. break;
  1797. case 11:
  1798. sparsebit_validate_internal(s);
  1799. break;
  1800. default:
  1801. break;
  1802. }
  1803. }
  1804. unsigned char get8(void)
  1805. {
  1806. int ch;
  1807. ch = getchar();
  1808. if (ch == EOF)
  1809. exit(0);
  1810. return ch;
  1811. }
  1812. uint64_t get64(void)
  1813. {
  1814. uint64_t x;
  1815. x = get8();
  1816. x = (x << 8) | get8();
  1817. x = (x << 8) | get8();
  1818. x = (x << 8) | get8();
  1819. x = (x << 8) | get8();
  1820. x = (x << 8) | get8();
  1821. x = (x << 8) | get8();
  1822. return (x << 8) | get8();
  1823. }
  1824. int main(void)
  1825. {
  1826. s = sparsebit_alloc();
  1827. for (;;) {
  1828. uint8_t op = get8() & 0xf;
  1829. uint64_t first = get64();
  1830. uint64_t last = get64();
  1831. operate(op, first, last);
  1832. }
  1833. }
  1834. #endif