MCPcopy Create free account
hub / github.com/StereoKit/StereoKit / systems_sort

Function systems_sort

StereoKitC/systems/system.cpp:70–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68///////////////////////////////////////////
69
70bool systems_sort() {
71
72 int32_t result = 0;
73
74 // Turn dependency names into indices for update
75 sort_dependency_t *update_ids = sk_malloc_t(sort_dependency_t, systems.count);
76 for (int32_t i = 0; i < systems.count; i++) {
77 update_ids[i].count = systems[i].step_dependency_count;
78 update_ids[i].ids = sk_malloc_t(int32_t, systems[i].step_dependency_count);
79
80 for (int32_t d = 0; d < systems[i].step_dependency_count; d++) {
81 update_ids[i].ids[d] = systems_find_id(systems[i].step_dependencies[d]);
82 if (update_ids[i].ids[d] == -1) {
83 log_errf("Can't find system update dependency by the name of %s!", systems[i].step_dependencies[d]);
84 result = 1;
85 }
86 }
87 }
88
89 // Sort sort the update order
90 if (result == 0) {
91 int32_t *update_order = sk_malloc_t(int32_t, systems.count);
92
93 result = topological_sort(update_ids, systems.count, update_order);
94 if (result != 0) log_errf("Invalid update dependencies! Cyclic dependency detected at %s!", systems[result].name);
95 else systems.reorder(update_order);
96
97 sk_free(update_order);
98 }
99
100 // Turn dependency names into indices for init
101 sort_dependency_t *init_ids = sk_malloc_t(sort_dependency_t, systems.count);
102 for (int32_t i = 0; i < systems.count; i++) {
103 init_ids[i].count = systems[i].init_dependency_count;
104 init_ids[i].ids = sk_malloc_t(int32_t, systems[i].init_dependency_count);
105
106 for (int32_t d = 0; d < systems[i].init_dependency_count; d++) {
107 init_ids[i].ids[d] = systems_find_id(systems[i].init_dependencies[d]);
108 if (init_ids[i].ids[d] == -1) {
109 log_errf("Can't find system init dependency by the name of %s!", systems[i].init_dependencies[d]);
110 result = 1;
111 }
112 }
113 }
114
115 // Sort the init order
116 if (result == 0) {
117 system_init_order = sk_malloc_t(int32_t, systems.count);
118 result = topological_sort(init_ids, systems.count, system_init_order);
119 if (result != 0) log_errf("Invalid initialization dependencies! Cyclic dependency detected at %s!", systems[result].name);
120 }
121
122 // Release memory
123 for (int32_t i = 0; i < systems.count; i++) {
124 sk_free(init_ids [i].ids);
125 sk_free(update_ids[i].ids);
126 }
127 sk_free(init_ids);

Callers 1

systems_initializeFunction · 0.85

Calls 4

systems_find_idFunction · 0.85
log_errfFunction · 0.85
topological_sortFunction · 0.85
reorderMethod · 0.80

Tested by

no test coverage detected