Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <vppinfra/ptclosure.h> |
| 17 | #include <vppinfra/hash.h> |
| 18 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 19 | typedef struct |
| 20 | { |
| 21 | uword *index_by_name; |
| 22 | u8 *items; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 23 | } test_main_t; |
| 24 | |
| 25 | test_main_t test_main; |
| 26 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 27 | static char *items[] = { |
| 28 | "d", |
| 29 | "a", |
| 30 | "b", |
| 31 | "c", |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 34 | char *constraints[] = { |
| 35 | "a,b", |
| 36 | "b,c", |
| 37 | "d,b", |
| 38 | // "c,a", /* no partial order possible */ |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 41 | u32 |
| 42 | vl (void *p) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 43 | { |
| 44 | return vec_len (p); |
| 45 | } |
| 46 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 47 | static void |
| 48 | dump_closure (test_main_t * tm, char *s, u8 ** orig) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 49 | { |
| 50 | int i, j; |
| 51 | |
| 52 | fformat (stdout, "--------- %s --------------\n", s); |
| 53 | for (i = 0; i < vec_len (orig); i++) |
| 54 | { |
| 55 | for (j = 0; j < vec_len (orig); j++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | if (orig[i][j]) |
| 57 | { |
| 58 | fformat (stdout, "%s <before> %s\n", items[i], items[j]); |
| 59 | } |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 63 | int |
| 64 | comma_split (u8 * s, u8 ** a, u8 ** b) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 65 | { |
| 66 | *a = s; |
| 67 | |
| 68 | while (*s && *s != ',') |
| 69 | s++; |
| 70 | |
| 71 | if (*s == ',') |
| 72 | *s = 0; |
| 73 | else |
| 74 | return 1; |
| 75 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | *b = (u8 *) (s + 1); |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 80 | int |
| 81 | test_ptclosure_main (unformat_input_t * input) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 82 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | test_main_t *tm = &test_main; |
| 84 | u8 *item_name; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 85 | int i, j; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | u8 **orig; |
| 87 | u8 **closure; |
| 88 | u8 *a_name, *b_name; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 89 | int a_index, b_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 90 | uword *p; |
| 91 | u8 *this_constraint; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 92 | int n; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | u32 *result = 0; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 94 | |
| 95 | tm->index_by_name = hash_create_string (0, sizeof (uword)); |
| 96 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 97 | n = ARRAY_LEN (items); |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < n; i++) |
| 100 | { |
| 101 | item_name = (u8 *) items[i]; |
| 102 | hash_set_mem (tm->index_by_name, item_name, i); |
| 103 | } |
| 104 | |
| 105 | orig = clib_ptclosure_alloc (n); |
| 106 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 107 | for (i = 0; i < ARRAY_LEN (constraints); i++) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 108 | { |
| 109 | this_constraint = format (0, "%s%c", constraints[i], 0); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 110 | |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 111 | if (comma_split (this_constraint, &a_name, &b_name)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 112 | { |
| 113 | clib_warning ("couldn't split '%s'", constraints[i]); |
| 114 | return 1; |
| 115 | } |
| 116 | |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 117 | p = hash_get_mem (tm->index_by_name, a_name); |
| 118 | if (p == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | { |
| 120 | clib_warning ("couldn't find '%s'", a_name); |
| 121 | return 1; |
| 122 | } |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 123 | a_index = p[0]; |
| 124 | |
| 125 | p = hash_get_mem (tm->index_by_name, b_name); |
| 126 | if (p == 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 127 | { |
| 128 | clib_warning ("couldn't find '%s'", b_name); |
| 129 | return 1; |
| 130 | } |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 131 | b_index = p[0]; |
| 132 | |
| 133 | orig[a_index][b_index] = 1; |
| 134 | vec_free (this_constraint); |
| 135 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 136 | |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 137 | dump_closure (tm, "original relation", orig); |
| 138 | |
| 139 | closure = clib_ptclosure (orig); |
| 140 | |
| 141 | dump_closure (tm, "closure", closure); |
| 142 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 143 | /* |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 144 | * Output partial order |
| 145 | */ |
| 146 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 147 | again: |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 148 | for (i = 0; i < n; i++) |
| 149 | { |
| 150 | for (j = 0; j < n; j++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 151 | { |
| 152 | if (closure[i][j]) |
| 153 | goto item_constrained; |
| 154 | } |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 155 | /* Item i can be output */ |
| 156 | vec_add1 (result, i); |
| 157 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 158 | int k; |
| 159 | for (k = 0; k < n; k++) |
| 160 | closure[k][i] = 0; |
| 161 | /* "Magic" a before a, to keep from ever outputting it again */ |
| 162 | closure[i][i] = 1; |
| 163 | goto again; |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 164 | } |
| 165 | item_constrained: |
| 166 | ; |
| 167 | } |
| 168 | |
| 169 | if (vec_len (result) != n) |
| 170 | { |
| 171 | clib_warning ("no partial order exists"); |
| 172 | exit (1); |
| 173 | } |
| 174 | |
| 175 | fformat (stdout, "Partial order:\n"); |
| 176 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 177 | for (i = vec_len (result) - 1; i >= 0; i--) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 178 | { |
| 179 | fformat (stdout, "%s\n", items[result[i]]); |
| 180 | } |
| 181 | |
| 182 | vec_free (result); |
| 183 | clib_ptclosure_free (orig); |
| 184 | clib_ptclosure_free (closure); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 190 | int |
| 191 | main (int argc, char *argv[]) |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 192 | { |
| 193 | unformat_input_t i; |
| 194 | int ret; |
| 195 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 196 | clib_mem_init (0, 3ULL << 30); |
Dave Barach | d653460 | 2016-06-14 18:38:02 -0400 | [diff] [blame] | 197 | |
| 198 | unformat_init_command_line (&i, argv); |
| 199 | ret = test_ptclosure_main (&i); |
| 200 | unformat_free (&i); |
| 201 | |
| 202 | return ret; |
| 203 | } |
| 204 | #endif /* CLIB_UNIX */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * fd.io coding-style-patch-verification: ON |
| 208 | * |
| 209 | * Local Variables: |
| 210 | * eval: (c-set-style "gnu") |
| 211 | * End: |
| 212 | */ |