summary refs log tree commit diff stats
path: root/doc/sets_fragment.txt
blob: 8436190a08e3ad859313973969b83928c94e6e99 (plain) (blame)
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
The set type models the mathematical notion of a set. The set's basetype can
only be an ordinal type of a certain size, namely:

* ``int8``-``int16``
* ``uint8``/``byte``-``uint16``
* ``char``
* ``enum``

or equivalent. For signed integers the set's base type is defined to be in the
range ``0 .. MaxSetElements-1`` where ``MaxSetElements`` is currently always
2^16.

The reason is that sets are implemented as high performance bit vectors.
Attempting to declare a set with a larger type will result in an error:

.. code-block:: nim

  var s: set[int64] # Error: set is too large

Sets can be constructed via the set constructor: ``{}`` is the empty set. The
empty set is type compatible with any concrete set type. The constructor
can also be used to include elements (and ranges of elements):

.. code-block:: nim
  type
    CharSet = set[char]
  var
    x: CharSet
  x = {'a'..'z', '0'..'9'} # This constructs a set that contains the
                           # letters from 'a' to 'z' and the digits
                           # from '0' to '9'

These operations are supported by sets:

==================    ========================================================
operation             meaning
==================    ========================================================
``A + B``             union of two sets
``A * B``             intersection of two sets
``A - B``             difference of two sets (A without B's elements)
``A == B``            set equality
``A <= B``            subset relation (A is subset of B or equal to B)
``A < B``             strict subset relation (A is a proper subset of B)
``e in A``            set membership (A contains element e)
``e notin A``         A does not contain element e
``contains(A, e)``    A contains element e
``card(A)``           the cardinality of A (number of elements in A)
``incl(A, elem)``     same as ``A = A + {elem}``
``excl(A, elem)``     same as ``A = A - {elem}``
==================    ========================================================

Bit fields
~~~~~~~~~~

Sets are often used to define a type for the *flags* of a procedure.
This is a cleaner (and type safe) solution than defining integer
constants that have to be ``or``'ed together.

Enum, sets and casting can be used together as in:

.. code-block:: nim

  type
    MyFlag* {.size: sizeof(cint).} = enum
      A
      B
      C
      D
    MyFlags = set[MyFlag]

  proc toNum(f: MyFlags): int = cast[cint](f)
  proc toFlags(v: int): MyFlags = cast[MyFlags](v)

  assert toNum({}) == 0
  assert toNum({A}) == 1
  assert toNum({D}) == 8
  assert toNum({A, C}) == 5
  assert toFlags(0) == {}
  assert toFlags(7) == {A, B, C}

Note how the set turns enum values into powers of 2.

If using enums and sets with C, use distinct cint.

For interoperability with C see also the
`bitsize pragma <manual.html#implementation-specific-pragmas-bitsize-pragma>`_.
st.subx?h=hlt&id=d762282438f603d47804be04cebf77d6137c2728'>^
1639687b ^
6030d7e2 ^

9d27e966 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
6030d7e2 ^
9d27e966 ^
03d50cc8 ^
9d27e966 ^
ee9a9237 ^
dd9ba09a ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
03d50cc8 ^
6030d7e2 ^
03d50cc8 ^
ee9a9237 ^
6030d7e2 ^

33e7c3a7 ^
ee9a9237 ^
6030d7e2 ^


57628c0e ^


e0ffdcd1 ^

57628c0e ^
6030d7e2 ^
9b16f190 ^
6030d7e2 ^

4224ec81 ^
e0ffdcd1 ^
03d50cc8 ^
9b16f190 ^
15ae0717 ^
ee9a9237 ^
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

                          
       


                                                                                                                                                 
 
                     
                              
                   


                                              
              
                                    
                      


                                                                                                                                                                  
                                
                         
 
                                                    
                                                                             
              

                                                                                                                                                                       
                      
               


                                        

                                                                                                                                                                             
                             

                                                                                                                                                                           
                             
                   

                           
              
                          
                      
                                                                                                                                                                  
              
                                        
                                   
                       
                             
                   
                                                                                                                                                                             

                           
              
                          
                      
                                                                                                                                                                  
                                 
                   

                           
              
                          
                      
                                                                                                                                                                  
                                 
                                                                                                                                                                                    
                      
                         

                 
                 
              


                                                                                                                                                                       


       

                                                         
        
          
           

              
 
                                            
                  
           
 
                            
# Rudimentary test harness

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

Entry:  # manual test
    # check-ints-equal(34, 34)
    # . . push args
    68/push  "error in check-ints-equal"/imm32
    68/push  34/imm32
    68/push  34/imm32
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
    # syscall(exit, 0)
    bb/copy-to-EBX  0/imm32
    b8/copy-to-EAX  1/imm32/exit
    cd/syscall  0x80/imm8

# print msg to stderr if a != b, otherwise print "."
check-ints-equal:  # (a : int, b : int, msg : (address array byte)) -> <void>
    # . prolog
    55/push-EBP
    89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
    # . save registers
    50/push-EAX
    51/push-ECX
    53/push-EBX
    # load first 2 args into EAX and EBX
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           0/r32/EAX   8/disp8         .                 # copy *(EBP+8) to EAX
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           3/r32/EBX   0xc/disp8       .                 # copy *(EBP+12) to EBX
    # if (EAX == EBX) success
    39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX and EBX
    75/jump-if-unequal  $check-ints-equal:else/disp8
    # . _write(2/stderr, '.')
    # . . push args
    68/push  "."/imm32
    68/push  2/imm32/stderr
    # . . call
    e8/call  _write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # . return
    eb/jump  $check-ints-equal:end/disp8
    # otherwise print error message
$check-ints-equal:else:
    # . _write(2/stderr, msg)
    # . . push args
    8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           1/r32/ECX   0x10/disp8      .                 # copy *(EBP+16) to ECX
    51/push-ECX
    68/push  2/imm32/stderr
    # . . call
    e8/call  _write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # . _write(2/stderr, Newline)
    # . . push args
    68/push  Newline/imm32
    68/push  2/imm32/stderr
    # . . call
    e8/call  _write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
    # increment Num-test-failures
    ff          0/subop/increment   0/mod/indirect  5/rm32/.disp32            .             .           .           Num-test-failures/disp32          # increment *Num-test-failures
$check-ints-equal:end:
    # . restore registers
    5b/pop-to-EBX
    59/pop-to-ECX
    58/pop-to-EAX
    # . epilog
    89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
    5d/pop-to-EBP
    c3/return

== data

# length-prefixed string containing just a single newline
# convenient to have when printing messages and so on
Newline:
    # size
    1/imm32
    # data
    0a/newline

# every test failure increments this counter
Num-test-failures:
    0/imm32

# . . vim:nowrap:textwidth=0