@@ -407,6 +407,9 @@ static Box* setIssubset(BoxedSet* self, Box* container) {
407
407
assert (PyAnySet_Check (container));
408
408
409
409
BoxedSet* rhs = static_cast <BoxedSet*>(container);
410
+ if (self->s .size () > rhs->s .size ())
411
+ return False;
412
+
410
413
for (auto e : self->s ) {
411
414
if (rhs->s .find (e) == rhs->s .end ())
412
415
return False;
@@ -421,13 +424,7 @@ static Box* setIssuperset(BoxedSet* self, Box* container) {
421
424
container = makeNewSet (set_cls, container);
422
425
}
423
426
assert (PyAnySet_Check (container));
424
-
425
- BoxedSet* rhs = static_cast <BoxedSet*>(container);
426
- for (auto e : rhs->s ) {
427
- if (self->s .find (e) == self->s .end ())
428
- return False;
429
- }
430
- return True;
427
+ return setIssubset ((BoxedSet*)container, self);
431
428
}
432
429
433
430
static Box* setIsdisjoint (BoxedSet* self, Box* container) {
@@ -473,7 +470,7 @@ Box* setCopy(BoxedSet* self) {
473
470
RELEASE_ASSERT (PyAnySet_Check (self), " " );
474
471
475
472
BoxedSet* rtn = new BoxedSet ();
476
- rtn->s . insert (self-> s . begin (), self->s . end ()) ;
473
+ rtn->s = self->s ;
477
474
return rtn;
478
475
}
479
476
@@ -497,24 +494,56 @@ Box* setContains(BoxedSet* self, Box* v) {
497
494
Box* setEq (BoxedSet* self, BoxedSet* rhs) {
498
495
RELEASE_ASSERT (PyAnySet_Check (self), " " );
499
496
if (!PyAnySet_Check (rhs))
500
- return NotImplemented ;
497
+ return False ;
501
498
502
499
if (self->s .size () != rhs->s .size ())
503
500
return False;
504
501
505
- for (auto e : self->s ) {
506
- if (!rhs->s .count (e))
507
- return False;
508
- }
509
- return True;
502
+ return setIssubset (self, rhs);
510
503
}
511
504
512
505
Box* setNe (BoxedSet* self, BoxedSet* rhs) {
513
506
Box* r = setEq (self, rhs);
514
- if (r->cls == bool_cls)
515
- return boxBool (r == False);
516
- assert (r == NotImplemented);
517
- return r;
507
+ assert (r->cls == bool_cls);
508
+ return boxBool (r == False);
509
+ }
510
+
511
+ Box* setLe (BoxedSet* self, BoxedSet* rhs) {
512
+ RELEASE_ASSERT (PyAnySet_Check (self), " " );
513
+ if (!PyAnySet_Check (rhs))
514
+ raiseExcHelper (TypeError, " can only compare to a set" );
515
+
516
+ return setIssubset (self, rhs);
517
+ }
518
+
519
+ Box* setLt (BoxedSet* self, BoxedSet* rhs) {
520
+ RELEASE_ASSERT (PyAnySet_Check (self), " " );
521
+ if (!PyAnySet_Check (rhs))
522
+ raiseExcHelper (TypeError, " can only compare to a set" );
523
+
524
+ if (self->s .size () >= rhs->s .size ())
525
+ return False;
526
+
527
+ return setIssubset (self, rhs);
528
+ }
529
+
530
+ Box* setGe (BoxedSet* self, BoxedSet* rhs) {
531
+ RELEASE_ASSERT (PyAnySet_Check (self), " " );
532
+ if (!PyAnySet_Check (rhs))
533
+ raiseExcHelper (TypeError, " can only compare to a set" );
534
+
535
+ return setIssuperset (self, rhs);
536
+ }
537
+
538
+ Box* setGt (BoxedSet* self, BoxedSet* rhs) {
539
+ RELEASE_ASSERT (PyAnySet_Check (self), " " );
540
+ if (!PyAnySet_Check (rhs))
541
+ raiseExcHelper (TypeError, " can only compare to a set" );
542
+
543
+ if (self->s .size () <= rhs->s .size ())
544
+ return False;
545
+
546
+ return setIssuperset (self, rhs);
518
547
}
519
548
520
549
Box* setNonzero (BoxedSet* self) {
@@ -627,10 +656,18 @@ void setupSet() {
627
656
set_cls->giveAttr (" __contains__" , new BoxedFunction (boxRTFunction ((void *)setContains, BOXED_BOOL, 2 )));
628
657
frozenset_cls->giveAttr (" __contains__" , set_cls->getattr (internStringMortal (" __contains__" )));
629
658
630
- set_cls->giveAttr (" __eq__" , new BoxedFunction (boxRTFunction ((void *)setEq, UNKNOWN , 2 )));
659
+ set_cls->giveAttr (" __eq__" , new BoxedFunction (boxRTFunction ((void *)setEq, BOXED_BOOL , 2 )));
631
660
frozenset_cls->giveAttr (" __eq__" , set_cls->getattr (internStringMortal (" __eq__" )));
632
- set_cls->giveAttr (" __ne__" , new BoxedFunction (boxRTFunction ((void *)setNe, UNKNOWN , 2 )));
661
+ set_cls->giveAttr (" __ne__" , new BoxedFunction (boxRTFunction ((void *)setNe, BOXED_BOOL , 2 )));
633
662
frozenset_cls->giveAttr (" __ne__" , set_cls->getattr (internStringMortal (" __ne__" )));
663
+ set_cls->giveAttr (" __le__" , new BoxedFunction (boxRTFunction ((void *)setLe, BOXED_BOOL, 2 )));
664
+ frozenset_cls->giveAttr (" __le__" , set_cls->getattr (internStringMortal (" __le__" )));
665
+ set_cls->giveAttr (" __lt__" , new BoxedFunction (boxRTFunction ((void *)setLt, BOXED_BOOL, 2 )));
666
+ frozenset_cls->giveAttr (" __lt__" , set_cls->getattr (internStringMortal (" __lt__" )));
667
+ set_cls->giveAttr (" __ge__" , new BoxedFunction (boxRTFunction ((void *)setGe, BOXED_BOOL, 2 )));
668
+ frozenset_cls->giveAttr (" __ge__" , set_cls->getattr (internStringMortal (" __ge__" )));
669
+ set_cls->giveAttr (" __gt__" , new BoxedFunction (boxRTFunction ((void *)setGt, BOXED_BOOL, 2 )));
670
+ frozenset_cls->giveAttr (" __gt__" , set_cls->getattr (internStringMortal (" __gt__" )));
634
671
635
672
set_cls->giveAttr (" __nonzero__" , new BoxedFunction (boxRTFunction ((void *)setNonzero, BOXED_BOOL, 1 )));
636
673
frozenset_cls->giveAttr (" __nonzero__" , set_cls->getattr (internStringMortal (" __nonzero__" )));
0 commit comments