diff options
author | H. Peter Anvin <hpa@zytor.com> | 2012-02-27 22:26:28 -0800 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2012-02-27 22:26:28 -0800 |
commit | cd619f94fae1040638af3cc10d480fe0fa9be2c4 (patch) | |
tree | d02bb27942f261130ec6e1f27287579c736fb05d /action.c | |
parent | 2b061d66d26d1a381aaf2cee0e629bb696e0c9f8 (diff) | |
download | grv-cd619f94fae1040638af3cc10d480fe0fa9be2c4.tar.gz grv-cd619f94fae1040638af3cc10d480fe0fa9be2c4.tar.xz grv-cd619f94fae1040638af3cc10d480fe0fa9be2c4.zip |
action: Use a binary search for insertion
Linear searches make me cry.
Diffstat (limited to 'action.c')
-rw-r--r-- | action.c | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -30,20 +30,31 @@ static uint64_t next_seq = 0; void addaction(int x, int y, double when, enum actions what) { - int i = 0; + int a, b; + int i; + if ( action_count >= MAXACT ) return; - while ( i < action_count && actions[i].when <= when && actions[i].what ) - i++; + a = 0; + b = action_count; + while (b > a) { + i = (a+b) >> 1; + if (actions[i].when <= when) + a = i+1; + else + b = i; + } + + if (a < action_count) + memmove(&actions[a+1], &actions[a], + (action_count-a)*sizeof(struct action)); - memmove(&actions[i+1], &actions[i], - (action_count-i)*sizeof(struct action)); - actions[i].x = x; - actions[i].y = y; - actions[i].when = when; - actions[i].what = what; - actions[i].seq = next_seq++; + actions[a].x = x; + actions[a].y = y; + actions[a].when = when; + actions[a].what = what; + actions[a].seq = next_seq++; action_count++; } |