[GRASS-SVN] r53689 - grass/trunk/lib/vector/Vlib
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Nov 5 04:45:07 PST 2012
Author: mmetz
Date: 2012-11-05 04:45:07 -0800 (Mon, 05 Nov 2012)
New Revision: 53689
Modified:
grass/trunk/lib/vector/Vlib/break_lines.c
Log:
Vlib: Vect_break_lines_list() speed-up
Modified: grass/trunk/lib/vector/Vlib/break_lines.c
===================================================================
--- grass/trunk/lib/vector/Vlib/break_lines.c 2012-11-05 12:43:38 UTC (rev 53688)
+++ grass/trunk/lib/vector/Vlib/break_lines.c 2012-11-05 12:45:07 UTC (rev 53689)
@@ -115,6 +115,29 @@
return break_lines(Map, List_break, List_ref, type, Err, 1);
}
+static int cmp(const void *a, const void *b)
+{
+ int ai = *(int *)a;
+ int bi = *(int *)b;
+
+ return (ai < bi ? -1 : ai > bi);
+}
+
+static void sort_ilist(struct ilist *List, int (*cmp_ilist)(const void *, const void *))
+{
+ int i, is_sorted = 1;
+
+ for (i = 1; i < List->n_values; i++) {
+ if (cmp_ilist(&List->value[i - 1], &List->value[i]) == 1) {
+ is_sorted = 0;
+ break;
+ }
+ }
+
+ if (!is_sorted)
+ qsort(List->value, List->n_values, sizeof(int), cmp);
+}
+
int
break_lines(struct Map_info *Map, struct ilist *List_break,
struct ilist *List_ref, int type,
@@ -169,6 +192,9 @@
* the file, and process next line (remaining lines overlapping box are skipped)
*/
nbreaks = 0;
+
+ if (List_ref)
+ sort_ilist(List_ref, cmp);
for (iline = 0; iline < nlines; iline++) {
G_percent(iline, nlines, 1);
@@ -179,7 +205,8 @@
aline = iline + 1;
}
- if (List_ref && !Vect_val_in_list(List_ref, aline))
+ if (List_ref &&
+ !bsearch(&aline, List_ref->value, List_ref->n_values, sizeof(int), cmp))
continue;
G_debug(3, "aline = %d", aline);
@@ -232,8 +259,13 @@
}
/* check intersection of aline with bline only once */
- if (bline > aline)
- continue;
+ if (bline > aline) {
+ if (!List_ref)
+ continue;
+ else if (bsearch(&bline, List_ref->value, List_ref->n_values,
+ sizeof(int), cmp))
+ continue;
+ }
G_debug(3, " j = %d bline = %d", j, bline);
@@ -335,17 +367,18 @@
/* line may collapse, don't write zero length lines */
Vect_line_prune(AXLines[k]);
if ((atype & GV_POINTS) || AXLines[k]->n_points > 1) {
- if (!check)
+ if (!check) {
ret = Vect_write_line(Map, atype, AXLines[k],
ACats);
- if (List_ref) {
- Vect_list_append(List_ref, ret);
+ if (List_ref) {
+ G_ilist_add(List_ref, ret);
+ }
+ G_debug(3, "Line %d written, npoints = %d", ret,
+ AXLines[k]->n_points);
+ if (List_break) {
+ Vect_list_append(List_break, ret);
+ }
}
- G_debug(3, "Line %d written, npoints = %d", ret,
- AXLines[k]->n_points);
- if (List_break) {
- Vect_list_append(List_break, ret);
- }
}
/* Write intersection points */
@@ -373,13 +406,14 @@
/* line may collapse, don't write zero length lines */
Vect_line_prune(BXLines[k]);
if ((btype & GV_POINTS) || BXLines[k]->n_points > 1) {
- if (!check)
+ if (!check) {
ret =
Vect_write_line(Map, btype, BXLines[k],
BCats);
- G_debug(5, "Line %d written", ret);
- if (List_break) {
- Vect_list_append(List_break, ret);
+ G_debug(5, "Line %d written", ret);
+ if (List_break) {
+ Vect_list_append(List_break, ret);
+ }
}
}
More information about the grass-commit
mailing list