SheafSystem  0.0.0.0
depth_first_itr.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #ifndef DEPTH_FIRST_ITR_H
22 #define DEPTH_FIRST_ITR_H
23 
24 #ifndef SHEAF_DLL_SPEC_H
25 #include "SheafSystem/sheaf_dll_spec.h"
26 #endif
27 
28 #ifndef ANY_H
29 #include "SheafSystem/any.h"
30 #endif
31 
32 #ifndef STD_UNORDERED_SET_H
33 #include "SheafSystem/std_unordered_set.h"
34 #endif
35 
36 #ifndef POSET_STATE_HANDLE_H
37 #include "SheafSystem/poset_state_handle.h"
38 #endif
39 
40 #ifndef STD_SET_H
41 #include "SheafSystem/std_set.h"
42 #endif
43 
44 #ifndef STD_STACK_H
45 #include "SheafSystem/std_stack.h"
46 #endif
47 
48 #ifndef SUBPOSET_H
49 #include "SheafSystem/subposet.h"
50 #endif
51 
52 #ifndef TOTAL_POSET_MEMBER_H
53 #include "SheafSystem/total_poset_member.h"
54 #endif
55 
56 namespace sheaf
57 {
58 
59 class zn_to_bool;
60 
89 template <typename T>
90 class SHEAF_DLL_SPEC depth_first_itr : public any
91 {
92  // ===========================================================
94  // ===========================================================
96 
97 public:
98 
102  depth_first_itr& operator=(const depth_first_itr& xother);
103 
107  virtual ~depth_first_itr();
108 
109  // SOME USEFUL CONSTANTS
110 
114  static const char* NULL_FILTER;
115 
122  {
123  PREORDER, // Member is visited before its inferiors, exports PREVISIT_ACTION.
124  POSTORDER, // Member is visited after its inferiors, exports POSTVISIT_ACTION.
125  LINKORDER, // Visits links; exports LINK_ACTION.
126  BIORDER, // Exports PREVISIT_ACTION and POSTVISIT_ACTION.
127  TRIORDER, // Exports PREVISIT_ACTION, POSTVISIT_ACTION, and LINK_ACTION.
128  NOT_AN_ORDER
129  };
130 
136  {
137  PREVISIT_ACTION,
138  LINK_ACTION,
139  POSTVISIT_ACTION,
140  NOT_AN_ACTION
141  };
142 
143 protected:
144 
148  depth_first_itr();
149 
153  depth_first_itr(const depth_first_itr& xother);
154 
158  void first();
159 
166  void mark_visited(abstract_poset_member* xmbr);
167 
174  void mark_not_visited(abstract_poset_member* xmbr);
175 
180  virtual void attach_item();
181 
186  virtual void detach_item();
187 
191  void initialize_order(order_type xorder);
192 
196  void initialize_traversal(const abstract_poset_member& xanchor);
197 
201  void initialize_traversal(pod_index_type xanchor_hub_id);
202 
206  void initialize_traversal(const scoped_index& xanchor_id);
207 
211  void initialize_anchor(const abstract_poset_member& xanchor);
212 
216  virtual void initialize_has_visited(const abstract_poset_member& xanchor);
217 
221  bool filter(pod_index_type xhub_id)
222  {
223  return (*_filter)[xhub_id];
224  };
225 
229  bool filter(const scoped_index& xid)
230  {
231  return (*_filter)[xid.hub_pod()];
232  };
233 
237  void initialize_filter();
238 
243  void initialize_filter(const subposet& xfilter);
244 
249  void initialize_filter(pod_index_type xfilter_hub_id);
250 
255  void initialize_filter(const scoped_index& xfilter_id);
256 
261  void initialize_filter(const std::string& xfilter_name);
262 
266  void release_cover_id_space_iterators();
267 
272 
278 
284 
289 
294 
299 
304 
309 
313  bool _strict;
314 
319 
324  {
325  FIRST,
326  INIT_COVER_ITERATOR,
327  TEST_HAS_VISITED,
328  INC_COVER_ITERATOR,
329  ERASE_COVER_ITERATOR,
330  DESCEND,
331  TEST_PATH_TAIL,
332  ASCEND,
333  EXECUTE_PREVISIT_ACTION,
334  EXECUTE_LINK_ACTION,
335  EXECUTE_POSTVISIT_ACTION,
336  FINISH,
337  NOT_A_STATE // Must be last; used for array bounds.
338  };
339 
344  {
345  PASS,
346  FAIL
347  };
348 
352  static const char* iterator_state_names[NOT_A_STATE+1];
353 
358  const iterator_state (*_transition_fcn)[FAIL+1];
359 
365  static const iterator_state PREORDER_TRANSITION_FCN[NOT_A_STATE-1][FAIL+1];
366 
372  static const iterator_state POSTORDER_TRANSITION_FCN[NOT_A_STATE-1][FAIL+1];
373 
379  static const iterator_state LINKORDER_TRANSITION_FCN[NOT_A_STATE-1][FAIL+1];
380 
386  static const iterator_state BIORDER_TRANSITION_FCN[NOT_A_STATE-1][FAIL+1];
387 
393  static const iterator_state TRIORDER_TRANSITION_FCN[NOT_A_STATE-1][FAIL+1];
394 
399 
405 
411 
416  std::stack<index_space_iterator*> _path_tail;
417 
423  std::stack<pod_index_type> _filtered_path_tail;
424 
430 
431 private:
432 
439  T* _has_visited;
440 
448  zn_to_bool* _filter;
449 
451 
452 
453  // ===========================================================
455  // ===========================================================
457 
458 public:
459 
464  order_type order() const;
465 
470  virtual bool is_initialized() const;
471 
476  virtual abstract_poset_member& anchor();
477 
482  virtual const abstract_poset_member& anchor() const;
483 
487  virtual bool anchor_is_ancestor_of(const abstract_poset_member& xmbr) const;
488 
492  bool descending() const;
493 
497  bool strict() const;
498 
503  subposet& filter();
504 
508  bool is_done() const;
509 
513  virtual void force_is_done();
514 
518  inline void next()
519  {
520  next(false);
521  };
522 
529  inline void truncate()
530  {
531  next(true);
532  };
533 
542  virtual void next(bool xtruncate);
543 
547  virtual void reset(bool xreset_markers = true);
548 
553  int ct(bool xreset = false);
554 
558  void clear_has_visited();
559 
563  void reserve_has_visited(pod_index_type xub);
564 
568  bool has_visited(pod_index_type xhub_id) const;
569 
573  bool has_visited(const scoped_index& xid) const;
574 
578  bool has_visited(const abstract_poset_member* xmbr) const;
579 
585  void put_has_visited(pod_index_type xhub_id, bool xvalue);
586 
592  void put_has_visited(const scoped_index& xid, bool xvalue);
593 
598  bool visit_once() const;
599 
603  void put_visit_once(bool xvisit_once);
604 
609  bool is_maximal();
610 
614  const scoped_index& greater_index() const;
615 
619  const scoped_index& lesser_index() const;
620 
625  action_type action() const;
626 
635  void erase_cover();
636 
637 protected:
638 
639 private:
640 
642 
643 
644  // ===========================================================
646  // ===========================================================
648 
649 public:
650 
654  const scoped_index& index() const;
655 
659  size_t depth();
660 
661 protected:
662 
663 private:
664 
666 
667 
668  // ===========================================================
670  // ===========================================================
672 
673 public:
674 
678  virtual bool is_ancestor_of(const any* other) const;
679 
683  virtual depth_first_itr* clone() const;
684 
688  bool invariant() const;
689 
690 protected:
691 
692 private:
693 
695 };
696 
697 // =============================================================================
698 // MEMBER FUNCTION SPECIALIZATIONS
699 // =============================================================================
700 
701 template <>
702 SHEAF_DLL_SPEC
703 void
706 
707 template <>
708 SHEAF_DLL_SPEC
709 void
712 
713 template <>
714 SHEAF_DLL_SPEC
715 bool
717 has_visited(pod_index_type xhub_id) const;
718 
719 template <>
720 SHEAF_DLL_SPEC
721 void
724 
725 template <>
726 SHEAF_DLL_SPEC
727 void
729 put_has_visited(pod_index_type xhub_id, bool xvalue);
730 
731 template <>
732 SHEAF_DLL_SPEC
733 void
735 clear_has_visited();
736 
737 
738 template <>
739 SHEAF_DLL_SPEC
740 void
742 reserve_has_visited(pod_index_type xub);
743 
744 template <>
745 SHEAF_DLL_SPEC
746 bool
748 has_visited(pod_index_type xhub_id) const;
749 
750 
751 template <>
752 SHEAF_DLL_SPEC
753 void
755 initialize_has_visited(const abstract_poset_member& xanchor);
756 
757 template <>
758 SHEAF_DLL_SPEC
759 void
761 put_has_visited(pod_index_type xhub_id, bool xvalue);
762 
763 template <>
764 SHEAF_DLL_SPEC
765 void
767 clear_has_visited();
768 
769 template <>
770 SHEAF_DLL_SPEC
771 void
773 reserve_has_visited(pod_index_type xub);
774 
775 template <>
776 SHEAF_DLL_SPEC
777 bool
779 has_visited(pod_index_type xhub_id) const;
780 
781 template <>
782 SHEAF_DLL_SPEC
783 void
785 initialize_has_visited(const abstract_poset_member& xanchor);
786 
787 template <>
788 SHEAF_DLL_SPEC
789 void
791 put_has_visited(pod_index_type xhub_id, bool xvalue);
792 
793 } // namespace sheaf
794 
795 #endif // ifndef DEPTH_FIRST_ITR_H
action_type
The types of action a client should take when the iterator returns control to the client...
void clear_has_visited()
Makes has_visited(i) false for all i.
void put_has_visited(pod_index_type xhub_id, bool xvalue)
Set the visited marker for hub id xhub_id to xvalue. Intended for use reseting iterator without havin...
virtual void initialize_has_visited(const abstract_poset_member &xanchor)
Initializes the has_visited markers.
A client handle for a subposet.
Definition: subposet.h:86
order_type
The types of order in which the iterator will visit the members of the poset. Determines which action...
bool has_visited(pod_index_type xhub_id) const
True if this has already visited member with hub id xhub_id.
An abstract iterator over the ids of an id space.
bool _descending
True if iterating over the up/down set of anchor.
bool filter(pod_index_type xhub_id)
The value of the filter at hub id xhub_id.
iterator_state
The states for the finite state machine that controls iteration.
iterator_state _state
The current state of iteration.
abstract_poset_member * _anchor
The top member of the down set being iterated over.
index_space_iterator * _path_head
The head of the path to the current member of the iteration lesser_index() == this->index() == **_pat...
void reserve_has_visited(pod_index_type xub)
Ensures has_visited(i) is a legal call for 0 <= i < xub.
Abstract base class with useful features for all objects.
Definition: any.h:39
A map from Zn (the integers mod n) to bools. A characteristic function used to represent subsets of Z...
Definition: zn_to_bool.h:52
index_space_iterator * _path_head_lc
The lower cover iterator for the head of the path to the current member of the iteration.
The general depth-first iterator over the intersection of a poset member anchor&#39;s whole with its down...
iterator_token
The input tokens for the finite state machine.
static const char * NULL_FILTER
Placeholder for null filter.
std::stack< pod_index_type > _filtered_path_tail
The tail of the filtered path to the current member of the iteration. Contains only members which pas...
scoped_index _greater_index
The index of the greater member of the current link.
bool _visit_once
True if traversal should only visit a member once; that is, it should not revisit members it has alre...
An index within the external ("client") scope of a given id space.
Definition: scoped_index.h:116
bool _new_filter
True if this allocated a new filter;.
bool filter(const scoped_index &xid)
The value of the filter at id xid.
action_type _action
The type of action the client should take; the state of the iterator.
scoped_index _index
The index of the lesser end of the current link; the current item in the iteration.
void next()
Makes this the next member of the subset.
scoped_index _lesser_index
The index of the lesser member of the current link.
int_type pod_index_type
The plain old data index type.
Definition: pod_types.h:49
Namespace for the sheaves component of the sheaf system.
std::stack< index_space_iterator * > _path_tail
The tail of the path to the current member of the iteration greater_index() == **(_path_tail.top()) == greater member of current link.
An abstract client handle for a member of a poset.
void truncate()
Makes this the next member of the subset which is not less than old this, i.e. the depth-first descen...
subposet _client_filter
The filter specified by the client.
order_type _order
The order of the iteration.
bool _strict
True if iterating over the strict up/down set of anchor.
pod_type hub_pod() const
The pod value of this mapped to the unglued hub id space.
Definition: scoped_index.h:710