Skip to content

Commit

Permalink
[opengl-2] cherry pick slice and index-of expression (#2023)
Browse files Browse the repository at this point in the history
Co-authored-by: Siarhei Fedartsou <siarhei.fedartsou@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Bart Louwers <bart@emeel.net>
Co-authored-by: Ovidiu Voda <ovi.voda@gmail.com>
  • Loading branch information
5 people authored Jan 17, 2024
1 parent 0849edc commit 7744591
Show file tree
Hide file tree
Showing 22 changed files with 875 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/image.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/image_expression.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/in.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/index_of.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/interpolate.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/interpolator.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/is_constant.hpp
Expand All @@ -239,6 +240,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/match.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/number_format.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/parsing_context.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/slice.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/step.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/type.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/value.hpp
Expand Down Expand Up @@ -630,6 +632,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image_expression.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/in.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/index_of.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/interpolate.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_constant.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_expression.cpp
Expand All @@ -639,6 +642,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/match.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/number_format.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/parsing_context.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/slice.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/step.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/util.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/util.hpp
Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/style/expression/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ enum class Kind : int32_t {
ImageExpression,
In,
Within,
Distance
Distance,
IndexOf,
Slice
};

class Expression {
Expand Down
41 changes: 41 additions & 0 deletions include/mbgl/style/expression/index_of.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/conversion.hpp>
#include <memory>

namespace mbgl {
namespace style {
namespace expression {

class IndexOf : public Expression {
public:
IndexOf(std::unique_ptr<Expression> keyword_,
std::unique_ptr<Expression> input_,
std::unique_ptr<Expression> fromIndex_);

static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);

EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>&) const override;

bool operator==(const Expression& e) const override;

std::vector<std::optional<Value>> possibleOutputs() const override;
std::string getOperator() const override;

private:
EvaluationResult evaluateForArrayInput(const std::vector<Value>& array,
const Value& keyword,
size_t fromIndex) const;
EvaluationResult evaluateForStringInput(const std::string& string, const Value& keyword, size_t fromIndex) const;

private:
std::unique_ptr<Expression> keyword;
std::unique_ptr<Expression> input;
std::unique_ptr<Expression> fromIndex;
};

} // namespace expression
} // namespace style
} // namespace mbgl
39 changes: 39 additions & 0 deletions include/mbgl/style/expression/slice.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/conversion.hpp>
#include <memory>

namespace mbgl {
namespace style {
namespace expression {

class Slice : public Expression {
public:
Slice(std::unique_ptr<Expression> input_,
std::unique_ptr<Expression> fromIndex_,
std::unique_ptr<Expression> toIndex_);

static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);

EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>&) const override;

bool operator==(const Expression& e) const override;

std::vector<std::optional<Value>> possibleOutputs() const override;
std::string getOperator() const override;

private:
EvaluationResult evaluateForStringInput(const std::string& input, int fromIndexValue, int toIndexValue) const;
EvaluationResult evaluateForArrayInput(const std::vector<Value>& input, int fromIndexValue, int toIndexValue) const;

private:
std::unique_ptr<Expression> input;
std::unique_ptr<Expression> fromIndex;
std::unique_ptr<Expression> toIndex;
};

} // namespace expression
} // namespace style
} // namespace mbgl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"expression": ["index-of", ["get", "i"], ["array", ["get", "arr"]]],
"inputs": [
[{}, {"properties": {"i": null, "arr": [9, 8, 7]}}],
[{}, {"properties": {"i": null, "arr": [9, 8, 7, null]}}],
[{}, {"properties": {"i": 1, "arr": [9, 8, 7]}}],
[{}, {"properties": {"i": 9, "arr": [9, 8, 7, 9]}}],
[{}, {"properties": {"i": 1, "arr": null}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "i"], ["array", ["get", "arr"]]],
"outputs": [
-1,
3,
-1,
0,
{"error": "Expected value to be of type array, but found null instead."}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"expression": ["index-of", ["get", "substr"], ["string", ["get", "str"]]],
"inputs": [
[{}, {"properties": {"substr": null, "str": "helloworld"}}],
[{}, {"properties": {"substr": "foo", "str": "helloworld"}}],
[{}, {"properties": {"substr": "low", "str": "helloworld"}}],
[{}, {"properties": {"substr": "low", "str": null}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "substr"], ["string", ["get", "str"]]],
"outputs": [
-1,
-1,
3,
{"error": "Expected value to be of type string, but found null instead."}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"expression": ["index-of", ["get", "i"], ["get", "arr"]],
"inputs": [
[{}, {"properties": {"i": null, "arr": [9, 8, 7]}}],
[{}, {"properties": {"i": 1, "arr": [9, 8, 7]}}],
[{}, {"properties": {"i": 9, "arr": [9, 8, 7]}}],
[
{},
{
"properties": {
"i": "foo",
"arr": ["baz", "bar", "hello", "foo", "world"]
}
}
],
[
{},
{
"properties": {
"i": true,
"arr": ["foo", 123, null, 456, false, {}, true]
}
}
],
[{}, {"properties": {"i": 1, "arr": null}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "i"], ["get", "arr"]],
"outputs": [
-1,
-1,
0,
3,
6,
{
"error": "Expected second argument to be of type array or string, but found null instead."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"expression": ["index-of", ["get", "substr"], ["get", "str"]],
"inputs": [
[{}, {"properties": {"substr": null, "str": "helloworld"}}],
[{}, {"properties": {"substr": "foo", "str": "helloworld"}}],
[{}, {"properties": {"substr": "low", "str": "helloworld"}}],
[{}, {"properties": {"substr": true, "str": "falsetrue"}}],
[{}, {"properties": {"substr": false, "str": "falsetrue"}}],
[{}, {"properties": {"substr": 123, "str": "hello123world"}}],
[{}, {"properties": {"substr": "low", "str": null}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "substr"], ["get", "str"]],
"outputs": [
-1,
-1,
3,
5,
0,
5,
{
"error": "Expected second argument to be of type array or string, but found null instead."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"expression": ["index-of", ["get", "needle"], ["get", "haystack"]],
"inputs": [
[{}, {"properties": {"needle": 1, "haystack": 123}}],
[{}, {"properties": {"needle": "foo", "haystack": {}}}],
[{}, {"properties": {"needle": "foo", "haystack": null}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "needle"], ["get", "haystack"]],
"outputs": [
{
"error": "Expected second argument to be of type array or string, but found number instead."
},
{
"error": "Expected second argument to be of type array or string, but found object instead."
},
{
"error": "Expected second argument to be of type array or string, but found null instead."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"expression": ["index-of", ["get", "needle"], ["get", "haystack"]],
"inputs": [
[{}, {"properties": {"needle": {}, "haystack": [9, 8, 7]}}],
[{}, {"properties": {"needle": {}, "haystack": "helloworld"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "needle"], ["get", "haystack"]],
"outputs": [
{
"error": "Expected first argument to be of type boolean, string, number or null, but found object instead."
},
{
"error": "Expected first argument to be of type boolean, string, number or null, but found object instead."
}
]
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"expression": ["index-of", ["get", "needle"], ["get", "hay"], ["get", "i"]],
"inputs": [
[{}, {"properties": {"needle": null, "hay": "helloworld", "i": 0}}],
[{}, {"properties": {"needle": "foo", "hay": "helloworld", "i": 0}}],
[{}, {"properties": {"needle": "low", "hay": "helloworldlow", "i": 4}}],
[{}, {"properties": {"needle": true, "hay": "falsetruetrue", "i": 6}}],
[{}, {"properties": {"needle": false, "hay": "falsetrue", "i": 0}}],
[{}, {"properties": {"needle": 123, "hay": "hello123world", "i": 6}}],
[{}, {"properties": {"needle": "low", "hay": null, "i": 0}}],
[{}, {"properties": {"needle": 7, "hay": [9, 8, 7, 8, 7, 7], "i": 3}}],
[{}, {"properties": {"needle": 9, "hay": [9, 8, 7, 8, 7, 7], "i": 1}}],
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": 1}}],
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": -1}}],
[{}, {"properties": {"needle": "foo", "hay": ["foo", "foo"], "i": -100}}],
[{}, {"properties": {"needle": "foo", "hay": "__foo__foo", "i": -100}}],
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": 10000}}],
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": "wrong"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "number"
},
"serialized": ["index-of", ["get", "needle"], ["get", "hay"], ["get", "i"]],
"outputs": [
-1,
-1,
10,
9,
0,
-1,
{
"error": "Expected second argument to be of type array or string, but found null instead."
},
4,
-1,
1,
1,
0,
2,
-1,
{
"error": "Expected third argument to be of type number, but found string instead."
}
]
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"expression": ["slice", ["array", ["get", "val"]], ["get", "index"]],
"inputs": [
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 2}}],
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 0}}],
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 99}}],
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": -2}}]
],
"expected": {
"serialized": ["slice", ["array", ["get", "val"]], ["get", "index"]],
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array"
},
"outputs": [[3, 4, 5], [1, 2, 3, 4, 5], [], [4, 5]]
}
}
Loading

0 comments on commit 7744591

Please sign in to comment.