Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the idea of schema states from the mapper #816

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME compiler
FOLDER "JSON BinPack/Compiler"
SOURCES
states.h encoding.h compiler.cc
encoding.h compiler.cc
mapper/enum_8_bit.h
mapper/enum_8_bit_top_level.h
mapper/enum_arbitrary.h
Expand Down
1 change: 0 additions & 1 deletion src/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <sourcemeta/alterschema/linter.h>

#include "encoding.h"
#include "states.h"

namespace sourcemeta::jsonbinpack {

Expand Down
10 changes: 5 additions & 5 deletions src/compiler/mapper/integer_bounded_multiplier_8_bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ class IntegerBoundedMultiplier8Bit final
!vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") ||
!schema.defines("type") || schema.at("type").to_string() != "integer" ||
!schema.defines("minimum") || !schema.defines("maximum") ||
!schema.defines("minimum") || !schema.at("minimum").is_integer() ||
!schema.defines("maximum") || !schema.at("maximum").is_integer() ||
!schema.defines("multipleOf") ||
!schema.at("multipleOf").is_integer()) {
return false;
}

const std::optional<std::vector<sourcemeta::jsontoolkit::JSON>> states{
states_integer(schema, vocabularies)};
return states.has_value() &&
states->size() <= std::numeric_limits<std::uint8_t>::max();
return is_byte(count_multiples(schema.at("minimum").to_integer(),
schema.at("maximum").to_integer(),
schema.at("multipleOf").to_integer()));
}

auto transform(sourcemeta::alterschema::Transformer &transformer) const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ class IntegerBoundedMultiplierGreaterThan8Bit final
!vocabularies.contains(
"https://json-schema.org/draft/2020-12/vocab/validation") ||
!schema.defines("type") || schema.at("type").to_string() != "integer" ||
!schema.defines("minimum") || !schema.defines("maximum") ||
!schema.defines("minimum") || !schema.at("minimum").is_integer() ||
!schema.defines("maximum") || !schema.at("maximum").is_integer() ||
!schema.defines("multipleOf") ||
!schema.at("multipleOf").is_integer()) {
return false;
}

const std::optional<std::vector<sourcemeta::jsontoolkit::JSON>> states{
states_integer(schema, vocabularies)};
return !states.has_value() || !is_byte(states->size());
return !is_byte(count_multiples(schema.at("minimum").to_integer(),
schema.at("maximum").to_integer(),
schema.at("multipleOf").to_integer()));
}

auto transform(sourcemeta::alterschema::Transformer &transformer) const
Expand Down
69 changes: 0 additions & 69 deletions src/compiler/states.h

This file was deleted.

12 changes: 11 additions & 1 deletion src/numeric/include/sourcemeta/jsonbinpack/numeric.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_H_
#define SOURCEMETA_JSONBINPACK_NUMERIC_H_

#include <cstdint> // std::uint8_t
#include <cstdint> // std::uint8_t, std::int64_t
#include <limits> // std::numeric_limits

/// @defgroup numeric Numeric
Expand All @@ -20,6 +20,16 @@ template <typename T> constexpr auto is_byte(const T value) noexcept -> bool {
return value <= std::numeric_limits<std::uint8_t>::max();
}

/// @ingroup numeric
constexpr auto count_multiples(const std::int64_t minimum,
const std::int64_t maximum,
const std::int64_t multiplier) -> std::uint64_t {
assert(minimum <= maximum);
assert(multiplier > 0);
return static_cast<std::uint64_t>((maximum / multiplier) -
((minimum - 1) / multiplier));
}

} // namespace sourcemeta::jsonbinpack

#endif
Loading