@@ -1414,6 +1414,170 @@ set1, set2 : set
14141414(check-catch 'type-error (set-xor " not a set" s-1))
14151415(check-catch 'value-error (set-xor s-1 s-str-ci))
14161416
1417+ #|
1418+ set-union!
1419+ 将多个 set 并入 set1(可变操作)。
1420+
1421+ 语法
1422+ ----
1423+ (set-union! set1 set2 ...)
1424+
1425+ 参数
1426+ ----
1427+ set1, set2 ... : set
1428+ 参与并集的 set。
1429+
1430+ 返回值
1431+ ------
1432+ 返回修改后的 set1,元素来自它们首次出现的 set。
1433+ |#
1434+
1435+ ; ; 测试 set-union! 基本行为
1436+ (define s-union!-1 (set 1 2 3 ))
1437+ (define s-union!-result (set-union! s-union!-1 s-2-3-4 s-4-5))
1438+ (check-true (eq? s-union!-result s-union!-1))
1439+ (check (set-size s-union!-1) => 5 )
1440+ (check-true (set-contains? s-union!-1 1 ))
1441+ (check-true (set-contains? s-union!-1 5 ))
1442+
1443+ ; ; 测试元素来源(使用大小写不敏感比较器)
1444+ (define s-union!-ci-1 (list->set-with-comparator string-ci-comparator ' (" Apple" )))
1445+ (define s-union!-ci-2 (list->set-with-comparator string-ci-comparator ' (" apple" " Banana" )))
1446+ (define s-union!-ci (set-union! s-union!-ci-1 s-union!-ci-2))
1447+ (check-true (eq? s-union!-ci s-union!-ci-1))
1448+ (check (set-member s-union!-ci " apple" 'not-found ) => " Apple" )
1449+ (check (set-member s-union!-ci " banana" 'not-found ) => " Banana" )
1450+
1451+ ; ; 测试类型与比较器错误
1452+ (check-catch 'type-error (set-union! " not a set" s-1))
1453+ (check-catch 'value-error (set-union! s-1 s-str-ci))
1454+
1455+ #|
1456+ set-intersection!
1457+ 就地更新 set1,使其成为多个 set 的交集。
1458+
1459+ 语法
1460+ ----
1461+ (set-intersection! set1 set2 ...)
1462+
1463+ 参数
1464+ ----
1465+ set1, set2 ... : set
1466+ 参与交集的 set。
1467+
1468+ 返回值
1469+ ------
1470+ 返回修改后的 set1,元素来自第一个 set。
1471+ |#
1472+
1473+ ; ; 测试 set-intersection! 基本行为
1474+ (define s-inter!-1 (set 1 2 3 4 ))
1475+ (define s-inter!-result (set-intersection! s-inter!-1 s-2-3-4))
1476+ (check-true (eq? s-inter!-result s-inter!-1))
1477+ (check (set-size s-inter!-1) => 3 )
1478+ (check-true (set-contains? s-inter!-1 2 ))
1479+ (check-true (set-contains? s-inter!-1 3 ))
1480+ (check-true (set-contains? s-inter!-1 4 ))
1481+
1482+ ; ; 测试多集合交集
1483+ (define s-inter!-2 (set 1 2 3 4 ))
1484+ (set-intersection! s-inter!-2 s-2-3-4 (set 2 3 ))
1485+ (check (set-size s-inter!-2) => 2 )
1486+ (check-true (set-contains? s-inter!-2 2 ))
1487+ (check-true (set-contains? s-inter!-2 3 ))
1488+
1489+ ; ; 测试元素来源(使用大小写不敏感比较器)
1490+ (define s-inter!-ci-1 (list->set-with-comparator string-ci-comparator ' (" Apple" " Banana" )))
1491+ (define s-inter!-ci-2 (list->set-with-comparator string-ci-comparator ' (" apple" " Pear" )))
1492+ (set-intersection! s-inter!-ci-1 s-inter!-ci-2)
1493+ (check (set-member s-inter!-ci-1 " apple" 'not-found ) => " Apple" )
1494+ (check (set-size s-inter!-ci-1) => 1 )
1495+
1496+ ; ; 测试类型与比较器错误
1497+ (check-catch 'type-error (set-intersection! " not a set" s-1))
1498+ (check-catch 'value-error (set-intersection! s-1 s-str-ci))
1499+
1500+ #|
1501+ set-difference!
1502+ 就地更新 set1,使其成为与其余 set 的差集。
1503+
1504+ 语法
1505+ ----
1506+ (set-difference! set1 set2 ...)
1507+
1508+ 参数
1509+ ----
1510+ set1, set2 ... : set
1511+ 参与差集的 set。
1512+
1513+ 返回值
1514+ ------
1515+ 返回修改后的 set1,元素来自第一个 set。
1516+ |#
1517+
1518+ ; ; 测试 set-difference! 基本行为
1519+ (define s-diff!-1 (set 1 2 3 4 ))
1520+ (define s-diff!-result (set-difference! s-diff!-1 s-2-3-4))
1521+ (check-true (eq? s-diff!-result s-diff!-1))
1522+ (check (set-size s-diff!-1) => 1 )
1523+ (check-true (set-contains? s-diff!-1 1 ))
1524+ (check-false (set-contains? s-diff!-1 2 ))
1525+
1526+ ; ; 测试多集合差集
1527+ (define s-diff!-2 (set 1 2 3 4 5 ))
1528+ (set-difference! s-diff!-2 s-2-3-4 s-4-5)
1529+ (check (set-size s-diff!-2) => 1 )
1530+ (check-true (set-contains? s-diff!-2 1 ))
1531+
1532+ ; ; 测试元素来源(使用大小写不敏感比较器)
1533+ (define s-diff!-ci-1 (list->set-with-comparator string-ci-comparator ' (" Apple" " Banana" )))
1534+ (define s-diff!-ci-2 (list->set-with-comparator string-ci-comparator ' (" apple" )))
1535+ (set-difference! s-diff!-ci-1 s-diff!-ci-2)
1536+ (check (set-size s-diff!-ci-1) => 1 )
1537+ (check (set-member s-diff!-ci-1 " banana" 'not-found ) => " Banana" )
1538+
1539+ ; ; 测试类型与比较器错误
1540+ (check-catch 'type-error (set-difference! " not a set" s-1))
1541+ (check-catch 'value-error (set-difference! s-1 s-str-ci))
1542+
1543+ #|
1544+ set-xor!
1545+ 就地更新 set1,使其成为与 set2 的对称差集。
1546+
1547+ 语法
1548+ ----
1549+ (set-xor! set1 set2)
1550+
1551+ 参数
1552+ ----
1553+ set1, set2 : set
1554+ 参与对称差集的 set。
1555+
1556+ 返回值
1557+ ------
1558+ 返回修改后的 set1。
1559+ |#
1560+
1561+ ; ; 测试 set-xor! 基本行为
1562+ (define s-xor!-1 (set 1 2 3 ))
1563+ (define s-xor!-result (set-xor! s-xor!-1 s-2-3-4))
1564+ (check-true (eq? s-xor!-result s-xor!-1))
1565+ (check (set-size s-xor!-1) => 2 )
1566+ (check-true (set-contains? s-xor!-1 1 ))
1567+ (check-true (set-contains? s-xor!-1 4 ))
1568+
1569+ ; ; 测试元素来源(使用大小写不敏感比较器)
1570+ (define s-xor!-ci-1 (list->set-with-comparator string-ci-comparator ' (" Apple" )))
1571+ (define s-xor!-ci-2 (list->set-with-comparator string-ci-comparator ' (" apple" " Banana" )))
1572+ (set-xor! s-xor!-ci-1 s-xor!-ci-2)
1573+ (check (set-size s-xor!-ci-1) => 1 )
1574+ (check (set-member s-xor!-ci-1 " banana" 'not-found ) => " Banana" )
1575+ (check-false (set-contains? s-xor!-ci-1 " apple" ))
1576+
1577+ ; ; 测试类型与比较器错误
1578+ (check-catch 'type-error (set-xor! " not a set" s-1))
1579+ (check-catch 'value-error (set-xor! s-1 s-str-ci))
1580+
14171581#|
14181582set->list
14191583将 set 转换为列表(顺序未指定)。
0 commit comments