From: Ben Pfaff Date: Wed, 3 Feb 2010 04:15:38 +0000 (-0800) Subject: llx: New function llx_find() to find a pointer in a list. X-Git-Tag: v0.7.4~20 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=88160345d1b718d5d4f7894bcd7139fd0bc1b404;p=pspp-builds.git llx: New function llx_find() to find a pointer in a list. --- diff --git a/src/libpspp/llx.c b/src/libpspp/llx.c index c58f840c..905850bc 100644 --- a/src/libpspp/llx.c +++ b/src/libpspp/llx.c @@ -219,6 +219,20 @@ llx_remove_if (struct llx *r0, struct llx *r1, return count; } +/* Returns the first node in R0...R1 that has data TARGET. + Returns NULL if no node in R0...R1 equals TARGET. */ +struct llx * +llx_find (const struct llx *r0, const struct llx *r1, const void *target) +{ + const struct llx *x; + + for (x = r0; x != r1; x = llx_next (x)) + if (llx_data (x) == target) + return CONST_CAST (struct llx *, x); + + return NULL; +} + /* Returns the first node in R0...R1 that equals TARGET according to COMPARE given auxiliary data AUX. Returns R1 if no node in R0...R1 equals TARGET. */ diff --git a/src/libpspp/llx.h b/src/libpspp/llx.h index 23c3bdaf..2aa18e7b 100644 --- a/src/libpspp/llx.h +++ b/src/libpspp/llx.h @@ -186,6 +186,8 @@ size_t llx_remove_if (struct llx *r0, struct llx *r1, const struct llx_manager *); /* Non-mutating algorithms. */ +struct llx *llx_find (const struct llx *r0, const struct llx *r1, + const void *target); struct llx *llx_find_equal (const struct llx *r0, const struct llx *r1, const void *target, llx_compare_func *, void *aux); diff --git a/tests/libpspp/llx-test.c b/tests/libpspp/llx-test.c index 2befd51a..f8fe9b73 100644 --- a/tests/libpspp/llx-test.c +++ b/tests/libpspp/llx-test.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2006 Free Software Foundation, Inc. + Copyright (C) 2006, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -865,6 +865,34 @@ test_find_equal (void) test_examine_equal_range (test_find_equal_helper); } +/* Tests llx_find(). */ +static void +test_find (void) +{ + const int max_elems = 8; + + int cnt; + + for (cnt = 0; cnt <= max_elems; cnt++) + { + struct llx_list list; + struct element **elems; + struct llx **elemp; + int *values; + + int i; + + allocate_ascending (cnt, &list, &elems, &elemp, &values); + + for (i = 0; i < cnt; i++) + check (llx_find (llx_head (&list), llx_null (&list), elems[i]) + == elemp[i]); + check (llx_find (llx_head (&list), llx_null (&list), NULL) == NULL); + + free_elements (cnt, &list, elems, elemp, values); + } +} + /* Helper function for testing llx_find_if. */ static void test_find_if_helper (int r0, int r1, int eq_pat, struct llx **elemp) @@ -2040,6 +2068,7 @@ main (void) run_test (test_remove_equal, "remove_equal"); run_test (test_remove_if, "remove_if"); run_test (test_find_equal, "find_equal"); + run_test (test_find, "find"); run_test (test_find_if, "find_if"); run_test (test_find_adjacent_equal, "find_adjacent_equal"); run_test (test_count_range, "count_range");