Skip to content

Commit 98cf89e

Browse files
committed
librlist: add rlist_subtract()
Problem: It would be useful to have an rlist diff routine the modifies the rlist argument, but this code is embedded in rlist_diff(), which returns a new rlist. Split rlist_subtract() out of rlist_diff() and make it public. Have rlist_diff() call rlist_subtract() internally.
1 parent d23661e commit 98cf89e

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/common/librlist/rlist.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,17 +533,9 @@ static struct rnode *rlist_detach_rank (struct rlist *rl, uint32_t rank)
533533
return n;
534534
}
535535

536-
struct rlist *rlist_diff (const struct rlist *rla, const struct rlist *rlb)
536+
int rlist_subtract (struct rlist *rl, const struct rlist *rlb)
537537
{
538-
struct rnode *n;
539-
struct rlist *rl = rlist_create ();
540-
541-
if (!rl || rlist_append (rl, rla) < 0) {
542-
rlist_destroy (rl);
543-
return NULL;
544-
}
545-
546-
n = zlistx_first (rlb->nodes);
538+
struct rnode *n = zlistx_first (rlb->nodes);
547539
while (n) {
548540
/* Attempt to find and "detach" the rank which we're diffing.
549541
*/
@@ -565,6 +557,18 @@ struct rlist *rlist_diff (const struct rlist *rla, const struct rlist *rlb)
565557
}
566558
n = zlistx_next (rlb->nodes);
567559
}
560+
return 0;
561+
}
562+
563+
struct rlist *rlist_diff (const struct rlist *rla, const struct rlist *rlb)
564+
{
565+
struct rlist *rl = rlist_create ();
566+
if (!rl
567+
|| rlist_append (rl, rla) < 0
568+
|| rlist_subtract (rl, rlb) < 0) {
569+
rlist_destroy (rl);
570+
return NULL;
571+
}
568572
return rl;
569573
}
570574

src/common/librlist/rlist.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ int rlist_append (struct rlist *rl, const struct rlist *rl2);
151151
*/
152152
int rlist_add (struct rlist *rl, const struct rlist *rl2);
153153

154+
/* Subtract resources in `rl2` from `rl`. It is not an error if
155+
* resources in rl2 are not present in `rl`.
156+
*/
157+
int rlist_subtract (struct rlist *rl, const struct rlist *rl2);
158+
154159
/* Return the set difference of 'rlb' from 'rla'.
155160
*/
156161
struct rlist *rlist_diff (const struct rlist *rla, const struct rlist *rlb);

0 commit comments

Comments
 (0)