timetable-aggregate.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "sys/timetable-aggregate.h"
00042
00043 #define XXX_HACK_MAX_CATEGORIES 32
00044
00045 #include <stdio.h>
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 static struct timetable_aggregate_entry *
00056 find_aggregate_category(struct timetable_aggregate *a,
00057 const uint16_t cat)
00058 {
00059 int i;
00060 uint16_t acat;
00061
00062 for(i = 0; i < a->ptr; ++i) {
00063 acat = (a->entries[i].id[0] << 8) + a->entries[i].id[1];
00064 if(acat == cat) {
00065
00066 return &a->entries[i];
00067 }
00068 }
00069
00070 if(i == a->size) {
00071 return NULL;
00072 }
00073
00074 a->entries[a->ptr].id = NULL;
00075 return &a->entries[a->ptr++];
00076 }
00077
00078
00079
00080
00081
00082
00083 static struct timetable_aggregate_entry *
00084 find_aggregate(struct timetable_aggregate *a,
00085 const char *id)
00086 {
00087 int i;
00088 for(i = 0; i < a->ptr; ++i) {
00089 if(a->entries[i].id == id) {
00090 return &a->entries[i];
00091 }
00092 }
00093 if(i == a->size) {
00094 return NULL;
00095 }
00096 a->entries[a->ptr].id = NULL;
00097 return &a->entries[a->ptr++];
00098 }
00099
00100 void
00101 timetable_aggregate_print_detailed(struct timetable_aggregate *a)
00102 {
00103 int i;
00104
00105 for(i = 0; i < a->ptr; ++i) {
00106 printf("-- %s: %lu / %u = %lu\n", a->entries[i].id,
00107 a->entries[i].time,
00108 a->entries[i].episodes,
00109 a->entries[i].time / a->entries[i].episodes);
00110 }
00111
00112 printf("Memory for entries: %d * %d = %d\n",
00113 (int)sizeof(struct timetable_aggregate), a->ptr,
00114 (int)sizeof(struct timetable_aggregate) * a->ptr);
00115 }
00116
00117 void
00118 timetable_aggregate_reset(struct timetable_aggregate *a)
00119 {
00120 int i;
00121 for(i = 0; i < a->ptr; ++i) {
00122 a->entries[i].time = 0;
00123 a->entries[i].episodes = 0;
00124 }
00125 }
00126
00127 void
00128 timetable_aggregate_print_categories(struct timetable_aggregate *a)
00129 {
00130 int i;
00131
00132
00133 for(i = 0; i < a->ptr; ++i) {
00134 printf("-- %c%c: %lu / %u = %lu\n",
00135 a->entries[i].id[0], a->entries[i].id[1],
00136 a->entries[i].time,
00137 a->entries[i].episodes,
00138 a->entries[i].time / a->entries[i].episodes);
00139 }
00140
00141 printf("Memory for entries: %d * %d = %d\n",
00142 (int)sizeof(struct timetable_aggregate), a->ptr,
00143 (int)sizeof(struct timetable_aggregate) * a->ptr);
00144 }
00145
00146 void
00147 timetable_aggregate_compute_detailed(struct timetable_aggregate *a,
00148 struct timetable *timetable)
00149 {
00150 unsigned int i;
00151 rtimer_clock_t t;
00152
00153 t = timetable->timestamps[0].time;
00154
00155 for(i = 1; i < *timetable->ptr; ++i) {
00156 struct timetable_aggregate_entry *entry;
00157 entry = find_aggregate(a, timetable->timestamps[i - 1].id);
00158 if(entry == NULL) {
00159
00160
00161 } else if(entry->id == NULL) {
00162
00163 entry->id = timetable->timestamps[i - 1].id;
00164 entry->time = (unsigned long)(timetable->timestamps[i].time - t -
00165 timetable_timestamp_time);
00166 entry->episodes = 1;
00167
00168 } else {
00169 entry->time += (unsigned long)(timetable->timestamps[i].time - t -
00170 timetable_timestamp_time);
00171 entry->episodes++;
00172 }
00173 t = timetable->timestamps[i].time;
00174
00175 }
00176 }
00177
00178 void
00179 timetable_aggregate_compute_categories(struct timetable_aggregate *a,
00180 struct timetable *timetable)
00181 {
00182 unsigned int i;
00183 int j;
00184 rtimer_clock_t t;
00185 uint16_t categories[XXX_HACK_MAX_CATEGORIES];
00186 int categories_ptr = 0;
00187
00188 t = timetable->timestamps[0].time;
00189
00190 for(i = 1; i < *timetable->ptr; ++i) {
00191 struct timetable_aggregate_entry *entry;
00192 uint16_t cat;
00193
00194
00195
00196 cat = (timetable->timestamps[i - 1].id[0] << 8) +
00197 (timetable->timestamps[i - 1].id[1] & 0xff);
00198 entry = find_aggregate_category(a, cat);
00199 if(entry == NULL) {
00200
00201
00202 } else if(entry->id == NULL) {
00203
00204 entry->id = timetable->timestamps[i - 1].id;
00205 entry->time = (unsigned long)(timetable->timestamps[i].time - t -
00206 timetable_timestamp_time);
00207 entry->episodes = 1;
00208
00209
00210
00211 } else {
00212
00213 entry->time += (unsigned long)(timetable->timestamps[i].time - t -
00214 timetable_timestamp_time);
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 for(j = 0; j < categories_ptr; ++j) {
00227 if(categories[j] == cat) {
00228 break;
00229 }
00230 }
00231 if(j == categories_ptr) {
00232 categories[j] = cat;
00233 categories_ptr++;
00234 entry->episodes++;
00235 }
00236 }
00237 t = timetable->timestamps[i].time;
00238 }
00239 }
00240