X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fintmap%2Fintmap.h;h=834c969fa7c77e46dcc262b50b7b4c80b3c9b4d5;hp=207b7c9d2b18f993fd3a2a7692f8146391e3f864;hb=7aac849abf06ef469c2aad9a0dd6e3bb82ef1f96;hpb=f7ead5da96bfb5ce3ebffe11256bbcb5f4b4f750 diff --git a/ccan/intmap/intmap.h b/ccan/intmap/intmap.h index 207b7c9d..834c969f 100644 --- a/ccan/intmap/intmap.h +++ b/ccan/intmap/intmap.h @@ -309,6 +309,32 @@ void *intmap_after_(const struct intmap *map, intmap_index_t *indexp); tcon_cast((smap), sintmap_canary, \ sintmap_after_(sintmap_unwrap_(smap), (indexp))) +/** + * uintmap_before - get the closest preceding index in an unsigned intmap + * @umap: the typed intmap to iterate through. + * @indexp: the succeeding index (may not exist) + * + * Returns NULL if the there is no entry < @indexp, otherwise + * populates *@indexp and returns the highest entry < @indexp. + */ +#define uintmap_before(umap, indexp) \ + tcon_cast((umap), uintmap_canary, \ + intmap_before_(uintmap_unwrap_(umap), (indexp))) + +void *intmap_before_(const struct intmap *map, intmap_index_t *indexp); + +/** + * sintmap_before - get the closest preceding index in a signed intmap + * @smap: the typed intmap to iterate through. + * @indexp: the succeeding index (may not exist) + * + * Returns NULL if the there is no entry < @indexp, otherwise + * populates *@indexp and returns the highest entry < @indexp. + */ +#define sintmap_before(smap, indexp) \ + tcon_cast((smap), sintmap_canary, \ + sintmap_before_(sintmap_unwrap_(smap), (indexp))) + /** * uintmap_last - get last value in an unsigned intmap * @umap: the typed intmap to iterate through. @@ -455,6 +481,15 @@ static inline void *sintmap_after_(const struct intmap *map, return ret; } +static inline void *sintmap_before_(const struct intmap *map, + sintmap_index_t *indexp) +{ + intmap_index_t i = SINTMAP_OFF(*indexp); + void *ret = intmap_before_(map, &i); + *indexp = SINTMAP_UNOFF(i); + return ret; +} + static inline void *sintmap_last_(const struct intmap *map, sintmap_index_t *indexp) {