2 * Copyright © 2022 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Garret Rieger
27 #include "../hb-set.hh"
28 #include "../hb-priority-queue.hh"
29 #include "../hb-serialize.hh"
31 #ifndef GRAPH_GRAPH_HH
32 #define GRAPH_GRAPH_HH
37 * Represents a serialized table in the form of a graph.
38 * Provides methods for modifying and reordering the graph.
44 hb_serialize_context_t::object_t obj;
45 int64_t distance = 0 ;
49 unsigned priority = 0;
51 unsigned incoming_edges_ = 0;
52 unsigned single_parent = (unsigned) -1;
53 hb_hashmap_t<unsigned, unsigned> parents;
56 auto parents_iter () const HB_AUTO_RETURN
59 hb_iter (&single_parent, single_parent != (unsigned) -1),
64 bool in_error () const
66 return parents.in_error ();
69 bool link_positions_valid (unsigned num_objects, bool removed_nil)
71 hb_set_t assigned_bytes;
72 for (const auto& l : obj.real_links)
74 if (l.objidx >= num_objects
75 || (removed_nil && !l.objidx))
77 DEBUG_MSG (SUBSET_REPACK, nullptr,
78 "Invalid graph. Invalid object index.");
82 unsigned start = l.position;
83 unsigned end = start + l.width - 1;
85 if (unlikely (l.width < 2 || l.width > 4))
87 DEBUG_MSG (SUBSET_REPACK, nullptr,
88 "Invalid graph. Invalid link width.");
92 if (unlikely (end >= table_size ()))
94 DEBUG_MSG (SUBSET_REPACK, nullptr,
95 "Invalid graph. Link position is out of bounds.");
99 if (unlikely (assigned_bytes.intersects (start, end)))
101 DEBUG_MSG (SUBSET_REPACK, nullptr,
102 "Invalid graph. Found offsets whose positions overlap.");
106 assigned_bytes.add_range (start, end);
109 return !assigned_bytes.in_error ();
114 obj.real_links.qsort ();
115 for (auto& l : obj.real_links)
117 for (unsigned i = 0; i < l.width; i++)
119 obj.head[l.position + i] = 0;
124 bool equals (const vertex_t& other,
125 const graph_t& graph,
126 const graph_t& other_graph,
127 unsigned depth) const
129 if (!(as_bytes () == other.as_bytes ()))
131 DEBUG_MSG (SUBSET_REPACK, nullptr,
132 "vertex [%lu] bytes != [%lu] bytes, depth = %u",
133 (unsigned long) table_size (),
134 (unsigned long) other.table_size (),
137 auto a = as_bytes ();
138 auto b = other.as_bytes ();
141 DEBUG_MSG (SUBSET_REPACK, nullptr,
142 " 0x%x %s 0x%x", (unsigned) *a, (*a == *b) ? "==" : "!=", (unsigned) *b);
149 return links_equal (obj.real_links, other.obj.real_links, graph, other_graph, depth);
152 hb_bytes_t as_bytes () const
154 return hb_bytes_t (obj.head, table_size ());
157 friend void swap (vertex_t& a, vertex_t& b)
159 hb_swap (a.obj, b.obj);
160 hb_swap (a.distance, b.distance);
161 hb_swap (a.space, b.space);
162 hb_swap (a.single_parent, b.single_parent);
163 hb_swap (a.parents, b.parents);
164 hb_swap (a.incoming_edges_, b.incoming_edges_);
165 hb_swap (a.start, b.start);
166 hb_swap (a.end, b.end);
167 hb_swap (a.priority, b.priority);
170 hb_hashmap_t<unsigned, unsigned>
171 position_to_index_map () const
173 hb_hashmap_t<unsigned, unsigned> result;
175 result.alloc (obj.real_links.length);
176 for (const auto& l : obj.real_links) {
177 result.set (l.position, l.objidx);
183 bool is_shared () const
185 return parents.get_population () > 1;
188 unsigned incoming_edges () const
190 if (HB_DEBUG_SUBSET_REPACK)
192 assert (incoming_edges_ == (single_parent != (unsigned) -1) +
193 (parents.values_ref () | hb_reduce (hb_add, 0)));
195 return incoming_edges_;
198 void reset_parents ()
201 single_parent = (unsigned) -1;
205 void add_parent (unsigned parent_index)
207 assert (parent_index != (unsigned) -1);
208 if (incoming_edges_ == 0)
210 single_parent = parent_index;
214 else if (single_parent != (unsigned) -1)
216 assert (incoming_edges_ == 1);
217 if (!parents.set (single_parent, 1))
219 single_parent = (unsigned) -1;
223 if (parents.has (parent_index, &v))
228 else if (parents.set (parent_index, 1))
232 void remove_parent (unsigned parent_index)
234 if (parent_index == single_parent)
236 single_parent = (unsigned) -1;
242 if (parents.has (parent_index, &v))
248 parents.del (parent_index);
250 if (incoming_edges_ == 1)
252 single_parent = *parents.keys ();
258 void remove_real_link (unsigned child_index, const void* offset)
260 unsigned count = obj.real_links.length;
261 for (unsigned i = 0; i < count; i++)
263 auto& link = obj.real_links.arrayZ[i];
264 if (link.objidx != child_index)
267 if ((obj.head + link.position) != offset)
270 obj.real_links.remove_unordered (i);
275 bool remap_parents (const hb_vector_t<unsigned>& id_map)
277 if (single_parent != (unsigned) -1)
279 assert (single_parent < id_map.length);
280 single_parent = id_map[single_parent];
284 hb_hashmap_t<unsigned, unsigned> new_parents;
285 new_parents.alloc (parents.get_population ());
286 for (auto _ : parents)
288 assert (_.first < id_map.length);
289 assert (!new_parents.has (id_map[_.first]));
290 new_parents.set (id_map[_.first], _.second);
293 if (parents.in_error() || new_parents.in_error ())
296 parents = std::move (new_parents);
300 void remap_parent (unsigned old_index, unsigned new_index)
302 if (single_parent != (unsigned) -1)
304 if (single_parent == old_index)
305 single_parent = new_index;
310 if (parents.has (old_index, &pv))
313 if (!parents.set (new_index, v))
314 incoming_edges_ -= v;
315 parents.del (old_index);
317 if (incoming_edges_ == 1)
319 single_parent = *parents.keys ();
325 bool is_leaf () const
327 return !obj.real_links.length && !obj.virtual_links.length;
330 bool raise_priority ()
332 if (has_max_priority ()) return false;
337 bool has_max_priority () const {
338 return priority >= 3;
341 size_t table_size () const {
342 return obj.tail - obj.head;
345 int64_t modified_distance (unsigned order) const
347 // TODO(garretrieger): once priority is high enough, should try
348 // setting distance = 0 which will force to sort immediately after
349 // it's parent where possible.
351 int64_t modified_distance =
352 hb_min (hb_max(distance + distance_modifier (), 0), 0x7FFFFFFFFFF);
353 if (has_max_priority ()) {
354 modified_distance = 0;
356 return (modified_distance << 18) | (0x003FFFF & order);
359 int64_t distance_modifier () const
361 if (!priority) return 0;
362 int64_t table_size = obj.tail - obj.head;
365 return -table_size / 2;
371 bool links_equal (const hb_vector_t<hb_serialize_context_t::object_t::link_t>& this_links,
372 const hb_vector_t<hb_serialize_context_t::object_t::link_t>& other_links,
373 const graph_t& graph,
374 const graph_t& other_graph,
375 unsigned depth) const
377 auto a = this_links.iter ();
378 auto b = other_links.iter ();
382 const auto& link_a = *a;
383 const auto& link_b = *b;
385 if (link_a.width != link_b.width ||
386 link_a.is_signed != link_b.is_signed ||
387 link_a.whence != link_b.whence ||
388 link_a.position != link_b.position ||
389 link_a.bias != link_b.bias)
392 if (!graph.vertices_[link_a.objidx].equals (
393 other_graph.vertices_[link_b.objidx], graph, other_graph, depth + 1))
400 if (bool (a) != bool (b))
407 template <typename T>
408 struct vertex_and_table_t
410 vertex_and_table_t () : index (0), vertex (nullptr), table (nullptr)
418 return table && vertex;
423 * A topological sorting of an object graph. Ordered
424 * in reverse serialization order (first object in the
425 * serialization is at the end of the list). This matches
426 * the 'packed' object stack used internally in the
430 graph_t (const T& objects)
431 : parents_invalid (true),
432 distance_invalid (true),
433 positions_invalid (true),
437 num_roots_for_space_.push (1);
438 bool removed_nil = false;
439 vertices_.alloc (objects.length);
440 vertices_scratch_.alloc (objects.length);
441 unsigned count = objects.length;
442 for (unsigned i = 0; i < count; i++)
444 // If this graph came from a serialization buffer object 0 is the
445 // nil object. We don't need it for our purposes here so drop it.
446 if (i == 0 && !objects.arrayZ[i])
452 vertex_t* v = vertices_.push ();
453 if (check_success (!vertices_.in_error ()))
454 v->obj = *objects.arrayZ[i];
456 check_success (v->link_positions_valid (count, removed_nil));
458 if (!removed_nil) continue;
459 // Fix indices to account for removed nil object.
460 for (auto& l : v->obj.all_links_writer ()) {
468 for (char* b : buffers)
472 bool operator== (const graph_t& other) const
474 return root ().equals (other.root (), *this, other, 0);
477 void print () const {
478 for (int i = vertices_.length - 1; i >= 0; i--)
480 const auto& v = vertices_[i];
481 printf("%d: %u [", i, (unsigned int)v.table_size());
482 for (const auto &l : v.obj.real_links) {
483 printf("%u, ", l.objidx);
489 // Sorts links of all objects in a consistent manner and zeroes all offsets.
492 for (auto& v : vertices_.writer ())
496 bool in_error () const
498 return !successful ||
499 vertices_.in_error () ||
500 num_roots_for_space_.in_error ();
503 const vertex_t& root () const
505 return vertices_[root_idx ()];
508 unsigned root_idx () const
510 // Object graphs are in reverse order, the first object is at the end
511 // of the vector. Since the graph is topologically sorted it's safe to
512 // assume the first object has no incoming edges.
513 return vertices_.length - 1;
516 const hb_serialize_context_t::object_t& object (unsigned i) const
518 return vertices_[i].obj;
521 bool add_buffer (char* buffer)
523 buffers.push (buffer);
524 return !buffers.in_error ();
528 * Adds a 16 bit link from parent_id to child_id
531 void add_link (T* offset,
535 auto& v = vertices_[parent_id];
536 auto* link = v.obj.real_links.push ();
538 link->objidx = child_id;
539 link->position = (char*) offset - (char*) v.obj.head;
540 vertices_[child_id].add_parent (parent_id);
544 * Generates a new topological sorting of graph ordered by the shortest
545 * distance to each node if positions are marked as invalid.
547 void sort_shortest_distance_if_needed ()
549 if (!positions_invalid) return;
550 sort_shortest_distance ();
555 * Generates a new topological sorting of graph ordered by the shortest
556 * distance to each node.
558 void sort_shortest_distance ()
560 positions_invalid = true;
562 if (vertices_.length <= 1) {
563 // Graph of 1 or less doesn't need sorting.
569 hb_priority_queue_t<int64_t> queue;
570 hb_vector_t<vertex_t> &sorted_graph = vertices_scratch_;
571 if (unlikely (!check_success (sorted_graph.resize (vertices_.length)))) return;
572 hb_vector_t<unsigned> id_map;
573 if (unlikely (!check_success (id_map.resize (vertices_.length)))) return;
575 hb_vector_t<unsigned> removed_edges;
576 if (unlikely (!check_success (removed_edges.resize (vertices_.length)))) return;
579 queue.insert (root ().modified_distance (0), root_idx ());
580 int new_id = root_idx ();
582 while (!queue.in_error () && !queue.is_empty ())
584 unsigned next_id = queue.pop_minimum().second;
586 sorted_graph[new_id] = std::move (vertices_[next_id]);
587 const vertex_t& next = sorted_graph[new_id];
589 if (unlikely (!check_success(new_id >= 0))) {
590 // We are out of ids. Which means we've visited a node more than once.
591 // This graph contains a cycle which is not allowed.
592 DEBUG_MSG (SUBSET_REPACK, nullptr, "Invalid graph. Contains cycle.");
596 id_map[next_id] = new_id--;
598 for (const auto& link : next.obj.all_links ()) {
599 removed_edges[link.objidx]++;
600 if (!(vertices_[link.objidx].incoming_edges () - removed_edges[link.objidx]))
601 // Add the order that the links were encountered to the priority.
602 // This ensures that ties between priorities objects are broken in a consistent
603 // way. More specifically this is set up so that if a set of objects have the same
604 // distance they'll be added to the topological order in the order that they are
605 // referenced from the parent object.
606 queue.insert (vertices_[link.objidx].modified_distance (order++),
611 check_success (!queue.in_error ());
612 check_success (!sorted_graph.in_error ());
614 check_success (remap_all_obj_indices (id_map, &sorted_graph));
615 vertices_ = std::move (sorted_graph);
617 if (!check_success (new_id == -1))
618 print_orphaned_nodes ();
622 * Finds the set of nodes (placed into roots) that should be assigned unique spaces.
623 * More specifically this looks for the top most 24 bit or 32 bit links in the graph.
624 * Some special casing is done that is specific to the layout of GSUB/GPOS tables.
626 void find_space_roots (hb_set_t& visited, hb_set_t& roots)
628 int root_index = (int) root_idx ();
629 for (int i = root_index; i >= 0; i--)
631 if (visited.has (i)) continue;
633 // Only real links can form 32 bit spaces
634 for (auto& l : vertices_[i].obj.real_links)
636 if (l.is_signed || l.width < 3)
639 if (i == root_index && l.width == 3)
640 // Ignore 24bit links from the root node, this skips past the single 24bit
641 // pointer to the lookup list.
646 // A 24bit offset forms a root, unless there is 32bit offsets somewhere
647 // in it's subgraph, then those become the roots instead. This is to make sure
648 // that extension subtables beneath a 24bit lookup become the spaces instead
649 // of the offset to the lookup.
651 find_32bit_roots (l.objidx, sub_roots);
653 for (unsigned sub_root_idx : sub_roots) {
654 roots.add (sub_root_idx);
655 find_subgraph (sub_root_idx, visited);
661 roots.add (l.objidx);
662 find_subgraph (l.objidx, visited);
667 template <typename T, typename ...Ts>
668 vertex_and_table_t<T> as_table (unsigned parent, const void* offset, Ts... ds)
670 return as_table_from_index<T> (index_for_offset (parent, offset), std::forward<Ts>(ds)...);
673 template <typename T, typename ...Ts>
674 vertex_and_table_t<T> as_mutable_table (unsigned parent, const void* offset, Ts... ds)
676 return as_table_from_index<T> (mutable_index_for_offset (parent, offset), std::forward<Ts>(ds)...);
679 template <typename T, typename ...Ts>
680 vertex_and_table_t<T> as_table_from_index (unsigned index, Ts... ds)
682 if (index >= vertices_.length)
683 return vertex_and_table_t<T> ();
685 vertex_and_table_t<T> r;
686 r.vertex = &vertices_[index];
687 r.table = (T*) r.vertex->obj.head;
690 return vertex_and_table_t<T> ();
692 if (!r.table->sanitize (*(r.vertex), std::forward<Ts>(ds)...))
693 return vertex_and_table_t<T> ();
698 // Finds the object id of the object pointed to by the offset at 'offset'
699 // within object[node_idx].
700 unsigned index_for_offset (unsigned node_idx, const void* offset) const
702 const auto& node = object (node_idx);
703 if (offset < node.head || offset >= node.tail) return -1;
705 unsigned count = node.real_links.length;
706 for (unsigned i = 0; i < count; i++)
708 // Use direct access for increased performance, this is a hot method.
709 const auto& link = node.real_links.arrayZ[i];
710 if (offset != node.head + link.position)
718 // Finds the object id of the object pointed to by the offset at 'offset'
719 // within object[node_idx]. Ensures that the returned object is safe to mutate.
720 // That is, if the original child object is shared by parents other than node_idx
721 // it will be duplicated and the duplicate will be returned instead.
722 unsigned mutable_index_for_offset (unsigned node_idx, const void* offset)
724 unsigned child_idx = index_for_offset (node_idx, offset);
725 auto& child = vertices_[child_idx];
726 for (unsigned p : child.parents_iter ())
729 return duplicate (node_idx, child_idx);
738 * Assign unique space numbers to each connected subgraph of 24 bit and/or 32 bit offset(s).
739 * Currently, this is implemented specifically tailored to the structure of a GPOS/GSUB
740 * (including with 24bit offsets) table.
742 bool assign_spaces ()
748 find_space_roots (visited, roots);
750 // Mark everything not in the subgraphs of the roots as visited. This prevents
751 // subgraphs from being connected via nodes not in those subgraphs.
754 if (!roots) return false;
758 uint32_t next = HB_SET_VALUE_INVALID;
759 if (unlikely (!check_success (!roots.in_error ()))) break;
760 if (!roots.next (&next)) break;
762 hb_set_t connected_roots;
763 find_connected_nodes (next, roots, visited, connected_roots);
764 if (unlikely (!check_success (!connected_roots.in_error ()))) break;
766 isolate_subgraph (connected_roots);
767 if (unlikely (!check_success (!connected_roots.in_error ()))) break;
769 unsigned next_space = this->next_space ();
770 num_roots_for_space_.push (0);
771 for (unsigned root : connected_roots)
773 DEBUG_MSG (SUBSET_REPACK, nullptr, "Subgraph %u gets space %u", root, next_space);
774 vertices_[root].space = next_space;
775 num_roots_for_space_[next_space] = num_roots_for_space_[next_space] + 1;
776 distance_invalid = true;
777 positions_invalid = true;
780 // TODO(grieger): special case for GSUB/GPOS use extension promotions to move 16 bit space
781 // into the 32 bit space as needed, instead of using isolation.
790 * Isolates the subgraph of nodes reachable from root. Any links to nodes in the subgraph
791 * that originate from outside of the subgraph will be removed by duplicating the linked to
794 * Indices stored in roots will be updated if any of the roots are duplicated to new indices.
796 bool isolate_subgraph (hb_set_t& roots)
801 // incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these
802 // set the subgraph incoming edge count to match all of root_idx's incoming edges
804 for (unsigned root_idx : roots)
806 subgraph.set (root_idx, wide_parents (root_idx, parents));
807 find_subgraph (root_idx, subgraph);
809 if (subgraph.in_error ())
812 unsigned original_root_idx = root_idx ();
814 bool made_changes = false;
815 for (auto entry : subgraph.iter ())
817 assert (entry.first < vertices_.length);
818 const auto& node = vertices_[entry.first];
819 unsigned subgraph_incoming_edges = entry.second;
821 if (subgraph_incoming_edges < node.incoming_edges ())
823 // Only de-dup objects with incoming links from outside the subgraph.
825 duplicate_subgraph (entry.first, index_map);
835 if (original_root_idx != root_idx ()
836 && parents.has (original_root_idx))
838 // If the root idx has changed since parents was determined, update root idx in parents
839 parents.add (root_idx ());
840 parents.del (original_root_idx);
845 | hb_map([&] (uint32_t node_idx) {
847 if (index_map.has (node_idx, &v)) return *v;
852 remap_obj_indices (index_map, new_subgraph);
853 remap_obj_indices (index_map, parents.iter (), true);
855 // Update roots set with new indices as needed.
856 for (auto next : roots)
859 if (index_map.has (next, &v))
869 void find_subgraph (unsigned node_idx, hb_map_t& subgraph)
871 for (const auto& link : vertices_[node_idx].obj.all_links ())
874 if (subgraph.has (link.objidx, &v))
879 subgraph.set (link.objidx, 1);
880 find_subgraph (link.objidx, subgraph);
884 void find_subgraph (unsigned node_idx, hb_set_t& subgraph)
886 if (subgraph.has (node_idx)) return;
887 subgraph.add (node_idx);
888 for (const auto& link : vertices_[node_idx].obj.all_links ())
889 find_subgraph (link.objidx, subgraph);
892 size_t find_subgraph_size (unsigned node_idx, hb_set_t& subgraph, unsigned max_depth = -1)
894 if (subgraph.has (node_idx)) return 0;
895 subgraph.add (node_idx);
897 const auto& o = vertices_[node_idx].obj;
898 size_t size = o.tail - o.head;
902 for (const auto& link : o.all_links ())
903 size += find_subgraph_size (link.objidx, subgraph, max_depth - 1);
908 * Finds the topmost children of 32bit offsets in the subgraph starting
909 * at node_idx. Found indices are placed into 'found'.
911 void find_32bit_roots (unsigned node_idx, hb_set_t& found)
913 for (const auto& link : vertices_[node_idx].obj.all_links ())
915 if (!link.is_signed && link.width == 4) {
916 found.add (link.objidx);
919 find_32bit_roots (link.objidx, found);
924 * Moves the child of old_parent_idx pointed to by old_offset to a new
925 * vertex at the new_offset.
928 void move_child (unsigned old_parent_idx,
930 unsigned new_parent_idx,
933 distance_invalid = true;
934 positions_invalid = true;
936 auto& old_v = vertices_[old_parent_idx];
937 auto& new_v = vertices_[new_parent_idx];
939 unsigned child_id = index_for_offset (old_parent_idx,
942 auto* new_link = new_v.obj.real_links.push ();
943 new_link->width = O::static_size;
944 new_link->objidx = child_id;
945 new_link->position = (const char*) new_offset - (const char*) new_v.obj.head;
947 auto& child = vertices_[child_id];
948 child.add_parent (new_parent_idx);
950 old_v.remove_real_link (child_id, old_offset);
951 child.remove_parent (old_parent_idx);
955 * duplicates all nodes in the subgraph reachable from node_idx. Does not re-assign
956 * links. index_map is updated with mappings from old id to new id. If a duplication has already
957 * been performed for a given index, then it will be skipped.
959 void duplicate_subgraph (unsigned node_idx, hb_map_t& index_map)
961 if (index_map.has (node_idx))
964 unsigned clone_idx = duplicate (node_idx);
965 if (!check_success (clone_idx != (unsigned) -1))
968 index_map.set (node_idx, clone_idx);
969 for (const auto& l : object (node_idx).all_links ()) {
970 duplicate_subgraph (l.objidx, index_map);
975 * Creates a copy of node_idx and returns it's new index.
977 unsigned duplicate (unsigned node_idx)
979 positions_invalid = true;
980 distance_invalid = true;
982 auto* clone = vertices_.push ();
983 auto& child = vertices_[node_idx];
984 if (vertices_.in_error ()) {
988 clone->obj.head = child.obj.head;
989 clone->obj.tail = child.obj.tail;
990 clone->distance = child.distance;
991 clone->space = child.space;
992 clone->reset_parents ();
994 unsigned clone_idx = vertices_.length - 2;
995 for (const auto& l : child.obj.real_links)
997 clone->obj.real_links.push (l);
998 vertices_[l.objidx].add_parent (clone_idx);
1000 for (const auto& l : child.obj.virtual_links)
1002 clone->obj.virtual_links.push (l);
1003 vertices_[l.objidx].add_parent (clone_idx);
1006 check_success (!clone->obj.real_links.in_error ());
1007 check_success (!clone->obj.virtual_links.in_error ());
1009 // The last object is the root of the graph, so swap back the root to the end.
1010 // The root's obj idx does change, however since it's root nothing else refers to it.
1011 // all other obj idx's will be unaffected.
1012 hb_swap (vertices_[vertices_.length - 2], *clone);
1014 // Since the root moved, update the parents arrays of all children on the root.
1015 for (const auto& l : root ().obj.all_links ())
1016 vertices_[l.objidx].remap_parent (root_idx () - 1, root_idx ());
1022 * Creates a copy of child and re-assigns the link from
1023 * parent to the clone. The copy is a shallow copy, objects
1024 * linked from child are not duplicated.
1026 unsigned duplicate_if_shared (unsigned parent_idx, unsigned child_idx)
1028 unsigned new_idx = duplicate (parent_idx, child_idx);
1029 if (new_idx == (unsigned) -1) return child_idx;
1035 * Creates a copy of child and re-assigns the link from
1036 * parent to the clone. The copy is a shallow copy, objects
1037 * linked from child are not duplicated.
1039 unsigned duplicate (unsigned parent_idx, unsigned child_idx)
1043 unsigned links_to_child = 0;
1044 for (const auto& l : vertices_[parent_idx].obj.all_links ())
1046 if (l.objidx == child_idx) links_to_child++;
1049 if (vertices_[child_idx].incoming_edges () <= links_to_child)
1051 // Can't duplicate this node, doing so would orphan the original one as all remaining links
1052 // to child are from parent.
1053 DEBUG_MSG (SUBSET_REPACK, nullptr, " Not duplicating %u => %u",
1054 parent_idx, child_idx);
1058 DEBUG_MSG (SUBSET_REPACK, nullptr, " Duplicating %u => %u",
1059 parent_idx, child_idx);
1061 unsigned clone_idx = duplicate (child_idx);
1062 if (clone_idx == (unsigned) -1) return false;
1063 // duplicate shifts the root node idx, so if parent_idx was root update it.
1064 if (parent_idx == clone_idx) parent_idx++;
1066 auto& parent = vertices_[parent_idx];
1067 for (auto& l : parent.obj.all_links_writer ())
1069 if (l.objidx != child_idx)
1072 reassign_link (l, parent_idx, clone_idx);
1080 * Adds a new node to the graph, not connected to anything.
1082 unsigned new_node (char* head, char* tail)
1084 positions_invalid = true;
1085 distance_invalid = true;
1087 auto* clone = vertices_.push ();
1088 if (vertices_.in_error ()) {
1092 clone->obj.head = head;
1093 clone->obj.tail = tail;
1094 clone->distance = 0;
1097 unsigned clone_idx = vertices_.length - 2;
1099 // The last object is the root of the graph, so swap back the root to the end.
1100 // The root's obj idx does change, however since it's root nothing else refers to it.
1101 // all other obj idx's will be unaffected.
1102 hb_swap (vertices_[vertices_.length - 2], *clone);
1104 // Since the root moved, update the parents arrays of all children on the root.
1105 for (const auto& l : root ().obj.all_links ())
1106 vertices_[l.objidx].remap_parent (root_idx () - 1, root_idx ());
1112 * Raises the sorting priority of all children.
1114 bool raise_childrens_priority (unsigned parent_idx)
1116 DEBUG_MSG (SUBSET_REPACK, nullptr, " Raising priority of all children of %u",
1118 // This operation doesn't change ordering until a sort is run, so no need
1119 // to invalidate positions. It does not change graph structure so no need
1120 // to update distances or edge counts.
1121 auto& parent = vertices_[parent_idx].obj;
1122 bool made_change = false;
1123 for (auto& l : parent.all_links_writer ())
1124 made_change |= vertices_[l.objidx].raise_priority ();
1128 bool is_fully_connected ()
1132 if (root().incoming_edges ())
1133 // Root cannot have parents.
1136 for (unsigned i = 0; i < root_idx (); i++)
1138 if (!vertices_[i].incoming_edges ())
1146 * Saves the current graph to a packed binary format which the repacker fuzzer takes
1149 void save_fuzzer_seed (hb_tag_t tag) const
1151 FILE* f = fopen ("./repacker_fuzzer_seed", "w");
1152 fwrite ((void*) &tag, sizeof (tag), 1, f);
1154 uint16_t num_objects = vertices_.length;
1155 fwrite ((void*) &num_objects, sizeof (num_objects), 1, f);
1157 for (const auto& v : vertices_)
1159 uint16_t blob_size = v.table_size ();
1160 fwrite ((void*) &blob_size, sizeof (blob_size), 1, f);
1161 fwrite ((const void*) v.obj.head, blob_size, 1, f);
1164 uint16_t link_count = 0;
1165 for (const auto& v : vertices_)
1166 link_count += v.obj.real_links.length;
1168 fwrite ((void*) &link_count, sizeof (link_count), 1, f);
1178 for (unsigned i = 0; i < vertices_.length; i++)
1180 for (const auto& l : vertices_[i].obj.real_links)
1183 (uint16_t) i, (uint16_t) l.objidx,
1184 (uint16_t) l.position, (uint8_t) l.width
1186 fwrite ((void*) &link, sizeof (link), 1, f);
1194 void print_orphaned_nodes ()
1196 if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
1198 DEBUG_MSG (SUBSET_REPACK, nullptr, "Graph is not fully connected.");
1199 parents_invalid = true;
1202 if (root().incoming_edges ()) {
1203 DEBUG_MSG (SUBSET_REPACK, nullptr, "Root node has incoming edges.");
1206 for (unsigned i = 0; i < root_idx (); i++)
1208 const auto& v = vertices_[i];
1209 if (!v.incoming_edges ())
1210 DEBUG_MSG (SUBSET_REPACK, nullptr, "Node %u is orphaned.", i);
1214 unsigned num_roots_for_space (unsigned space) const
1216 return num_roots_for_space_[space];
1219 unsigned next_space () const
1221 return num_roots_for_space_.length;
1224 void move_to_new_space (const hb_set_t& indices)
1226 num_roots_for_space_.push (0);
1227 unsigned new_space = num_roots_for_space_.length - 1;
1229 for (unsigned index : indices) {
1230 auto& node = vertices_[index];
1231 num_roots_for_space_[node.space] = num_roots_for_space_[node.space] - 1;
1232 num_roots_for_space_[new_space] = num_roots_for_space_[new_space] + 1;
1233 node.space = new_space;
1234 distance_invalid = true;
1235 positions_invalid = true;
1239 unsigned space_for (unsigned index, unsigned* root = nullptr) const
1242 assert (index < vertices_.length);
1243 const auto& node = vertices_[index];
1246 if (root != nullptr)
1251 if (!node.incoming_edges ())
1258 index = *node.parents_iter ();
1262 void err_other_error () { this->successful = false; }
1264 size_t total_size_in_bytes () const {
1265 size_t total_size = 0;
1266 unsigned count = vertices_.length;
1267 for (unsigned i = 0; i < count; i++) {
1268 size_t size = vertices_.arrayZ[i].obj.tail - vertices_.arrayZ[i].obj.head;
1278 * Returns the numbers of incoming edges that are 24 or 32 bits wide.
1280 unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const
1283 for (unsigned p : vertices_[node_idx].parents_iter ())
1285 // Only real links can be wide
1286 for (const auto& l : vertices_[p].obj.real_links)
1288 if (l.objidx == node_idx
1289 && (l.width == 3 || l.width == 4)
1300 bool check_success (bool success)
1301 { return this->successful && (success || ((void) err_other_error (), false)); }
1305 * Creates a map from objid to # of incoming edges.
1307 void update_parents ()
1309 if (!parents_invalid) return;
1311 unsigned count = vertices_.length;
1313 for (unsigned i = 0; i < count; i++)
1314 vertices_.arrayZ[i].reset_parents ();
1316 for (unsigned p = 0; p < count; p++)
1318 for (auto& l : vertices_.arrayZ[p].obj.all_links ())
1319 vertices_[l.objidx].add_parent (p);
1322 for (unsigned i = 0; i < count; i++)
1323 // parents arrays must be accurate or downstream operations like cycle detection
1324 // and sorting won't work correctly.
1325 check_success (!vertices_.arrayZ[i].in_error ());
1327 parents_invalid = false;
1331 * compute the serialized start and end positions for each vertex.
1333 void update_positions ()
1335 if (!positions_invalid) return;
1337 unsigned current_pos = 0;
1338 for (int i = root_idx (); i >= 0; i--)
1340 auto& v = vertices_[i];
1341 v.start = current_pos;
1342 current_pos += v.obj.tail - v.obj.head;
1343 v.end = current_pos;
1346 positions_invalid = false;
1350 * Finds the distance to each object in the graph
1351 * from the initial node.
1353 void update_distances ()
1355 if (!distance_invalid) return;
1357 // Uses Dijkstra's algorithm to find all of the shortest distances.
1358 // https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
1360 // Implementation Note:
1361 // Since our priority queue doesn't support fast priority decreases
1362 // we instead just add new entries into the queue when a priority changes.
1363 // Redundant ones are filtered out later on by the visited set.
1364 // According to https://www3.cs.stonybrook.edu/~rezaul/papers/TR-07-54.pdf
1365 // for practical performance this is faster then using a more advanced queue
1366 // (such as a fibonacci queue) with a fast decrease priority.
1367 unsigned count = vertices_.length;
1368 for (unsigned i = 0; i < count; i++)
1369 vertices_.arrayZ[i].distance = hb_int_max (int64_t);
1370 vertices_.tail ().distance = 0;
1372 hb_priority_queue_t<int64_t> queue;
1373 queue.insert (0, vertices_.length - 1);
1375 hb_vector_t<bool> visited;
1376 visited.resize (vertices_.length);
1378 while (!queue.in_error () && !queue.is_empty ())
1380 unsigned next_idx = queue.pop_minimum ().second;
1381 if (visited[next_idx]) continue;
1382 const auto& next = vertices_[next_idx];
1383 int64_t next_distance = vertices_[next_idx].distance;
1384 visited[next_idx] = true;
1386 for (const auto& link : next.obj.all_links ())
1388 if (visited[link.objidx]) continue;
1390 const auto& child = vertices_.arrayZ[link.objidx].obj;
1391 unsigned link_width = link.width ? link.width : 4; // treat virtual offsets as 32 bits wide
1392 int64_t child_weight = (child.tail - child.head) +
1393 ((int64_t) 1 << (link_width * 8)) * (vertices_.arrayZ[link.objidx].space + 1);
1394 int64_t child_distance = next_distance + child_weight;
1396 if (child_distance < vertices_.arrayZ[link.objidx].distance)
1398 vertices_.arrayZ[link.objidx].distance = child_distance;
1399 queue.insert (child_distance, link.objidx);
1404 check_success (!queue.in_error ());
1405 if (!check_success (queue.is_empty ()))
1407 print_orphaned_nodes ();
1411 distance_invalid = false;
1416 * Updates a link in the graph to point to a different object. Corrects the
1417 * parents vector on the previous and new child nodes.
1419 void reassign_link (hb_serialize_context_t::object_t::link_t& link,
1420 unsigned parent_idx,
1423 unsigned old_idx = link.objidx;
1424 link.objidx = new_idx;
1425 vertices_[old_idx].remove_parent (parent_idx);
1426 vertices_[new_idx].add_parent (parent_idx);
1430 * Updates all objidx's in all links using the provided mapping. Corrects incoming edge counts.
1432 template<typename Iterator, hb_requires (hb_is_iterator (Iterator))>
1433 void remap_obj_indices (const hb_map_t& id_map,
1435 bool only_wide = false)
1437 if (!id_map) return;
1438 for (unsigned i : subgraph)
1440 for (auto& link : vertices_[i].obj.all_links_writer ())
1443 if (!id_map.has (link.objidx, &v)) continue;
1444 if (only_wide && !(link.width == 4 && !link.is_signed)) continue;
1446 reassign_link (link, i, *v);
1452 * Updates all objidx's in all links using the provided mapping.
1454 bool remap_all_obj_indices (const hb_vector_t<unsigned>& id_map,
1455 hb_vector_t<vertex_t>* sorted_graph) const
1457 unsigned count = sorted_graph->length;
1458 for (unsigned i = 0; i < count; i++)
1460 if (!(*sorted_graph)[i].remap_parents (id_map))
1462 for (auto& link : sorted_graph->arrayZ[i].obj.all_links_writer ())
1464 link.objidx = id_map[link.objidx];
1471 * Finds all nodes in targets that are reachable from start_idx, nodes in visited will be skipped.
1472 * For this search the graph is treated as being undirected.
1474 * Connected targets will be added to connected and removed from targets. All visited nodes
1475 * will be added to visited.
1477 void find_connected_nodes (unsigned start_idx,
1480 hb_set_t& connected)
1482 if (unlikely (!check_success (!visited.in_error ()))) return;
1483 if (visited.has (start_idx)) return;
1484 visited.add (start_idx);
1486 if (targets.has (start_idx))
1488 targets.del (start_idx);
1489 connected.add (start_idx);
1492 const auto& v = vertices_[start_idx];
1494 // Graph is treated as undirected so search children and parents of start_idx
1495 for (const auto& l : v.obj.all_links ())
1496 find_connected_nodes (l.objidx, targets, visited, connected);
1498 for (unsigned p : v.parents_iter ())
1499 find_connected_nodes (p, targets, visited, connected);
1503 // TODO(garretrieger): make private, will need to move most of offset overflow code into graph.
1504 hb_vector_t<vertex_t> vertices_;
1505 hb_vector_t<vertex_t> vertices_scratch_;
1507 bool parents_invalid;
1508 bool distance_invalid;
1509 bool positions_invalid;
1511 hb_vector_t<unsigned> num_roots_for_space_;
1512 hb_vector_t<char*> buffers;
1517 #endif // GRAPH_GRAPH_HH