c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef SEQ_RANGE_ARRAY_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define SEQ_RANGE_ARRAY_H
503a863a317acba125a4e46435694e35fad769e4Timo Sirainen
503a863a317acba125a4e46435694e35fad769e4Timo Sirainenstruct seq_range {
503a863a317acba125a4e46435694e35fad769e4Timo Sirainen uint32_t seq1, seq2;
503a863a317acba125a4e46435694e35fad769e4Timo Sirainen};
8d80659e504ffb34bb0c6a633184fece35751b18Timo SirainenARRAY_DEFINE_TYPE(seq_range, struct seq_range);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainenstruct seq_range_iter {
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen const ARRAY_TYPE(seq_range) *array;
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen unsigned int prev_n, prev_idx;
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen};
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmodystatic inline uint32_t ATTR_PURE seq_range_length(struct seq_range *range)
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody{
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody i_assert(range->seq2 >= range->seq1);
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody return range->seq2 - range->seq1 + 1;
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody}
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody/* Add sequence to range. If the array isn't created yet, create it with
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen initial size of init_count. */
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenbool ATTR_NOWARN_UNUSED_RESULT
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenseq_range_array_add(ARRAY_TYPE(seq_range) *array, uint32_t seq);
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen/* Like seq_range_array_add(), but if the array isn't already initialized do
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen it with i_array_init(). */
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainenvoid seq_range_array_add_with_init(ARRAY_TYPE(seq_range) *array,
86bde2c1838d1ce967fa2b394bb952004a4adcb7Timo Sirainen unsigned int init_count, uint32_t seq);
605eca549c08af753e05c25937bcccd66079c321Timo Sirainenvoid seq_range_array_add_range(ARRAY_TYPE(seq_range) *array,
605eca549c08af753e05c25937bcccd66079c321Timo Sirainen uint32_t seq1, uint32_t seq2);
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmodyunsigned int seq_range_array_add_range_count(ARRAY_TYPE(seq_range) *array,
ba66ac5557ca97d8a6fe5d524056264a9f92243cPhil Carmody uint32_t seq1, uint32_t seq2);
6646bd844c85d5b27451199d8868b6d2357cd293Timo Sirainenvoid seq_range_array_merge(ARRAY_TYPE(seq_range) *dest,
6646bd844c85d5b27451199d8868b6d2357cd293Timo Sirainen const ARRAY_TYPE(seq_range) *src);
19557f192d37cd54a1a090a8a26d9d47265e4413Aki Tuomi/* Remove the given sequence from range. Returns TRUE if it was found. */
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenbool ATTR_NOWARN_UNUSED_RESULT
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenseq_range_array_remove(ARRAY_TYPE(seq_range) *array, uint32_t seq);
ef174bf5299348e8c0662d235341869f319cfe54Timo Sirainen/* Remove a sequence range. Returns number of sequences actually removed. */
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenunsigned int ATTR_NOWARN_UNUSED_RESULT
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenseq_range_array_remove_range(ARRAY_TYPE(seq_range) *array,
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen uint32_t seq1, uint32_t seq2);
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenunsigned int ATTR_NOWARN_UNUSED_RESULT
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenseq_range_array_remove_seq_range(ARRAY_TYPE(seq_range) *dest,
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen const ARRAY_TYPE(seq_range) *src);
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen/* Remove count number of sequences from the nth sequence (0 = first). */
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainenvoid seq_range_array_remove_nth(ARRAY_TYPE(seq_range) *array,
199566f5a171b2c43b9a5254634f6bf47b8baca8Timo Sirainen uint32_t n, uint32_t count);
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen/* Remove sequences from dest that don't exist in src. */
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenunsigned int ATTR_NOWARN_UNUSED_RESULT
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainenseq_range_array_intersect(ARRAY_TYPE(seq_range) *dest,
461ffead9720d1e516b959d5e41f049c73d38c7cTimo Sirainen const ARRAY_TYPE(seq_range) *src);
ef174bf5299348e8c0662d235341869f319cfe54Timo Sirainen/* Returns TRUE if sequence exists in the range. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenbool seq_range_exists(const ARRAY_TYPE(seq_range) *array,
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen uint32_t seq) ATTR_PURE;
e4cebacdec9c9e5b685dde5f7cbf7a5cf7e1d248Timo Sirainen/* Returns TRUE if arrays have common sequences. */
e4cebacdec9c9e5b685dde5f7cbf7a5cf7e1d248Timo Sirainenbool seq_range_array_have_common(const ARRAY_TYPE(seq_range) *array1,
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainen const ARRAY_TYPE(seq_range) *array2) ATTR_PURE;
9905ec03fb2011419caeac4cd5a1b6c28ab50a73Timo Sirainen/* Return number of sequences in the range. */
68a4946b12583b88fa802e52ebee45cd96056772Timo Sirainenunsigned int seq_range_count(const ARRAY_TYPE(seq_range) *array) ATTR_PURE;
503a863a317acba125a4e46435694e35fad769e4Timo Sirainen
8927ce341a9f075798d36107c2eafbf7499b4ad6Timo Sirainen/* Invert the sequence range. For example 5:6 -> min_seq:4,7:max_seq.
8927ce341a9f075798d36107c2eafbf7499b4ad6Timo Sirainen The array must not have any sequences outside min_seq..max_seq or this
8927ce341a9f075798d36107c2eafbf7499b4ad6Timo Sirainen function will assert-crash. */
90b50df264b57e0f63cd8cc6aea1ce3bb7cf5f64Timo Sirainenvoid seq_range_array_invert(ARRAY_TYPE(seq_range) *array,
90b50df264b57e0f63cd8cc6aea1ce3bb7cf5f64Timo Sirainen uint32_t min_seq, uint32_t max_seq);
90b50df264b57e0f63cd8cc6aea1ce3bb7cf5f64Timo Sirainen
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainenvoid seq_range_array_iter_init(struct seq_range_iter *iter_r,
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen const ARRAY_TYPE(seq_range) *array);
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen/* Get the nth sequence (0 = first). Returns FALSE if idx is too large. */
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainenbool seq_range_array_iter_nth(struct seq_range_iter *iter, unsigned int n,
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen uint32_t *seq_r);
5fdeff082e329e4a85bb7e74aaec2c35e2288557Timo Sirainen
503a863a317acba125a4e46435694e35fad769e4Timo Sirainen#endif