Skip to content

Commit

Permalink
change iteration more ideomatic (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
scholarsmate authored Jan 31, 2022
1 parent 6c7cbb2 commit b10db25
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 32 deletions.
28 changes: 28 additions & 0 deletions src/bindings/java/omega_edit_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,34 @@ SWIGEXPORT jlong JNICALL Java_omega_1editJNI_omega_1visit_1change_1create_1conte
}


SWIGEXPORT jint JNICALL Java_omega_1editJNI_omega_1visit_1change_1at_1end(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
omega_visit_change_context_t *arg1 = (omega_visit_change_context_t *) 0 ;
int result;

(void)jenv;
(void)jcls;
arg1 = *(omega_visit_change_context_t **)&jarg1;
result = (int)omega_visit_change_at_end((omega_visit_change_context_struct const *)arg1);
jresult = (jint)result;
return jresult;
}


SWIGEXPORT jint JNICALL Java_omega_1editJNI_omega_1visit_1change_1begin(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
omega_visit_change_context_t *arg1 = (omega_visit_change_context_t *) 0 ;
int result;

(void)jenv;
(void)jcls;
arg1 = *(omega_visit_change_context_t **)&jarg1;
result = (int)omega_visit_change_begin(arg1);
jresult = (jint)result;
return jresult;
}


SWIGEXPORT jint JNICALL Java_omega_1editJNI_omega_1visit_1change_1next(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
omega_visit_change_context_t *arg1 = (omega_visit_change_context_t *) 0 ;
Expand Down
58 changes: 58 additions & 0 deletions src/bindings/node/omega_edit_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4967,6 +4967,62 @@ static SwigV8ReturnValue _wrap_omega_visit_change_create_context(const SwigV8Arg
}


static SwigV8ReturnValue _wrap_omega_visit_change_at_end(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

SWIGV8_VALUE jsresult;
omega_visit_change_context_t *arg1 = (omega_visit_change_context_t *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int result;

if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_visit_change_at_end.");

res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_visit_change_context_struct, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_visit_change_at_end" "', argument " "1"" of type '" "omega_visit_change_context_t const *""'");
}
arg1 = reinterpret_cast< omega_visit_change_context_t * >(argp1);
result = (int)omega_visit_change_at_end((omega_visit_change_context_struct const *)arg1);
jsresult = SWIG_From_int(static_cast< int >(result));


SWIGV8_RETURN(jsresult);

goto fail;
fail:
SWIGV8_RETURN(SWIGV8_UNDEFINED());
}


static SwigV8ReturnValue _wrap_omega_visit_change_begin(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

SWIGV8_VALUE jsresult;
omega_visit_change_context_t *arg1 = (omega_visit_change_context_t *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int result;

if(args.Length() != 1) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_omega_visit_change_begin.");

res1 = SWIG_ConvertPtr(args[0], &argp1,SWIGTYPE_p_omega_visit_change_context_struct, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "omega_visit_change_begin" "', argument " "1"" of type '" "omega_visit_change_context_t *""'");
}
arg1 = reinterpret_cast< omega_visit_change_context_t * >(argp1);
result = (int)omega_visit_change_begin(arg1);
jsresult = SWIG_From_int(static_cast< int >(result));


SWIGV8_RETURN(jsresult);

goto fail;
fail:
SWIGV8_RETURN(SWIGV8_UNDEFINED());
}


static SwigV8ReturnValue _wrap_omega_visit_change_next(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

Expand Down Expand Up @@ -5561,6 +5617,8 @@ SWIGV8_AddStaticFunction(exports_obj, "omega_viewport_notify", _wrap_omega_viewp
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_changes", _wrap_omega_visit_changes, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_changes_reverse", _wrap_omega_visit_changes_reverse, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_create_context", _wrap_omega_visit_change_create_context, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_at_end", _wrap_omega_visit_change_at_end, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_begin", _wrap_omega_visit_change_begin, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_next", _wrap_omega_visit_change_next, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_context_get_change", _wrap_omega_visit_change_context_get_change, context);
SWIGV8_AddStaticFunction(exports_obj, "omega_visit_change_destroy_context", _wrap_omega_visit_change_destroy_context, context);
Expand Down
14 changes: 14 additions & 0 deletions src/include/omega_edit/visit.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ typedef struct omega_visit_change_context_struct omega_visit_change_context_t;
*/
omega_visit_change_context_t *omega_visit_change_create_context(const omega_session_t *session_ptr, int reverse);

/**
* Return non-zero if we are at the end of the change visitations
* @param change_context_ptr change visitor context to see if we're at the end of
* @return non-zero if we are at the end of the change visitations and zero if there are changes remaining to visit
*/
int omega_visit_change_at_end(const omega_visit_change_context_t *change_context_ptr);

/**
* Set the change visitor context to the beginning of the changes
* @param change_context_ptr change visitor context to set to the beginning
* @return non-zero if there are no changes to visit and zero otherwise
*/
int omega_visit_change_begin(omega_visit_change_context_t *change_context_ptr);

/**
* Given a change visitor context, find the next change
* @param change_context_ptr change visitor context to find the next change in
Expand Down
53 changes: 36 additions & 17 deletions src/lib/visit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int omega_visit_changes_reverse(const omega_session_t *session_ptr, omega_sessio
struct omega_visit_change_context_struct {
const omega_session_t *session_ptr{};
const omega_change_t *change_ptr{};
bool at_end;
bool reverse{};
union {
omega_changes_t::const_iterator *iter_ptr;
Expand All @@ -56,10 +57,16 @@ omega_visit_change_context_t *omega_visit_change_create_context(const omega_sess
change_context_ptr->change_ptr = nullptr;
change_context_ptr->reverse = (0 != reverse);
change_context_ptr->change_iter.iter_ptr = nullptr;
change_context_ptr->at_end = true;
return change_context_ptr;
}

int omega_visit_change_next(omega_visit_change_context_t *change_context_ptr) {
int omega_visit_change_at_end(const omega_visit_change_context_t *change_context_ptr) {
assert(change_context_ptr);
return (change_context_ptr->at_end) ? 1 : 0;
}

int omega_visit_change_begin(omega_visit_change_context_t *change_context_ptr) {
assert(change_context_ptr);
assert(change_context_ptr->session_ptr);
assert(change_context_ptr->session_ptr->models_.back());
Expand All @@ -68,27 +75,39 @@ int omega_visit_change_next(omega_visit_change_context_t *change_context_ptr) {
if (!change_context_ptr->change_iter.riter_ptr) {
change_context_ptr->change_iter.riter_ptr = new omega_changes_t::const_reverse_iterator;
assert(change_context_ptr->change_iter.riter_ptr);
*change_context_ptr->change_iter.riter_ptr =
change_context_ptr->session_ptr->models_.back()->changes.rbegin();
} else {
++*change_context_ptr->change_iter.riter_ptr;
}
return (*change_context_ptr->change_iter.riter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.rend())
? 0
: 1;
}
if (!change_context_ptr->change_iter.iter_ptr) {
change_context_ptr->change_iter.iter_ptr = new omega_changes_t::const_iterator;
assert(change_context_ptr->change_iter.iter_ptr);
*change_context_ptr->change_iter.riter_ptr = change_context_ptr->session_ptr->models_.back()->changes.rbegin();
change_context_ptr->at_end = (*change_context_ptr->change_iter.riter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.rend());
} else {
if (!change_context_ptr->change_iter.iter_ptr) {
change_context_ptr->change_iter.iter_ptr = new omega_changes_t::const_iterator;
assert(change_context_ptr->change_iter.iter_ptr);
}
*change_context_ptr->change_iter.iter_ptr = change_context_ptr->session_ptr->models_.back()->changes.cbegin();
change_context_ptr->at_end = (*change_context_ptr->change_iter.iter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.cend());
}
return omega_visit_change_at_end(change_context_ptr);
}

int omega_visit_change_next(omega_visit_change_context_t *change_context_ptr) {
assert(change_context_ptr);
assert(change_context_ptr->session_ptr);
assert(change_context_ptr->session_ptr->models_.back());
if (change_context_ptr->session_ptr->models_.back()->changes.empty()) { return 0; }
if (change_context_ptr->reverse) {
assert(change_context_ptr->change_iter.riter_ptr);
++*change_context_ptr->change_iter.riter_ptr;
change_context_ptr->at_end = (*change_context_ptr->change_iter.riter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.rend());
} else {
assert(change_context_ptr->change_iter.iter_ptr);
++*change_context_ptr->change_iter.iter_ptr;
change_context_ptr->at_end = (*change_context_ptr->change_iter.iter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.cend());
}
return (*change_context_ptr->change_iter.iter_ptr ==
change_context_ptr->session_ptr->models_.back()->changes.cend())
? 0
: 1;
return omega_visit_change_at_end(change_context_ptr);
}

const omega_change_t *omega_visit_change_context_get_change(const omega_visit_change_context_t *change_context_ptr) {
Expand Down
36 changes: 21 additions & 15 deletions src/tests/omega_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST_CASE("File Compare", "[UtilTests]") {

TEST_CASE("End Of Line", "[EOLTests]") {
omega_byte_t buffer[1024];
FILE * in_fp = fopen("data/test1.dat", "rb");
FILE *in_fp = fopen("data/test1.dat", "rb");
REQUIRE(in_fp);
REQUIRE(0 == fseek(in_fp, 0, SEEK_END));
auto file_size = ftell(in_fp);
Expand All @@ -110,7 +110,7 @@ TEST_CASE("End Of Line", "[EOLTests]") {
REQUIRE(file_size < sizeof(buffer));
REQUIRE(file_size == fread(buffer, sizeof(omega_byte_t), file_size, in_fp));
REQUIRE(0 == fclose(in_fp));
FILE * out_fp = fopen("data/test1.actual.eol.1.dat", "wb");
FILE *out_fp = fopen("data/test1.actual.eol.1.dat", "wb");
REQUIRE(out_fp);
REQUIRE(file_size == fwrite(buffer, sizeof(omega_byte_t), file_size, out_fp));
REQUIRE(file_size == ftell(out_fp));
Expand Down Expand Up @@ -272,14 +272,14 @@ TEST_CASE("Transformer", "[TransformerTest]") {
}

TEST_CASE("File Transformer", "[TransformerTest]") {
REQUIRE(0 == omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.1.dat", to_upper, nullptr, 0,
0));
REQUIRE(0 == omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.1.dat",
to_upper, nullptr, 0, 0));
REQUIRE(0 == compare_files("data/test1.expected.transformed.1.dat", "data/test1.actual.transformed.1.dat"));
REQUIRE(0 == omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.2.dat", to_lower, nullptr, 37,
10));
REQUIRE(0 == omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.2.dat",
to_lower, nullptr, 37, 10));
REQUIRE(0 == compare_files("data/test1.expected.transformed.2.dat", "data/test1.actual.transformed.2.dat"));
REQUIRE(0 != omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.3.dat", to_lower, nullptr, 37,
100));
REQUIRE(0 != omega_util_apply_byte_transform_to_file("data/test1.dat", "data/test1.actual.transformed.3.dat",
to_lower, nullptr, 37, 100));
REQUIRE(0 == omega_util_file_exists("data/test1.actual.transformed.3.dat"));
}

Expand All @@ -300,7 +300,8 @@ typedef struct file_info_struct {
size_t num_changes{};
} file_info_t;

static inline void session_change_cbk(const omega_session_t *session_ptr, omega_session_event_t session_event, const omega_change_t *change_ptr) {
static inline void session_change_cbk(const omega_session_t *session_ptr, omega_session_event_t session_event,
const omega_change_t *change_ptr) {
// Not all session changes are the result of a standard change like delete / insert / overwrite
switch (session_event) {
case SESSION_EVT_EDIT:
Expand Down Expand Up @@ -361,7 +362,7 @@ typedef struct mask_info_struct {
omega_mask_kind_t mask_kind;
} mask_info_t;

static inline omega_byte_t byte_mask_transform(omega_byte_t byte, void * user_data_ptr) {
static inline omega_byte_t byte_mask_transform(omega_byte_t byte, void *user_data_ptr) {
const auto mask_info_ptr = reinterpret_cast<mask_info_t *>(user_data_ptr);
return omega_util_mask_byte(byte, mask_info_ptr->mask, mask_info_ptr->mask_kind);
}
Expand All @@ -374,7 +375,8 @@ TEST_CASE("Checkpoint Tests", "[CheckpointTests]") {
REQUIRE(session_ptr);
auto file_size = omega_session_get_computed_file_size(session_ptr);
REQUIRE(file_size > 0);
REQUIRE(0 != omega_edit_insert_string(session_ptr, 0, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
REQUIRE(0 !=
omega_edit_insert_string(session_ptr, 0, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
REQUIRE(0 == omega_session_get_num_checkpoints(session_ptr));
REQUIRE(-1 == omega_edit_destroy_last_checkpoint(session_ptr));
REQUIRE(0 == omega_edit_apply_transform(session_ptr, to_lower, nullptr, 0, 0, "./data"));
Expand Down Expand Up @@ -406,7 +408,8 @@ TEST_CASE("Checkpoint Tests", "[CheckpointTests]") {
mask_info.mask_kind = MASK_AND;
REQUIRE(0 == omega_edit_apply_transform(session_ptr, byte_mask_transform, &mask_info, 10, 0, "./data"));
REQUIRE(6 == omega_session_get_num_checkpoints(session_ptr));
REQUIRE(0 != omega_edit_overwrite_string(session_ptr, 0, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
REQUIRE(0 != omega_edit_overwrite_string(session_ptr, 0,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
REQUIRE(0 == omega_edit_save(session_ptr, "data/test1.actual.checkpoint.6.dat", 1, nullptr));
REQUIRE(0 == compare_files("data/test1.expected.checkpoint.6.dat", "data/test1.actual.checkpoint.6.dat"));
REQUIRE(0 == omega_edit_destroy_last_checkpoint(session_ptr));
Expand Down Expand Up @@ -617,7 +620,8 @@ TEST_CASE("Check initialization", "[InitTests]") {
auto visit_change_context = omega_visit_change_create_context(session_ptr, 0);
REQUIRE(visit_change_context);
string forward_change_sequence;
while (omega_visit_change_next(visit_change_context)) {
for (omega_visit_change_begin(visit_change_context); !omega_visit_change_at_end(visit_change_context);
omega_visit_change_next(visit_change_context)) {
change_ptr = omega_visit_change_context_get_change(visit_change_context);
forward_change_sequence += omega_change_get_kind_as_char(change_ptr);
}
Expand All @@ -628,7 +632,8 @@ TEST_CASE("Check initialization", "[InitTests]") {
auto reverse_change_sequence = forward_change_sequence;
std::reverse(reverse_change_sequence.begin(), reverse_change_sequence.end());
string change_sequence;
while (omega_visit_change_next(visit_change_context)) {
for (omega_visit_change_begin(visit_change_context); !omega_visit_change_at_end(visit_change_context);
omega_visit_change_next(visit_change_context)) {
change_ptr = omega_visit_change_context_get_change(visit_change_context);
change_sequence += omega_change_get_kind_as_char(change_ptr);
}
Expand Down Expand Up @@ -662,7 +667,8 @@ struct view_mode_t {
display_mode_t display_mode = display_mode_t::CHAR_MODE;
};

static inline void vpt_change_cbk(const omega_viewport_t *viewport_ptr, omega_viewport_event_t viewport_event, const omega_change_t *change_ptr) {
static inline void vpt_change_cbk(const omega_viewport_t *viewport_ptr, omega_viewport_event_t viewport_event,
const omega_change_t *change_ptr) {
if (change_ptr) { clog << "Change serial: " << omega_change_get_serial(change_ptr) << endl; }
clog << dec << "capacity: " << omega_viewport_get_capacity(viewport_ptr)
<< " length: " << omega_viewport_get_length(viewport_ptr)
Expand Down

0 comments on commit b10db25

Please sign in to comment.