]> git.ozlabs.org Git - ccan/blobdiff - ccan/intmap/intmap.h
intmap: implement intmap_before()
[ccan] / ccan / intmap / intmap.h
index 207b7c9d2b18f993fd3a2a7692f8146391e3f864..834c969fa7c77e46dcc262b50b7b4c80b3c9b4d5 100644 (file)
@@ -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)
 {