Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

r""" 

Bitsets 

  

A Python interface to the fast bitsets in Sage. Bitsets are fast 

binary sets that store elements by toggling bits in an array of 

numbers. A bitset can store values between `0` and ``capacity - 1``, 

inclusive (where ``capacity`` is finite, but arbitrary). The storage cost is 

linear in ``capacity``. 

  

.. warning:: 

  

This class is most likely to be useful as a way to store Cython 

bitsets in Python data structures, acting on them using the Cython 

inline functions. If you want to use these classes for a Python 

set type, the Python ``set`` or ``frozenset`` data types may be 

faster. 

""" 

  

#***************************************************************************** 

# Copyright (C) 2009 Jason Grout <jason-sage@creativetrax.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

  

include "bitset.pxi" 

from cpython.object cimport Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, Py_GE 

  

  

cdef class FrozenBitset: 

r""" 

A frozen bitset class which leverages inline Cython functions for 

creating and manipulating bitsets. 

  

A bitset can be thought of in two ways. First, as a set of elements 

from the universe of the `n` natural numbers `0, 1, \dots, n-1` (where 

the capacity `n` can be specified), with typical set operations such as 

intersection, union, symmetric difference, etc. Secondly, a bitset can 

be thought of as a binary vector with typical binary operations such as 

``and``, ``or``, ``xor``, etc. This class supports both interfaces. 

  

The interface in this class mirrors the interface in the ``frozenset`` 

data type of Python. See the Python documentation on `set types 

<http://docs.python.org/library/stdtypes.html#set-types-set-frozenset>`_ 

for more details on Python's ``set`` and ``frozenset`` classes. 

  

.. warning:: 

  

This class is most likely to be useful as a way to store 

Cython bitsets in Python data structures, acting on them using 

the Cython inline functions. If you want to use this class 

for a Python set type, the Python ``frozenset`` data type may 

be faster. 

  

INPUT: 

  

- ``iter`` -- initialization parameter (default: ``None``). Valid input 

are: 

  

- :class:`Bitset` and :class:`FrozenBitset` -- If this is a 

:class:`Bitset` or :class:`FrozenBitset`, then it is copied. 

  

- ``None`` -- If ``None``, then the bitset is set to the empty set. 

  

- string -- If a nonempty string, then the bitset is initialized by 

including an element if the index of the string is ``1``. If the 

string is empty, then raise a ``ValueError``. 

  

- iterable -- If an iterable, then it is assumed to contain a list of 

nonnegative integers and those integers are placed in the set. 

  

- ``capacity`` -- (default: ``None``) The maximum capacity of the bitset. 

If this is not specified, then it is automatically calculated from the 

passed iterable. It must be at least one. 

  

OUTPUT: 

  

- None. 

  

The string representation of a :class:`FrozenBitset` ``FB`` can be 

understood as follows. Let `B = b_0 b_1 b_2 \cdots b_k` be the string 

representation of the bitset ``FB``, where each `b_i \in \{0, 1\}`. We 

read the `b_i` from left to right. If `b_i = 1`, then the nonnegative 

integer `i` is in the bitset ``FB``. Similarly, if `b_i = 0`, then `i` 

is not in ``FB``. In other words, ``FB`` is a subset of 

`\{0, 1, 2, \dots, k\}` and the membership in ``FB`` of each `i` is 

determined by the binary value `b_i`. 

  

.. SEEALSO:: 

  

- :class:`Bitset` 

  

- Python's `set types <http://docs.python.org/library/stdtypes.html#set-types-set-frozenset>`_ 

  

EXAMPLES: 

  

The default bitset, which has capacity 1:: 

  

sage: FrozenBitset() 

0 

sage: FrozenBitset(None) 

0 

  

Trying to create an empty bitset fails:: 

  

sage: FrozenBitset([]) 

Traceback (most recent call last): 

... 

ValueError: Bitsets must not be empty 

sage: FrozenBitset(list()) 

Traceback (most recent call last): 

... 

ValueError: Bitsets must not be empty 

sage: FrozenBitset(()) 

Traceback (most recent call last): 

... 

ValueError: Bitsets must not be empty 

sage: FrozenBitset(tuple()) 

Traceback (most recent call last): 

... 

ValueError: Bitsets must not be empty 

sage: FrozenBitset("") 

Traceback (most recent call last): 

... 

ValueError: Bitsets must not be empty 

  

We can create the all-zero bitset as follows:: 

  

sage: FrozenBitset(capacity=10) 

0000000000 

sage: FrozenBitset([], capacity=10) 

0000000000 

  

We can initialize a :class:`FrozenBitset` with a :class:`Bitset` or 

another :class:`FrozenBitset`, and compare them for equality. As they 

are logically the same bitset, the equality test should return ``True``. 

Furthermore, each bitset is a subset of the other. :: 

  

sage: def bitcmp(a, b, c): # custom function for comparing bitsets 

....: print(a == b == c) 

....: print((a <= b, b <= c, a <= c)) 

....: print((a >= b, b >= c, a >= c)) 

....: print((a != b, b != c, a != c)) 

sage: a = Bitset("1010110"); b = FrozenBitset(a); c = FrozenBitset(b) 

sage: a; b; c 

1010110 

1010110 

1010110 

sage: a < b, b < c, a < c 

(False, False, False) 

sage: a > b, b > c, a > c 

(False, False, False) 

sage: bitcmp(a, b, c) 

True 

(True, True, True) 

(True, True, True) 

(False, False, False) 

  

Try a random bitset:: 

  

sage: a = Bitset(randint(0, 1) for n in range(1, randint(1, 10^4))) 

sage: b = FrozenBitset(a); c = FrozenBitset(b) 

sage: bitcmp(a, b, c) 

True 

(True, True, True) 

(True, True, True) 

(False, False, False) 

  

A bitset with a hard-coded bitstring:: 

  

sage: FrozenBitset('101') 

101 

  

For a string, only those positions with ``1`` would be initialized to ``1`` 

in the corresponding position in the bitset. All other characters in the 

string, including ``0``, are set to ``0`` in the resulting bitset. :: 

  

sage: FrozenBitset('a') 

0 

sage: FrozenBitset('abc') 

000 

sage: FrozenBitset('abc1') 

0001 

sage: FrozenBitset('0abc1') 

00001 

sage: FrozenBitset('0abc10') 

000010 

sage: FrozenBitset('0a*c10') 

000010 

  

Represent the first 10 primes as a bitset. The primes are stored as a 

list and as a tuple. We then recover the primes from its bitset 

representation, and query the bitset for its length (how many elements 

it contains) and whether an element is in the bitset. Note that the 

length of a bitset is different from its capacity. The length counts 

the number of elements currently in the bitset, while the capacity 

is the number of elements that the bitset can hold. :: 

  

sage: p = primes_first_n(10); p 

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] 

sage: tuple(p) 

(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) 

sage: F = FrozenBitset(p); F; FrozenBitset(tuple(p)) 

001101010001010001010001000001 

001101010001010001010001000001 

  

Recover the primes from the bitset:: 

  

sage: for b in F: 

....: print(b) 

2 

3 

... 

29 

sage: list(F) 

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29] 

  

Query the bitset:: 

  

sage: len(F) 

10 

sage: len(list(F)) 

10 

sage: F.capacity() 

30 

sage: s = str(F); len(s) 

30 

sage: 2 in F 

True 

sage: 1 in F 

False 

  

A random iterable, with all duplicate elements removed:: 

  

sage: L = [randint(0, 100) for n in range(1, randint(1, 10^4))] 

sage: FrozenBitset(L) == FrozenBitset(list(set(L))) 

True 

sage: FrozenBitset(tuple(L)) == FrozenBitset(tuple(set(L))) 

True 

  

TESTS: 

  

Loading and dumping objects:: 

  

sage: a = FrozenBitset('1101') 

sage: loads(dumps(a)) == a 

True 

sage: a = FrozenBitset('1101' * 64) 

sage: loads(dumps(a)) == a 

True 

  

If ``iter`` is a nonempty string and ``capacity`` is specified, then 

``capacity`` must match the number of elements in ``iter``:: 

  

sage: FrozenBitset("110110", capacity=3) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not match passed string 

sage: FrozenBitset("110110", capacity=100) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not match passed string 

  

The parameter ``capacity`` must be positive:: 

  

sage: FrozenBitset("110110", capacity=0) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity must be greater than 0 

sage: FrozenBitset("110110", capacity=-2) 

Traceback (most recent call last): 

... 

OverflowError: can't convert negative value to mp_bitcnt_t 

""" 

def __cinit__(self, iter=None, capacity=None): 

""" 

Allocate the bitset, which is initially empty. 

  

See the class documentation of :class:`FrozenBitset` for details 

on the parameters. 

  

EXAMPLES:: 

  

sage: FrozenBitset('1101') 

1101 

sage: FrozenBitset('1101' * 32) 

11011101110111011101110111011101110111011101110111011101110111011101110111011101110111011101110111011101110111011101110111011101 

""" 

if capacity is None: 

bitset_init(self._bitset, 1) 

else: 

bitset_init(self._bitset, capacity) 

  

def __dealloc__(self): 

""" 

Deallocate the C bitset data structure. 

  

EXAMPLES:: 

  

sage: a = FrozenBitset('11010') 

sage: del a 

sage: b = FrozenBitset('11010' * 64) 

sage: del b 

""" 

bitset_free(self._bitset) 

  

def __init__(self, iter=None, capacity=None): 

""" 

Initialize a bitset. 

  

See the class documentation of ``FrozenBitset`` for details on the 

parameters. 

  

EXAMPLES:: 

  

sage: FrozenBitset(capacity=3) 

000 

sage: FrozenBitset('11011') 

11011 

sage: FrozenBitset('110' * 32) 

110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110 

sage: FrozenBitset([0,3,2]) 

1011 

sage: FrozenBitset(set([0,3,2])) 

1011 

sage: FrozenBitset(FrozenBitset('11011')) 

11011 

sage: FrozenBitset([0,3,2], capacity=10) 

1011000000 

sage: FrozenBitset([i for i in range(100) if i % 2 == 0]) 

101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 

  

TESTS: 

  

The capacity must be at least one:: 

  

sage: FrozenBitset() 

0 

  

If capacity is specified, it must match up with the 

initialization items:: 

  

sage: FrozenBitset('10', capacity=3) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not match passed string 

sage: FrozenBitset([0,3,2], capacity=2) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not allow storing the passed iterable 

sage: FrozenBitset(FrozenBitset('1101'), capacity=2) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not match passed bitset 

sage: FrozenBitset(FrozenBitset('1101'), capacity=5) 

Traceback (most recent call last): 

... 

ValueError: bitset capacity does not match passed bitset 

""" 

cdef unsigned long n 

cdef FrozenBitset b 

if iter is not None and not isinstance(iter, (str, tuple, list, FrozenBitset, Bitset)): 

iter = list(iter) 

  

if iter is None: 

pass # Leave bitset empty 

elif isinstance(iter, (FrozenBitset, Bitset)): 

b = iter 

if capacity is None: 

bitset_realloc(self._bitset, b._bitset.size) 

elif self._bitset.size != b._bitset.size: 

raise ValueError("bitset capacity does not match passed bitset") 

bitset_copy(self._bitset, b._bitset) 

elif isinstance(iter, str): 

if len(iter) == 0: 

raise ValueError("Bitsets must not be empty") 

if capacity is None: 

bitset_realloc(self._bitset, len(iter)) 

elif self._bitset.size != len(iter): 

raise ValueError("bitset capacity does not match passed string") 

bitset_from_str(self._bitset, iter) 

else: # an iterable 

iter = list(iter) 

if len(iter) > 0: 

need_capacity = max(iter) + 1 

else: 

need_capacity = 0 

if capacity is None: 

if need_capacity == 0: 

raise ValueError("Bitsets must not be empty") 

bitset_realloc(self._bitset, need_capacity) 

elif self._bitset.size < need_capacity: 

raise ValueError("bitset capacity does not allow storing the passed iterable") 

bitset_clear(self._bitset) 

for n in iter: 

bitset_add(self._bitset, n) 

  

cdef FrozenBitset _new(self, long int capacity): 

r""" 

Return an object of the same type as ``self``, initialized with a 

bitset of capacity ``capacity``. 

""" 

cdef FrozenBitset b 

b = FrozenBitset.__new__(FrozenBitset, None, capacity) 

return b 

  

def __getstate__(self): 

""" 

Return the current state of the object as a string. 

  

EXAMPLES:: 

  

sage: FrozenBitset('1101').__getstate__() 

'1101' 

sage: FrozenBitset('110'*32).__getstate__() 

'110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110' 

""" 

return str(self) 

  

def __setstate__(self, state): 

""" 

Set the state of the object from the string state. 

  

EXAMPLES:: 

  

sage: a = FrozenBitset() 

sage: a.__setstate__('1101') 

sage: a 

1101 

sage: a.__setstate__('110'*32) 

sage: a 

110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110 

""" 

bitset_realloc(self._bitset, len(state)) 

bitset_from_str(self._bitset, state) 

  

def __iter__(self): 

""" 

Return an iterator over ``self``. 

  

EXAMPLES:: 

  

sage: list(FrozenBitset('11011')) 

[0, 1, 3, 4] 

sage: list(FrozenBitset('00001' * 20)) 

[4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79, 84, 89, 94, 99] 

sage: set(FrozenBitset('11011')) 

{0, 1, 3, 4} 

""" 

return iter(bitset_list(self._bitset)) 

  

cpdef FrozenBitset _larger_capacity_(self, long capacity): 

""" 

Return a copy of ``self`` where the bitset has the maximum of the 

current capacity and the capacity passed. If no resizing is needed, 

return ``self``. 

  

This function is mainly used to satisfy the assumption of the 

underlying bitset functions that all bitsets are of the same 

capacity. 

  

INPUT: 

  

- ``capacity`` -- the underlying bitset of the returned bitset 

will have this capacity if it is bigger than the current 

capacity. 

  

EXAMPLES:: 

  

sage: a = FrozenBitset('11010') 

sage: a.capacity() 

5 

sage: a._larger_capacity_(4) is a 

True 

sage: a._larger_capacity_(5) is a 

True 

sage: b = a._larger_capacity_(6) 

sage: b 

110100 

sage: b is a 

False 

sage: b.capacity() 

6 

sage: c = a._larger_capacity_(98) 

sage: c 

11010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

sage: c.capacity() 

98 

""" 

cdef FrozenBitset temp 

if self._bitset.size >= capacity: 

return self 

else: 

temp = self._new(self._bitset.size) 

bitset_copy(temp._bitset, self._bitset) 

bitset_realloc(temp._bitset, capacity) 

return temp 

  

cpdef long capacity(self): 

""" 

Return the size of the underlying bitset. 

  

The maximum value that can be stored in the current underlying 

bitset is ``self.capacity() - 1``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('11000').capacity() 

5 

sage: FrozenBitset('110' * 32).capacity() 

96 

sage: FrozenBitset(range(20), capacity=450).capacity() 

450 

""" 

return self._bitset.size 

  

def __hash__(self): 

""" 

Return a hash value for a bitset. 

  

EXAMPLES:: 

  

sage: hash(FrozenBitset(capacity=5)) 

0 

sage: hash(FrozenBitset('10110')) 

13 

sage: hash(FrozenBitset('10110' + '0' * 120, capacity=125)) 

13 

""" 

cdef long hash 

hash = bitset_hash(self._bitset) 

if hash == -1: 

return 0 

else: 

return hash 

  

cpdef bint isempty(self): 

""" 

Test if the bitset is empty. 

  

INPUT: 

  

- None. 

  

OUTPUT: 

  

- ``True`` if the bitset is empty; ``False`` otherwise. 

  

EXAMPLES:: 

  

sage: FrozenBitset().isempty() 

True 

sage: FrozenBitset([1]).isempty() 

False 

sage: FrozenBitset([], capacity=110).isempty() 

True 

sage: FrozenBitset(range(99)).isempty() 

False 

""" 

return bitset_isempty(self._bitset) 

  

def __richcmp__(FrozenBitset self, FrozenBitset other not None, int op): 

""" 

Implement comparisons, using the Cython richcmp convention. 

Comparison is done by inclusion. That is, a set ``A`` is less than 

another set ``B``, written ``A < B``, if ``A`` is a subset of ``B``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('11') < FrozenBitset('101') 

False 

sage: FrozenBitset('11') <= FrozenBitset('110') 

True 

sage: FrozenBitset('11') != FrozenBitset('10') 

True 

sage: FrozenBitset('11') == FrozenBitset('10') 

False 

sage: FrozenBitset('11') == FrozenBitset('110') 

True 

sage: FrozenBitset('11') > FrozenBitset('10') 

True 

sage: FrozenBitset('11') >= FrozenBitset('10') 

True 

sage: FrozenBitset('11') < FrozenBitset('110' * 32) 

True 

  

TESTS: 

  

When performing comparison, ``other`` cannot be ``None``. :: 

  

sage: F = FrozenBitset('11') 

sage: F < None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: F <= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: F > None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: F >= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: F == None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: F != None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None < F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None <= F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None > F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None >= F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None == F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None != F 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

cdef FrozenBitset left, right 

  

if self._bitset.size == other._bitset.size: 

left = self 

right = other 

elif self._bitset.size < other._bitset.size: 

left = self._larger_capacity_(other._bitset.size) 

right = other 

else: 

left = self 

right = other._larger_capacity_(self._bitset.size) 

  

if op == Py_EQ: 

return bitset_eq(left._bitset, right._bitset) 

elif op == Py_NE: 

return not bitset_eq(left._bitset, right._bitset) 

elif op == Py_LT: 

return bitset_issubset(left._bitset, right._bitset) and not bitset_eq(left._bitset, right._bitset) 

elif op == Py_LE: 

return bitset_issubset(left._bitset, right._bitset) 

elif op == Py_GT: 

return bitset_issuperset(left._bitset, right._bitset) and not bitset_eq(left._bitset, right._bitset) 

elif op == Py_GE: 

return bitset_issuperset(left._bitset, right._bitset) 

  

cpdef bint issubset(self, FrozenBitset other) except -1: 

""" 

Test to see if ``self`` is a subset of ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('11').issubset(FrozenBitset('01')) 

False 

sage: FrozenBitset('01').issubset(FrozenBitset('11')) 

True 

sage: FrozenBitset('01').issubset(FrozenBitset('01' * 45)) 

True 

  

TESTS:: 

  

sage: FrozenBitset('11').issubset(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset left, right 

if self._bitset.size == other._bitset.size: 

left = self 

right = other 

elif self._bitset.size < other._bitset.size: 

left = self._larger_capacity_(other._bitset.size) 

right = other 

else: 

left = self 

right = other._larger_capacity_(self._bitset.size) 

  

return bitset_issubset(left._bitset, right._bitset) 

  

cpdef bint issuperset(self, FrozenBitset other) except -1: 

""" 

Test to see if ``self`` is a superset of ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('11').issuperset(FrozenBitset('01')) 

True 

sage: FrozenBitset('01').issuperset(FrozenBitset('11')) 

False 

sage: FrozenBitset('01').issuperset(FrozenBitset('10' * 45)) 

False 

  

TESTS:: 

  

sage: FrozenBitset('11').issuperset(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset left, right 

if self._bitset.size == other._bitset.size: 

left = self 

right = other 

elif self._bitset.size < other._bitset.size: 

left = self._larger_capacity_(other._bitset.size) 

right = other 

else: 

left = self 

right = other._larger_capacity_(self._bitset.size) 

  

return bitset_issuperset(left._bitset, right._bitset) 

  

cpdef bint isdisjoint(self, FrozenBitset other) except -1: 

""" 

Test to see if ``self`` is disjoint from ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('11').isdisjoint(FrozenBitset('01')) 

False 

sage: FrozenBitset('01').isdisjoint(FrozenBitset('001')) 

True 

sage: FrozenBitset('00101').isdisjoint(FrozenBitset('110' * 35)) 

False 

  

TESTS:: 

  

sage: FrozenBitset('11').isdisjoint(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

cdef bint retval 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset smaller, larger 

cdef bitset_t temp 

if self._bitset.size == other._bitset.size: 

bitset_init(temp, self._bitset.size) 

bitset_intersection(temp, self._bitset, other._bitset) 

retval = bitset_isempty(temp) 

bitset_free(temp) 

return retval 

elif self._bitset.size < other._bitset.size: 

smaller = self 

larger = other 

else: 

smaller = other 

larger = self 

  

bitset_init(temp, smaller._bitset.size) 

bitset_copy(temp, smaller._bitset) 

bitset_realloc(temp, larger._bitset.size) 

bitset_intersection(temp, temp, larger._bitset) 

retval = bitset_isempty(temp) 

bitset_free(temp) 

return retval 

  

def __contains__(self, unsigned long n): 

""" 

Test to see if ``n`` is in ``self``. 

  

EXAMPLES:: 

  

sage: 0 in FrozenBitset([0,1]) 

True 

sage: 0 in FrozenBitset([1,2]) 

False 

sage: 10 in FrozenBitset([0,1]) 

False 

sage: 121 in FrozenBitset('110' * 50) 

True 

sage: 122 in FrozenBitset('110' * 50) 

False 

  

TESTS:: 

  

sage: None in FrozenBitset([0,1]) 

Traceback (most recent call last): 

... 

TypeError: an integer is required 

""" 

if n < self._bitset.size: 

return bitset_in(self._bitset, n) 

else: 

return False 

  

def __len__(self): 

""" 

Return the number of elements in the bitset (which may be 

different from the capacity of the bitset). 

  

EXAMPLES:: 

  

sage: len(FrozenBitset([0,1], capacity=10)) 

2 

sage: len(FrozenBitset(range(98))) 

98 

""" 

return bitset_len(self._bitset) 

  

def __str__(self): 

""" 

Return a string representing the bitset as a binary vector. 

  

EXAMPLES:: 

  

sage: a = FrozenBitset('10110') 

sage: str(a) 

'10110' 

sage: str(FrozenBitset('110' * 32)) 

'110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110' 

""" 

return bitset_string(self._bitset) 

  

def __repr__(self): 

""" 

Return a string representing the bitset as a binary vector. 

  

EXAMPLES:: 

  

sage: a = FrozenBitset('10110') 

sage: repr(a) 

'10110' 

sage: repr(FrozenBitset('110' * 32)) 

'110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110110' 

""" 

return str(self) 

  

cpdef _union(self, FrozenBitset other): 

""" 

Return the union of ``self`` and ``other``. 

  

In order to get a Cython "union" function, we have to use the 

underscore since "union" is a C keyword. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101')._union(FrozenBitset('11100')) 

11101 

sage: FrozenBitset('10101' * 10)._union(FrozenBitset('01010' * 10)) 

11111111111111111111111111111111111111111111111111 

  

TESTS:: 

  

sage: set(FrozenBitset('10101' * 10)._union(FrozenBitset('01010' * 10))) == set(FrozenBitset('10101' * 10)).union(FrozenBitset('01010' * 10)) 

True 

sage: set(FrozenBitset('10101')._union(FrozenBitset('01010' * 20))) == set(FrozenBitset('10101')).union(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20)._union(FrozenBitset('01010'))) == set(FrozenBitset('10101' * 20)).union(FrozenBitset('01010')) 

True 

sage: FrozenBitset('10101' * 10)._union(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset temp, smaller, larger 

if self._bitset.size <= other._bitset.size: 

smaller = self 

larger = other 

else: 

smaller = other 

larger = self 

  

temp = self._new(smaller._bitset.size) 

bitset_copy(temp._bitset, smaller._bitset) 

bitset_realloc(temp._bitset, larger._bitset.size) 

bitset_union(temp._bitset, temp._bitset, larger._bitset) 

return temp 

  

def union(self, FrozenBitset other): 

""" 

Return the union of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101').union(FrozenBitset('11100')) 

11101 

sage: FrozenBitset('10101' * 10).union(FrozenBitset('01010' * 10)) 

11111111111111111111111111111111111111111111111111 

  

TESTS:: 

  

sage: set(FrozenBitset('10101' * 10).union(FrozenBitset('01010' * 10))) == set(FrozenBitset('10101' * 10)).union(FrozenBitset('01010' * 10)) 

True 

sage: set(FrozenBitset('10101').union(FrozenBitset('01010' * 20))) == set(FrozenBitset('10101')).union(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20).union(FrozenBitset('01010'))) == set(FrozenBitset('10101' * 20)).union(FrozenBitset('01010')) 

True 

sage: FrozenBitset('10101' * 10).union(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

return self._union(other) 

  

def __or__(self, FrozenBitset other not None): 

""" 

Return the union of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101') | FrozenBitset('11100') 

11101 

sage: FrozenBitset('10101' * 10) | FrozenBitset('01010' * 10) 

11111111111111111111111111111111111111111111111111 

  

TESTS:: 

  

sage: set(FrozenBitset('10101' * 10) | FrozenBitset('01010' * 10)) == set(FrozenBitset('10101' * 10)) | set(FrozenBitset('01010' * 10)) 

True 

sage: set(FrozenBitset('10101') | FrozenBitset('01010' * 20)) == set(FrozenBitset('10101')) | set(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20) | FrozenBitset('01010')) == set(FrozenBitset('10101' * 20)) | set(FrozenBitset('01010')) 

True 

sage: FrozenBitset('10101') | None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None | FrozenBitset('10101') 

Traceback (most recent call last): 

... 

AttributeError: 'NoneType' object has no attribute '_union' 

""" 

return self._union(other) 

  

cpdef intersection(self, FrozenBitset other): 

""" 

Return the intersection of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101').intersection(FrozenBitset('11100')) 

10100 

sage: FrozenBitset('11111' * 10).intersection(FrozenBitset('010101' * 10)) 

010101010101010101010101010101010101010101010101010000000000 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10).intersection(FrozenBitset('010101' * 10))) == set(FrozenBitset('11111' * 10)).intersection(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5).intersection(FrozenBitset('01010' * 20))) == set(FrozenBitset('1' * 5)).intersection(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20).intersection(FrozenBitset('1' * 5))) == set(FrozenBitset('10101' * 20)).intersection(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset("101011").intersection(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset temp, smaller, larger 

if self._bitset.size <= other._bitset.size: 

smaller = self 

larger = other 

else: 

smaller = other 

larger = self 

  

temp = self._new(smaller._bitset.size) 

bitset_copy(temp._bitset, smaller._bitset) 

bitset_realloc(temp._bitset, larger._bitset.size) 

bitset_intersection(temp._bitset, temp._bitset, larger._bitset) 

return temp 

  

def __and__(self, FrozenBitset other not None): 

""" 

Return the intersection of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101') & FrozenBitset('11100') 

10100 

sage: FrozenBitset('11111' * 10) & FrozenBitset('010101' * 10) 

010101010101010101010101010101010101010101010101010000000000 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10) & FrozenBitset('010101' * 10)) == set(FrozenBitset('11111' * 10)) & set(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5) & FrozenBitset('01010' * 20)) == set(FrozenBitset('1' * 5)) & set(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20) & FrozenBitset('1' * 5)) == set(FrozenBitset('10101' * 20)) & set(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset("101011") & None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None & FrozenBitset("101011") 

Traceback (most recent call last): 

... 

AttributeError: 'NoneType' object has no attribute 'intersection' 

""" 

return self.intersection(other) 

  

cpdef difference(self, FrozenBitset other): 

""" 

Return the difference of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101').difference(FrozenBitset('11100')) 

00001 

sage: FrozenBitset('11111' * 10).difference(FrozenBitset('010101' * 10)) 

101010101010101010101010101010101010101010101010100000000000 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10).difference(FrozenBitset('010101' * 10))) == set(FrozenBitset('11111' * 10)).difference(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5).difference(FrozenBitset('01010' * 20))) == set(FrozenBitset('1' * 5)).difference(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20).difference(FrozenBitset('1' * 5))) == set(FrozenBitset('10101' * 20)).difference(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset('10101').difference(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset temp = self._new(self._bitset.size) 

bitset_copy(temp._bitset, self._bitset) 

  

if temp._bitset.size == other._bitset.size: 

bitset_difference(temp._bitset, temp._bitset, other._bitset) 

elif temp._bitset.size < other._bitset.size: 

bitset_realloc(temp._bitset, other._bitset.size) 

bitset_difference(temp._bitset, temp._bitset, other._bitset) 

else: 

bitset_difference(temp._bitset, temp._bitset, other._larger_capacity_(temp._bitset.size)._bitset) 

  

return temp 

  

def __sub__(self, FrozenBitset other not None): 

""" 

Return the difference of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101') - FrozenBitset('11100') 

00001 

sage: FrozenBitset('11111' * 10)-FrozenBitset('010101' * 10) 

101010101010101010101010101010101010101010101010100000000000 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10)-FrozenBitset('010101' * 10)) == set(FrozenBitset('11111' * 10))-set(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5)-FrozenBitset('01010' * 20)) == set(FrozenBitset('1' * 5))-set(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20)-FrozenBitset('1' * 5)) == set(FrozenBitset('10101' * 20))-set(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset('10101') - None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None - FrozenBitset('10101') 

Traceback (most recent call last): 

... 

AttributeError: 'NoneType' object has no attribute 'difference' 

""" 

return self.difference(other) 

  

cpdef symmetric_difference(self, FrozenBitset other): 

""" 

Return the symmetric difference of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101').symmetric_difference(FrozenBitset('11100')) 

01001 

sage: FrozenBitset('11111' * 10).symmetric_difference(FrozenBitset('010101' * 10)) 

101010101010101010101010101010101010101010101010100101010101 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10).symmetric_difference(FrozenBitset('010101' * 10))) == set(FrozenBitset('11111' * 10)).symmetric_difference(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5).symmetric_difference(FrozenBitset('01010' * 20))) == set(FrozenBitset('1' * 5)).symmetric_difference(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20).symmetric_difference(FrozenBitset('1' * 5))) == set(FrozenBitset('10101' * 20)).symmetric_difference(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset('11111' * 10).symmetric_difference(None) 

Traceback (most recent call last): 

... 

ValueError: other cannot be None 

""" 

if other is None: 

raise ValueError("other cannot be None") 

cdef FrozenBitset temp, smaller, larger 

if self._bitset.size <= other._bitset.size: 

smaller = self 

larger = other 

else: 

smaller = other 

larger = self 

  

temp = self._new(smaller._bitset.size) 

bitset_copy(temp._bitset, smaller._bitset) 

bitset_realloc(temp._bitset, larger._bitset.size) 

bitset_symmetric_difference(temp._bitset, temp._bitset, larger._bitset) 

return temp 

  

def __xor__(self, FrozenBitset other not None): 

""" 

Return the symmetric difference of ``self`` and ``other``. 

  

Note that because of the Sage preprocessor, in Sage, ``^^`` is the 

exclusive or, rather than ``^``. 

  

EXAMPLES:: 

  

sage: FrozenBitset('10101') ^^ FrozenBitset('11100') 

01001 

sage: FrozenBitset('11111' * 10) ^^ FrozenBitset('010101' * 10) 

101010101010101010101010101010101010101010101010100101010101 

  

TESTS:: 

  

sage: set(FrozenBitset('11111' * 10) ^^ FrozenBitset('010101' * 10)) == set(FrozenBitset('11111' * 10)) ^^ set(FrozenBitset('010101' * 10)) 

True 

sage: set(FrozenBitset('1' * 5) ^^ FrozenBitset('01010' * 20)) == set(FrozenBitset('1' * 5)) ^^ set(FrozenBitset('01010' * 20)) 

True 

sage: set(FrozenBitset('10101' * 20) ^^ FrozenBitset('1' * 5)) == set(FrozenBitset('10101' * 20)) ^^ set(FrozenBitset('1' * 5)) 

True 

sage: FrozenBitset('11111' * 10) ^^ None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None ^^ FrozenBitset('11111' * 10) 

Traceback (most recent call last): 

... 

AttributeError: 'NoneType' object has no attribute 'symmetric_difference' 

""" 

return self.symmetric_difference(other) 

  

cpdef complement(self): 

""" 

Return the complement of self. 

  

EXAMPLES:: 

  

sage: ~FrozenBitset('10101') 

01010 

sage: ~FrozenBitset('11111'*10) 

00000000000000000000000000000000000000000000000000 

sage: x = FrozenBitset('10'*40) 

sage: x == ~x 

False 

sage: x == ~~x 

True 

sage: x|(~x) == FrozenBitset('11'*40) 

True 

sage: ~x == FrozenBitset('01'*40) 

True 

""" 

cdef FrozenBitset temp = self._new(self._bitset.size) 

bitset_complement(temp._bitset, self._bitset) 

return temp 

  

def __invert__(self): 

""" 

Return the complement of self. 

  

EXAMPLES:: 

  

sage: ~FrozenBitset('10101') 

01010 

sage: ~FrozenBitset('11111'*10) 

00000000000000000000000000000000000000000000000000 

sage: x = FrozenBitset('10'*40) 

sage: x == ~x 

False 

sage: x == ~~x 

True 

sage: x|(~x) == FrozenBitset('11'*40) 

True 

sage: ~x == FrozenBitset('01'*40) 

True 

""" 

return self.complement() 

  

cpdef __copy__(self): 

""" 

Return ``self`` (since ``self`` is immutable). 

  

EXAMPLES:: 

  

sage: a = FrozenBitset('10101') 

sage: from copy import copy 

sage: b = copy(a) 

sage: b is a 

True 

sage: c = FrozenBitset('1010' * 32) 

sage: d = copy(c) 

sage: d is c 

True 

""" 

return self 

  

cdef class Bitset(FrozenBitset): 

r""" 

A bitset class which leverages inline Cython functions for creating 

and manipulating bitsets. See the class documentation of 

:class:`FrozenBitset` for details on the parameters of the constructor 

and how to interpret the string representation of a :class:`Bitset`. 

  

A bitset can be thought of in two ways. First, as a set of elements 

from the universe of the `n` natural numbers `0, 1, \dots, n-1` (where 

the capacity `n` can be specified), with typical set operations such as 

intersection, union, symmetric difference, etc. Secondly, a bitset can 

be thought of as a binary vector with typical binary operations such as 

``and``, ``or``, ``xor``, etc. This class supports both interfaces. 

  

The interface in this class mirrors the interface in the ``set`` 

data type of Python. 

  

.. warning:: 

  

This class is most likely to be useful as a way to store 

Cython bitsets in Python data structures, acting on them using 

the Cython inline functions. If you want to use this class 

for a Python set type, the Python ``set`` data type may be 

faster. 

  

.. SEEALSO:: 

  

- :class:`FrozenBitset` 

- Python's `set types <http://docs.python.org/library/stdtypes.html#set-types-set-frozenset>`_ 

  

EXAMPLES:: 

  

sage: a = Bitset('1101') 

sage: loads(dumps(a)) == a 

True 

sage: a = Bitset('1101' * 32) 

sage: loads(dumps(a)) == a 

True 

""" 

  

cpdef __copy__(self): 

""" 

Return a copy of ``self``. 

  

EXAMPLES:: 

  

sage: a = Bitset('10101') 

sage: from copy import copy 

sage: b = copy(a) 

sage: b is a 

False 

sage: b == a 

True 

sage: c = Bitset('1010' * 32) 

sage: d = copy(c) 

sage: d is c 

False 

sage: d == c 

True 

""" 

cdef FrozenBitset temp = self._new(self._bitset.size) 

bitset_copy(temp._bitset, self._bitset) 

return temp 

  

def __hash__(self): 

""" 

Raise an error, since mutable ``Bitset``s are not hashable. 

  

EXAMPLES:: 

  

sage: hash(Bitset('110')) 

Traceback (most recent call last): 

... 

TypeError: Bitset objects are unhashable; use FrozenBitset 

""" 

raise TypeError("Bitset objects are unhashable; use FrozenBitset") 

  

def __richcmp__(FrozenBitset self, FrozenBitset other not None, int op): 

""" 

Implement comparisons, using the Cython richcmp convention. 

Comparison is done by inclusion. That is, a set ``A`` is less than 

another set ``B``, written ``A < B``, if ``A`` is a subset of ``B``. 

  

EXAMPLES:: 

  

sage: Bitset('11') < Bitset('101') 

False 

sage: Bitset('11') <= Bitset('110') 

True 

sage: Bitset('11') != Bitset('10') 

True 

sage: Bitset('11') == Bitset('10') 

False 

sage: Bitset('11') == Bitset('110') 

True 

sage: Bitset('11') > Bitset('10') 

True 

sage: Bitset('11') >= Bitset('10') 

True 

sage: FrozenBitset('11') < FrozenBitset('110' * 32) 

True 

  

TESTS: 

  

During comparison, ``other`` cannot be ``None``. :: 

  

sage: Bitset('11') < None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: Bitset('11') <= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: Bitset('11') > None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: Bitset('11') >= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: Bitset('11') == None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: Bitset('11') != None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None < Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None <= Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None > Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None >= Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None == Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

sage: None != Bitset('11') 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

cdef FrozenBitset left, right 

  

if self._bitset.size == other._bitset.size: 

left = self 

right = other 

elif self._bitset.size < other._bitset.size: 

left = self._larger_capacity_(other._bitset.size) 

right = other 

else: 

left = self 

right = other._larger_capacity_(self._bitset.size) 

  

if op == Py_EQ: 

return bitset_eq(left._bitset, right._bitset) 

elif op == Py_NE: 

return not bitset_eq(left._bitset, right._bitset) 

elif op == Py_LT: 

return bitset_issubset(left._bitset, right._bitset) and not bitset_eq(left._bitset, right._bitset) 

elif op == Py_LE: 

return bitset_issubset(left._bitset, right._bitset) 

elif op == Py_GT: 

return bitset_issuperset(left._bitset, right._bitset) and not bitset_eq(left._bitset, right._bitset) 

elif op == Py_GE: 

return bitset_issuperset(left._bitset, right._bitset) 

  

cdef FrozenBitset _new(self, long int capacity): 

""" 

Return an object of the same type as ``self``, initialized with a 

bitset of capacity ``capacity``. 

""" 

cdef Bitset b 

b = Bitset.__new__(Bitset, None, capacity) 

return b 

  

cpdef update(self, FrozenBitset other): 

""" 

Update the bitset to include items in ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.update(Bitset('0101')) 

sage: a 

1101 

sage: a_set = set(a) 

sage: a.update(Bitset('00011' * 25)) 

sage: a 

11011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011 

sage: a_set.update(Bitset('00011' * 25)) 

sage: set(a) == a_set 

True 

  

TESTS: 

  

During update, ``other`` cannot be ``None``. :: 

  

sage: a = Bitset('1101') 

sage: a.update(None) 

Traceback (most recent call last): 

... 

TypeError: other cannot be None 

""" 

if other is None: 

raise TypeError("other cannot be None") 

cdef bitset_t temp 

if self._bitset.size == other._bitset.size: 

bitset_union(self._bitset, self._bitset, other._bitset) 

elif self._bitset.size < other._bitset.size: 

bitset_realloc(self._bitset, other._bitset.size) 

bitset_union(self._bitset, self._bitset, other._bitset) 

else: 

bitset_init(temp, other._bitset.size) 

bitset_copy(temp, other._bitset) 

bitset_realloc(temp, self._bitset.size) 

bitset_union(self._bitset, self._bitset, temp) 

bitset_free(temp) 

  

def __ior__(self, FrozenBitset other not None): 

""" 

Update the bitset to include items in ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a |= Bitset('0101') 

sage: a 

1101 

sage: a_set = set(a) 

sage: a |= Bitset('00011' * 25) 

sage: a 

11011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011000110001100011 

sage: a_set |= set(Bitset('00011' * 25)) 

sage: set(a) == a_set 

True 

  

TESTS:: 

  

sage: a = Bitset('110') 

sage: a |= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

self.update(other) 

return self 

  

cpdef intersection_update(self, FrozenBitset other): 

""" 

Update the bitset to the intersection of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.intersection_update(Bitset('0101')) 

sage: a 

0100 

sage: a_set = set(a) 

sage: a.intersection_update(Bitset('0110' * 25)) 

sage: a 

0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

sage: a_set.intersection_update(Bitset('0110' * 25)) 

sage: set(a) == a_set 

True 

  

TESTS:: 

  

sage: Bitset('110').intersection_update(None) 

Traceback (most recent call last): 

... 

TypeError: other cannot be None 

""" 

if other is None: 

raise TypeError("other cannot be None") 

cdef bitset_t temp 

if self._bitset.size == other._bitset.size: 

bitset_intersection(self._bitset, self._bitset, other._bitset) 

elif self._bitset.size < other._bitset.size: 

bitset_realloc(self._bitset, other._bitset.size) 

bitset_intersection(self._bitset, self._bitset, other._bitset) 

else: 

bitset_init(temp, other._bitset.size) 

bitset_copy(temp, other._bitset) 

bitset_realloc(temp, self._bitset.size) 

bitset_intersection(self._bitset, self._bitset, temp) 

bitset_free(temp) 

  

def __iand__(self, FrozenBitset other not None): 

""" 

Update the bitset to the intersection of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a &= Bitset('0101') 

sage: a 

0100 

sage: a_set = set(a) 

sage: a &= Bitset('0110' * 25) 

sage: a 

0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

sage: a_set &= set(Bitset('0110' * 25)) 

sage: a_set == set(a) 

True 

  

TESTS:: 

  

sage: a = Bitset('110') 

sage: a &= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

self.intersection_update(other) 

return self 

  

cpdef difference_update(self, FrozenBitset other): 

""" 

Update the bitset to the difference of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.difference_update(Bitset('0101')) 

sage: a 

1000 

sage: a_set = set(a) 

sage: a.difference_update(FrozenBitset('010101' * 10)); a 

100000000000000000000000000000000000000000000000000000000000 

sage: a_set.difference_update(FrozenBitset('010101' * 10)) 

sage: a_set == set(a) 

True 

sage: a.difference_update(FrozenBitset('110')) 

sage: a_set.difference_update(FrozenBitset('110')) 

sage: a_set == set(a) 

True 

sage: a.difference_update(FrozenBitset('01010' * 20)); a 

0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

sage: a_set.difference_update(FrozenBitset('01010' * 20)) 

sage: a_set == set(a) 

True 

sage: b = Bitset('10101' * 20) 

sage: b_set = set(b) 

sage: b.difference_update(FrozenBitset('1' * 5)); b 

0000010101101011010110101101011010110101101011010110101101011010110101101011010110101101011010110101 

sage: b_set.difference_update(FrozenBitset('1' * 5)) 

sage: b_set == set(b) 

True 

  

TESTS:: 

  

sage: Bitset('110').difference_update(None) 

Traceback (most recent call last): 

... 

TypeError: other cannot be None 

""" 

if other is None: 

raise TypeError("other cannot be None") 

cdef bitset_t temp 

if self._bitset.size == other._bitset.size: 

bitset_difference(self._bitset, self._bitset, other._bitset) 

elif self._bitset.size < other._bitset.size: 

bitset_realloc(self._bitset, other._bitset.size) 

bitset_difference(self._bitset, self._bitset, other._bitset) 

else: 

bitset_init(temp, other._bitset.size) 

bitset_copy(temp, other._bitset) 

bitset_realloc(temp, self._bitset.size) 

bitset_difference(self._bitset, self._bitset, temp) 

bitset_free(temp) 

  

def __isub__(self, FrozenBitset other not None): 

""" 

Update the bitset to the difference of ``self`` and ``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a -= Bitset('0101') 

sage: a 

1000 

sage: a_set = set(a) 

sage: a -= FrozenBitset('010101' * 10); a 

100000000000000000000000000000000000000000000000000000000000 

sage: a_set -= set(FrozenBitset('010101' * 10)) 

sage: a_set == set(a) 

True 

sage: a = Bitset('110') 

sage: a_set = set(a) 

sage: a -= FrozenBitset('01010' * 20); a 

1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

sage: a_set -= set(FrozenBitset('01010' * 20)) 

sage: a_set == set(a) 

True 

sage: b = FrozenBitset('10101' * 20) 

sage: b_set = set(b) 

sage: b -= FrozenBitset('1' * 5); b 

0000010101101011010110101101011010110101101011010110101101011010110101101011010110101101011010110101 

sage: b_set -= FrozenBitset('1' * 5) 

sage: b_set == set(b) 

True 

  

TESTS:: 

  

sage: a = Bitset('110') 

sage: a -= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

self.difference_update(other) 

return self 

  

cpdef symmetric_difference_update(self, FrozenBitset other): 

""" 

Update the bitset to the symmetric difference of ``self`` and 

``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.symmetric_difference_update(Bitset('0101')) 

sage: a 

1001 

sage: a_set = set(a) 

sage: a.symmetric_difference_update(FrozenBitset('010101' * 10)); a 

110001010101010101010101010101010101010101010101010101010101 

sage: a_set.symmetric_difference_update(FrozenBitset('010101' * 10)) 

sage: a_set == set(a) 

True 

sage: a.symmetric_difference_update(FrozenBitset('01010' * 20)); a 

1001011111000001111100000111110000011111000001111100000111110101001010010100101001010010100101001010 

sage: a_set.symmetric_difference_update(FrozenBitset('01010' * 20)) 

sage: a_set == set(a) 

True 

sage: b = Bitset('10101' * 20) 

sage: b_set = set(b) 

sage: b.symmetric_difference_update( FrozenBitset('1' * 5)); b 

0101010101101011010110101101011010110101101011010110101101011010110101101011010110101101011010110101 

sage: b_set.symmetric_difference_update( FrozenBitset('1' * 5)) 

sage: b_set == set(b) 

True 

  

TESTS:: 

  

sage: Bitset('110').symmetric_difference_update(None) 

Traceback (most recent call last): 

... 

TypeError: other cannot be None 

""" 

if other is None: 

raise TypeError("other cannot be None") 

cdef bitset_t temp 

if self._bitset.size == other._bitset.size: 

bitset_symmetric_difference(self._bitset, self._bitset, other._bitset) 

elif self._bitset.size < other._bitset.size: 

bitset_realloc(self._bitset, other._bitset.size) 

bitset_symmetric_difference(self._bitset, self._bitset, other._bitset) 

else: 

bitset_init(temp, other._bitset.size) 

bitset_copy(temp, other._bitset) 

bitset_realloc(temp, self._bitset.size) 

bitset_symmetric_difference(self._bitset, self._bitset, temp) 

bitset_free(temp) 

  

def __ixor__(self, FrozenBitset other not None): 

""" 

Update the bitset to the symmetric difference of ``self`` and 

``other``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a ^^=Bitset('0101') 

sage: a 

1001 

sage: a_set = set(a) 

sage: a ^^= FrozenBitset('010101' * 10); a 

110001010101010101010101010101010101010101010101010101010101 

sage: a_set ^^= set(FrozenBitset('010101' * 10)) 

sage: a_set == set(a) 

True 

sage: a ^^= FrozenBitset('01010' * 20); a 

1001011111000001111100000111110000011111000001111100000111110101001010010100101001010010100101001010 

sage: a_set ^^= set(FrozenBitset('01010' * 20)) 

sage: a_set == set(a) 

True 

sage: b = Bitset('10101' * 20) 

sage: b_set = set(b) 

sage: b ^^= FrozenBitset('1' * 5); b 

0101010101101011010110101101011010110101101011010110101101011010110101101011010110101101011010110101 

sage: b_set ^^= set(FrozenBitset('1' * 5)) 

sage: b_set == set(b) 

True 

  

TESTS:: 

  

sage: a = Bitset('110') 

sage: a ^^= None 

Traceback (most recent call last): 

... 

TypeError: Argument 'other' has incorrect type (expected sage.data_structures.bitset.FrozenBitset, got NoneType) 

""" 

self.symmetric_difference_update(other) 

return self 

  

cpdef add(self, unsigned long n): 

""" 

Update the bitset by adding ``n``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.add(5) 

sage: a 

110001 

sage: a.add(100) 

sage: sorted(list(a)) 

[0, 1, 5, 100] 

sage: a.capacity() 

101 

  

TESTS: 

  

The input ``n`` must be an integer. :: 

  

sage: Bitset('110').add(None) 

Traceback (most recent call last): 

... 

TypeError: an integer is required 

""" 

if n >= self._bitset.size: 

bitset_realloc(self._bitset, n + 1) 

bitset_add(self._bitset, n) 

  

cpdef remove(self, unsigned long n): 

""" 

Update the bitset by removing ``n``. Raises ``KeyError`` if ``n`` is 

not contained in the bitset. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.remove(1) 

sage: a 

100 

sage: a.remove(2) 

Traceback (most recent call last): 

... 

KeyError: 2L 

sage: a.remove(4) 

Traceback (most recent call last): 

... 

KeyError: 4L 

sage: a 

100 

sage: a = Bitset('000001' * 15); sorted(list(a)) 

[5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83, 89] 

sage: a.remove(83); sorted(list(a)) 

[5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 89] 

  

TESTS: 

  

The input ``n`` must be an integer. :: 

  

sage: Bitset('110').remove(None) 

Traceback (most recent call last): 

... 

TypeError: an integer is required 

""" 

if n >= self._bitset.size: 

raise KeyError(n) 

else: 

bitset_remove(self._bitset, n) 

  

cpdef discard(self, unsigned long n): 

""" 

Update the bitset by removing ``n``. 

  

EXAMPLES:: 

  

sage: a = Bitset('110') 

sage: a.discard(1) 

sage: a 

100 

sage: a.discard(2) 

sage: a.discard(4) 

sage: a 

100 

sage: a = Bitset('000001' * 15); sorted(list(a)) 

[5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 83, 89] 

sage: a.discard(83); sorted(list(a)) 

[5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 89] 

sage: a.discard(82); sorted(list(a)) 

[5, 11, 17, 23, 29, 35, 41, 47, 53, 59, 65, 71, 77, 89] 

  

TESTS: 

  

The input ``n`` must be an integer. :: 

  

sage: Bitset('110').discard(None) 

Traceback (most recent call last): 

... 

TypeError: an integer is required 

""" 

if n < self._bitset.size: 

bitset_discard(self._bitset, n) 

  

cpdef pop(self): 

""" 

Remove and return an arbitrary element from the set. Raises 

``KeyError`` if the set is empty. 

  

EXAMPLES:: 

  

sage: a = Bitset('011') 

sage: a.pop() 

1 

sage: a 

001 

sage: a.pop() 

2 

sage: a 

000 

sage: a.pop() 

Traceback (most recent call last): 

... 

KeyError: 'pop from an empty set' 

sage: a = Bitset('0001'*32) 

sage: a.pop() 

3 

sage: [a.pop() for _ in range(20)] 

[7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, 83] 

""" 

return bitset_pop(self._bitset) 

  

cpdef clear(self): 

""" 

Removes all elements from the bitset. 

  

EXAMPLES:: 

  

sage: a = Bitset('011') 

sage: a.clear() 

sage: a 

000 

sage: a = Bitset('011' * 32) 

sage: a.clear() 

sage: set(a) 

set() 

""" 

bitset_clear(self._bitset) 

  

  

############################################################################# 

# Bitset Testing: test basic Cython bitsets 

############################################################################# 

  

def test_bitset(py_a, py_b, long n): 

""" 

Test the Cython bitset functions so we can have some relevant doctests. 

  

TESTS:: 

  

sage: from sage.data_structures.bitset import test_bitset 

sage: test_bitset('00101', '01110', 4) 

a 00101 

list a [2, 4] 

a.size 5 

len(a) 2 

a.limbs 1 

b 01110 

a.in(n) True 

a.not_in(n) False 

a.add(n) 00101 

a.discard(n) 00100 

a.set_to(n) 00101 

a.flip(n) 00100 

a.set_first_n(n) 11110 

a.first_in_complement() 4 

a.isempty() False 

a.eq(b) False 

a.cmp(b) 1 

a.lex_cmp(b) -1 

a.issubset(b) False 

a.issuperset(b) False 

a.copy() 00101 

r.clear() 00000 

complement a 11010 

a intersect b 00100 

a union b 01111 

a minus b 00001 

a symmetric_difference b 01011 

a.rshift(n) 10000 

a.lshift(n) 00000 

a.first() 2 

a.next(n) 4 

a.first_diff(b) 1 

a.next_diff(b, n) 4 

a.hamming_weight() 2 

a.map(m) 10100 

a == loads(dumps(a)) True 

reallocating a 00101 

to size 4 0010 

to size 8 00100000 

to original size 00100 

  

:: 

  

sage: test_bitset('11101', '11001', 2) 

a 11101 

list a [0, 1, 2, 4] 

a.size 5 

len(a) 4 

a.limbs 1 

b 11001 

a.in(n) True 

a.not_in(n) False 

a.add(n) 11101 

a.discard(n) 11001 

a.set_to(n) 11101 

a.flip(n) 11001 

a.set_first_n(n) 11000 

a.first_in_complement() 2 

a.isempty() False 

a.eq(b) False 

a.cmp(b) 1 

a.lex_cmp(b) 1 

a.issubset(b) False 

a.issuperset(b) True 

a.copy() 11101 

r.clear() 00000 

complement a 00010 

a intersect b 11001 

a union b 11101 

a minus b 00100 

a symmetric_difference b 00100 

a.rshift(n) 10100 

a.lshift(n) 00111 

a.first() 0 

a.next(n) 2 

a.first_diff(b) 2 

a.next_diff(b, n) 2 

a.hamming_weight() 4 

a.map(m) 10111 

a == loads(dumps(a)) True 

reallocating a 11101 

to size 2 11 

to size 4 1100 

to original size 11000 

  

Test a corner-case: a bitset that is a multiple of words:: 

  

sage: test_bitset('00'*64, '01'*64, 127) 

a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

list a [] 

a.size 128 

len(a) 0 

a.limbs ... 

b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 

a.in(n) False 

a.not_in(n) True 

a.add(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 

a.discard(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a.set_to(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 

a.flip(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 

a.set_first_n(n) 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110 

a.first_in_complement() 127 

a.isempty() True 

a.eq(b) False 

a.cmp(b) -1 

a.lex_cmp(b) -1 

a.issubset(b) True 

a.issuperset(b) False 

a.copy() 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

r.clear() 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

complement a 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 

a intersect b 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a union b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 

a minus b 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a symmetric_difference b 01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101 

a.rshift(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a.lshift(n) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a.first() -1 

a.next(n) -1 

a.first_diff(b) 1 

a.next_diff(b, n) 127 

a.hamming_weight() 0 

a.map(m) 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a == loads(dumps(a)) True 

rshifts add True 

lshifts add True 

intersection commutes True 

union commutes True 

not not = id True 

flipped bit 127 

add bit 127 

discard bit 127 

lshift add unset ok True 

rshift set unset ok True 

reallocating a 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

to size 127 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

to size 254 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

to original size 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

  

Large enough to span multiple limbs. We don't explicitly check the number of limbs below because it will be different in the 32 bit versus 64 bit cases:: 

  

sage: test_bitset('111001'*25, RealField(151)(pi).str(2)[2:], 69) 

a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001 

list a [0, 1, 2, 5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20, 23, 24, 25, 26, 29, 30, 31, 32, 35, 36, 37, 38, 41, 42, 43, 44, 47, 48, 49, 50, 53, 54, 55, 56, 59, 60, 61, 62, 65, 66, 67, 68, 71, 72, 73, 74, 77, 78, 79, 80, 83, 84, 85, 86, 89, 90, 91, 92, 95, 96, 97, 98, 101, 102, 103, 104, 107, 108, 109, 110, 113, 114, 115, 116, 119, 120, 121, 122, 125, 126, 127, 128, 131, 132, 133, 134, 137, 138, 139, 140, 143, 144, 145, 146, 149] 

a.size 150 

len(a) 100 

a.limbs ... 

b 000100100001111110110101010001000100001011010001100001000110100110001001100011001100010100010111000000011011100000111001101000100101001000000100100111 

a.in(n) False 

a.not_in(n) True 

a.add(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001 

a.discard(n) 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001 

a.set_to(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001 

a.flip(n) 111001111001111001111001111001111001111001111001111001111001111001111101111001111001111001111001111001111001111001111001111001111001111001111001111001 

a.set_first_n(n) 111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000 

a.first_in_complement() 69 

a.isempty() False 

a.eq(b) False 

a.cmp(b) -1 

a.lex_cmp(b) 1 

a.issubset(b) False 

a.issuperset(b) False 

a.copy() 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001 

r.clear() 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

complement a 000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110000110 

a intersect b 000000100001111000110001010001000000001001010001100001000000100000001001100001001000010000010001000000011001100000111001101000100001001000000000100001 

a union b 111101111001111111111101111001111101111011111001111001111111111111111001111011111101111101111111111001111011111001111001111001111101111001111101111111 

a minus b 111001011000000001001000101000111001110000101000011000111001011001110000011000110001101001101000111001100000011001000000010001011000110001111001011000 

a symmetric_difference b 111101011000000111001100101000111101110010101000011000111111011111110000011010110101101101101110111001100010011001000000010001011100110001111101011110 

a.rshift(n) 001111001111001111001111001111001111001111001111001111001111001111001111001111001000000000000000000000000000000000000000000000000000000000000000000000 

a.lshift(n) 000000000000000000000000000000000000000000000000000000000000000000000111001111001111001111001111001111001111001111001111001111001111001111001111001111 

a.first() 0 

a.next(n) 71 

a.first_diff(b) 0 

a.next_diff(b, n) 73 

a.hamming_weight() 100 

a.map(m) 100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111100111 

a == loads(dumps(a)) True 

rshifts add True 

lshifts add True 

intersection commutes True 

union commutes True 

not not = id True 

flipped bit 69 

add bit 69 

discard bit 69 

lshift add unset ok True 

rshift set unset ok True 

reallocating a 111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001111001 

to size 69 111001111001111001111001111001111001111001111001111001111001111001111 

to size 138 111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000 

to original size 111001111001111001111001111001111001111001111001111001111001111001111000000000000000000000000000000000000000000000000000000000000000000000000000000000 

  

""" 

cdef bint bit = True 

cdef bitset_t a, b, r 

  

bitset_from_str(a, py_a) 

bitset_from_str(b, py_b) 

  

if a.size != b.size: 

raise ValueError("inputs must have same size") 

  

print("a", bitset_string(a)) 

print("list a", bitset_list(a)) 

print("a.size", a.size) 

print("len(a)", bitset_len(a)) 

print("a.limbs", a.limbs) 

print("b", bitset_string(b)) 

print("a.in(n) ", bitset_in(a, n)) 

print("a.not_in(n) ", bitset_not_in(a, n)) 

bitset_add(a, n) 

print("a.add(n) ", bitset_string(a)) 

bitset_from_str(a, py_a) 

bitset_discard(a, n) 

print("a.discard(n) ", bitset_string(a)) 

bitset_from_str(a, py_a) 

bitset_set_to(a, n, bit) 

print("a.set_to(n) ", bitset_string(a)) 

bitset_from_str(a, py_a) 

bitset_flip(a, n) 

print("a.flip(n) ", bitset_string(a)) 

bitset_set_first_n(a, n) 

print("a.set_first_n(n) ", bitset_string(a)) 

print("a.first_in_complement() ", bitset_first_in_complement(a)) 

  

bitset_from_str(a, py_a) 

bitset_from_str(b, py_b) 

print("a.isempty() ", bitset_isempty(a)) 

print("a.eq(b) ", bitset_eq(a, b)) 

print("a.cmp(b) ", bitset_cmp(a, b)) 

print("a.lex_cmp(b)", bitset_lex_cmp(a, b)) 

print("a.issubset(b)", bitset_issubset(a, b)) 

print("a.issuperset(b)", bitset_issuperset(a, b)) 

  

bitset_from_str(a, py_a) 

bitset_from_str(b, py_b) 

  

bitset_init(r, a.size) 

bitset_copy(r, a) 

print("a.copy() ", bitset_string(r)) 

bitset_clear(r) 

print("r.clear() ", bitset_string(r)) 

bitset_complement(r, a) 

print("complement a ", bitset_string(r)) 

bitset_intersection(r, a, b) 

print("a intersect b ", bitset_string(r)) 

bitset_union(r, a, b) 

print("a union b ", bitset_string(r)) 

bitset_difference(r, a, b) 

print("a minus b ", bitset_string(r)) 

bitset_symmetric_difference(r, a, b) 

print("a symmetric_difference b ", bitset_string(r)) 

  

bitset_rshift(r, a, n) 

print("a.rshift(n) ", bitset_string(r)) 

  

bitset_lshift(r, a, n) 

print("a.lshift(n) ", bitset_string(r)) 

  

print("a.first() ", bitset_first(a)) 

print("a.next(n) ", bitset_next(a, n)) 

print("a.first_diff(b) ", bitset_first_diff(a, b)) 

print("a.next_diff(b, n) ", bitset_next_diff(a, b, n)) 

  

print("a.hamming_weight() ", bitset_hamming_weight(a)) 

  

morphism = {} 

for i in xrange(a.size): 

morphism[i] = a.size - i - 1 

bitset_map(r, a, morphism) 

print("a.map(m) ", bitset_string(r)) 

  

data = bitset_pickle(a) 

bitset_unpickle(r, data) 

print("a == loads(dumps(a)) ", bitset_eq(r, a)) 

  

cdef bitset_t s 

bitset_init(s, a.size) 

  

if a.size > 100: 

bitset_rshift(r, b, 3) 

bitset_rshift(r, r, 77) 

bitset_rshift(s, b, 80) 

print("rshifts add ", bitset_eq(s, r)) 

  

bitset_lshift(r, b, 69) 

bitset_lshift(r, r, 6) 

bitset_lshift(s, b, 75) 

print("lshifts add ", bitset_eq(s, r)) 

  

bitset_intersection(r, a, b) 

bitset_intersection(s, b, a) 

print("intersection commutes", bitset_eq(s, r)) 

  

bitset_union(r, a, b) 

bitset_union(s, b, a) 

print("union commutes ", bitset_eq(s, r)) 

  

bitset_complement(r, b) 

bitset_complement(s, r) 

print("not not = id", bitset_eq(s, b)) 

  

bitset_copy(r, b) 

bitset_flip(r, n) 

print("flipped bit ", bitset_first_diff(b, r)) 

  

bitset_clear(r) 

bitset_add(r, n) 

print("add bit ", bitset_first(r)) 

  

bitset_clear(r) 

bitset_complement(r, r) 

bitset_discard(r, n) 

bitset_complement(r, r) 

print("discard bit ", bitset_first(r)) 

  

bitset_clear(r) 

bitset_add(r, 10) 

bitset_lshift(r, r, 68) 

bitset_flip(r, 78) 

print("lshift add unset ok", bitset_isempty(r)) 

  

bitset_clear(r) 

bitset_add(r, 19) 

bitset_rshift(r, r, 8) 

bitset_discard(r, 11) 

print("rshift set unset ok", bitset_isempty(r)) 

  

print("reallocating a ", bitset_string(a)) 

bitset_realloc(a, n) 

print("to size %d " % n, bitset_string(a)) 

bitset_realloc(a, 2 * n) 

print("to size %d " % (2 * n), bitset_string(a)) 

bitset_realloc(a, b.size) 

print("to original size ", bitset_string(a)) 

  

bitset_free(a) 

bitset_free(b) 

bitset_free(r) 

bitset_free(s) 

  

  

def test_bitset_set_first_n(py_a, long n): 

""" 

Test the bitset function set_first_n. 

  

TESTS:: 

  

sage: from sage.data_structures.bitset import test_bitset_set_first_n 

sage: test_bitset_set_first_n('00'*64, 128) 

a.set_first_n(n) 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 

  

""" 

cdef bint bit = True 

cdef bitset_t a 

  

bitset_from_str(a, py_a) 

bitset_set_first_n(a, n) 

print("a.set_first_n(n) ", bitset_string(a)) 

bitset_free(a) 

  

  

def test_bitset_remove(py_a, long n): 

""" 

Test the bitset_remove function. 

  

TESTS:: 

  

sage: from sage.data_structures.bitset import test_bitset_remove 

sage: test_bitset_remove('01', 0) 

Traceback (most recent call last): 

... 

KeyError: 0L 

sage: test_bitset_remove('01', 1) 

a 01 

a.size 2 

a.limbs 1 

n 1 

a.remove(n) 00 

""" 

cdef bitset_t a 

bitset_from_str(a, py_a) 

  

print("a", bitset_string(a)) 

print("a.size", a.size) 

print("a.limbs", a.limbs) 

print("n", n) 

  

bitset_remove(a, n) 

print("a.remove(n) ", bitset_string(a)) 

  

bitset_free(a) 

  

  

def test_bitset_pop(py_a): 

""" 

Tests for the bitset_pop function. 

  

TESTS:: 

  

sage: from sage.data_structures.bitset import test_bitset_pop 

sage: test_bitset_pop('0101') 

a.pop() 1 

new set: 0001 

sage: test_bitset_pop('0000') 

Traceback (most recent call last): 

... 

KeyError: 'pop from an empty set' 

""" 

cdef bitset_t a 

bitset_from_str(a, py_a) 

i = bitset_pop(a) 

print("a.pop() ", i) 

print("new set: ", bitset_string(a)) 

bitset_free(a) 

  

  

def test_bitset_unpickle(data): 

""" 

This (artificially) tests pickling of bitsets across systems. 

  

INPUT: 

  

- ``data`` -- A tuple of data as would be produced by the internal, Cython-only, method ``bitset_pickle``. 

  

OUTPUT: 

  

A list form of the bitset corresponding to the pickled data. 

  

EXAMPLES: 

  

We compare 64-bit and 32-bit encoding. Both should unpickle on any system:: 

  

sage: from sage.data_structures.bitset import test_bitset_unpickle 

sage: test_bitset_unpickle((0, 100, 2, 8, (33, 6001))) 

[0, 5, 64, 68, 69, 70, 72, 73, 74, 76] 

sage: test_bitset_unpickle((0, 100, 4, 4, (33, 0, 6001, 0))) 

[0, 5, 64, 68, 69, 70, 72, 73, 74, 76] 

""" 

cdef bitset_t bs 

bitset_init(bs, 1) 

bitset_unpickle(bs, data) 

L = bitset_list(bs) 

bitset_free(bs) 

return L