Permalink
Browse files

(raptor_sequence_swap): Added pulled out of raptor_sequence_reverse

  • Loading branch information...
1 parent edc67ca commit 02805ce5e6221314bad9fb997a57cca9183cc6f1 @dajobe dajobe committed Nov 27, 2011
Showing with 39 additions and 12 deletions.
  1. +9 −7 docs/raptor2-sections.txt
  2. +2 −0 src/raptor2.h.in
  3. +28 −5 src/raptor_sequence.c
View
@@ -273,16 +273,18 @@ raptor_new_sequence
raptor_new_sequence_with_context
raptor_free_sequence
raptor_sequence_delete_at
-raptor_sequence_size
-raptor_sequence_set_at
-raptor_sequence_push
-raptor_sequence_shift
raptor_sequence_get_at
+raptor_sequence_join
raptor_sequence_pop
-raptor_sequence_unshift
-raptor_sequence_sort
raptor_sequence_print
-raptor_sequence_join
+raptor_sequence_push
+raptor_sequence_reverse
+raptor_sequence_set_at
+raptor_sequence_shift
+raptor_sequence_size
+raptor_sequence_sort
+raptor_sequence_swap
+raptor_sequence_unshift
</SECTION>
<SECTION>
View
@@ -1519,6 +1519,8 @@ void* raptor_sequence_delete_at(raptor_sequence* seq, int idx);
RAPTOR_API
void raptor_sequence_sort(raptor_sequence* seq, raptor_data_compare_handler compare);
RAPTOR_API
+int raptor_sequence_swap(raptor_sequence* seq, int i, int j);
+RAPTOR_API
int raptor_sequence_reverse(raptor_sequence* seq, int start_index, int length);
/* helper for printing sequences of strings */
View
@@ -610,6 +610,32 @@ raptor_sequence_join(raptor_sequence* dest, raptor_sequence *src)
/**
+ * raptor_sequence_swap:
+ * @seq: sequence
+ * @i: first data index
+ * @j: second data index
+ *
+ * Swap a pair of elements in a sequence
+ *
+ * Return value: non-0 if arguments are out of range
+ */
+int
+raptor_sequence_swap(raptor_sequence* seq, int i, int j)
+{
+ if(i < 0 || i >= seq->size || j < 0 || j >= seq->size)
+ return 1;
+
+ if(i != j) {
+ void* tmp = seq->sequence[i];
+ seq->sequence[i] = seq->sequence[j];
+ seq->sequence[j] = tmp;
+ }
+
+ return 0;
+}
+
+
+/**
* raptor_sequence_reverse:
* @seq: sequence
* @start_index: starting index
@@ -626,14 +652,11 @@ raptor_sequence_reverse(raptor_sequence* seq, int start_index, int length)
RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(seq, raptor_sequence, 1);
- if(end_index >= seq->size || length == 1)
+ if(end_index >= seq->size || start_index < 1 || length <= 1)
return 1;
while( (start_index != end_index) && (start_index != end_index + 1) ) {
- void* tmp;
- tmp = seq->sequence[start_index];
- seq->sequence[start_index] = seq->sequence[end_index];
- seq->sequence[end_index] = tmp;
+ raptor_sequence_swap(seq, start_index, end_index);
start_index++; end_index--;
}

0 comments on commit 02805ce

Please sign in to comment.