mapbox / mapbox/mapbox-gl-js

Bitwise operations in expressions for flag checking

Open
#13,181 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged feature :green_apple:
Dominant language
TypeScript
Stars
12.4k
Forks
2.4k
PR merge metrics
No merged PRs in 30d

Description

## Motivation

With machine generated geojson it's more space saving to store up to 32 flags in a single integer value.

## Design Alternatives

The only alternative is to store boolean properties for each flag, which creates unnecessary overhead and requires us to write a boolean check expression for each flag instead of checking for multiple flags only once with an _and_ operation.
```
"properties": {
"type1": true,
"type2": false,
"type3": true,
...
}
```

Right now for each flag I want to check inside an integer I have to create an expression, which shifts the bit to the right with a division by powers of 2 and _modulo 2_ to extract the bit for the _flag == 1_ check
`['==', ['%', ['floor', ['/', ["get", "location-type"], 1<
With bitwise operators we could do a check for multiple flags within a simple _and_ operation:
`['&', ["get", "location-type"], 0b10101]`

### Implementation

So for the flag checking use case it would be enough to have the & operator expression or labelled as 'hasFlags' expression.
But you could also just add all bitwise operators to the math expressions as

- `'&'` and
- `'xor'` because ^ is already in use
- `'not'` because ! is already in use
- `'|'` or
- `'<<'` left shift
- `'>>'` right shift

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue does not name implementation files, tests, or an entry point. Start by locating the expression implementation and existing math-expression tests, then determine whether the scope is a single flag-checking operator or the proposed set of bitwise operators; done should include the agreed operator behavior and coverage for it.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend, web-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.