# Sets

Operators and functions for working with an array of boolean values (**set** type) are described here.

* [arr( set s ) arr.int](/stdlib/sets.md#arr-set-s-arr-int)
* [set( arr.int a ) set](/stdlib/sets.md#set-arr-int-a-set)
* [set( str s ) set](/stdlib/sets.md#set-str-s-set)
* [str( set s ) str](/stdlib/sets.md#str-set-s-str)
* [Set( set s, int index ) set](/stdlib/sets.md#set-set-s-int-index-set)
* [Toggle( set s, int index ) bool](/stdlib/sets.md#toggle-set-s-int-index-bool)
* [UnSet( set s, int index ) set](/stdlib/sets.md#unset-set-s-int-index-set)

## Operators

| Operator             | Result | Description                                                                                |
| -------------------- | ------ | ------------------------------------------------------------------------------------------ |
| **\*** set           | int    | Returns the set size.                                                                      |
| **^** set            | set    | Returns a new set as logical NOT of the source set. *!s\[i]* for each item.                |
| set **=** set        | set    | Assignment operator.                                                                       |
| set **&=** set       | set    | Create a clone of the set. The new variable will work with the same data set.              |
| set **+=** set       | set    | Appends a set to a set variable.                                                           |
| set **&** set        | set    | Returns a set that is the intersection of two sets. *left\[i] && right\[i]* for each item. |
| set **\|** set       | set    | Returns a set that is the union of two sets. *left\[i] \|\| right\[i]* for each item.      |
| set **\[** int **]** | bool   | Sets/gets a set value.                                                                     |

## Functions

### arr(set s) arr.int

The *arr* function converts a *set* value to an array of integers that contains indexes of *set* items.

### set(str s) set

The *set* function converts a string to a *set* value and returns it. The string must only have 1 and 0 characters.

### set(arr.int a) set

The *set* function converts an array of integers to a *set* value and returns it. The result set will have items with corresponding indexes.

```
run arr.int {
  set s &= {780, 99, 128, 105, 136}
  arr.int as = arr(s)
  as += 330
  s &= set(as)
  return arr(s) // [99 105 128 136 330 780]
}
```

### str(set s) str

The *str* function converts a *set* value to a string and returns it. The result string contains only 1 and 0.

### Set(set s, int index) set

The *Set* function adds an index item to the set. It is the same as *s\[index] = true*. The function returns *s*.

### Toggle(set s, int index) bool

The *Toggle* function adds an index item to the set if the set doesn't have it and otherwise, the function removes the item. It is the same as *s\[index] = !s\[index]*. The function returns the previous state - *true* if the item existed and, otherwise, false.

### UnSet(set s, int index) set

The *UnSet* function removes an index item from the set. It is the same as *s\[index] = false*. The function returns *s*.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gentee.org/stdlib/sets.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
