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

Ensure field unicity for records #286

Merged
merged 1 commit into from
Nov 7, 2016
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
4 changes: 0 additions & 4 deletions vm/vm/main/dynbuilders-decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ struct UnstableField {
UnstableNode value;
};

template <class T>
inline
void sortFeatures(VM vm, size_t width, T features[]);

template <class T>
inline
UnstableNode buildArityDynamic(VM vm, RichNode label, size_t width,
Expand Down
55 changes: 45 additions & 10 deletions vm/vm/main/dynbuilders.hh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ namespace internal {
return const_cast<UnstableNode&>(element.feature);
}

inline
UnstableNode valueOf(VM vm, const UnstableNode& element) {
return UnstableNode();
}

inline
UnstableNode& valueOf(VM vm, const UnstableField& element) {
return const_cast<UnstableNode&>(element.value);
}

template <class T>
inline
bool isTupleFeatureArray(VM vm, size_t width, T elements[]) {
Expand All @@ -155,17 +165,37 @@ namespace internal {

return true;
}
}

template <class T>
void sortFeatures(VM vm, size_t width, T features[]) {
using internal::featureOf;
template <class T>
inline
void sortFeatures(VM vm, size_t width, T features[]) {
std::sort(features, features+width,
[vm] (const T& lhs, const T& rhs) -> bool {
return compareFeatures(vm, featureOf(lhs), featureOf(rhs)) < 0;
}
);
}

template <class T>
inline
bool findDuplicateFeature(VM vm, size_t width, T features[]) {
return features+width != std::adjacent_find(features, features+width,
[vm] (const T& lhs, const T& rhs) -> bool {
return !compareFeatures(vm, featureOf(lhs), featureOf(rhs));
}
);
}

template <class T>
inline
UnstableNode featuresList(VM vm, size_t width, T features[]) {
return buildListDynamic(vm, width, features,
[vm] (const T& e) -> UnstableNode {
return buildSharp(vm, featureOf(e), valueOf(vm, e));
}
);
}

std::sort(features, features+width,
[vm] (const T& lhs, const T& rhs) -> bool {
return compareFeatures(vm, featureOf(lhs), featureOf(rhs)) < 0;
}
);
}

template <class T>
Expand All @@ -181,7 +211,12 @@ UnstableNode buildArityDynamic(VM vm, RichNode label, size_t width,
requireFeature(vm, featureOf(elements[i]));

// Sort the features
sortFeatures(vm, width, elements);
internal::sortFeatures(vm, width, elements);

// Forbid duplicated features
if (internal::findDuplicateFeature(vm, width, elements))
raiseKernelError(vm, "recordConstruction", label,
internal::featuresList(vm, width, elements));

// Check if the corresponding record should be a Tuple instead
if (internal::isTupleFeatureArray(vm, width, elements))
Expand Down