Class: AutoC::List

Inherits:
Collection show all
Includes:
Sequential
Defined in:
lib/autoc/list.rb

Overview

Generator for singly linked collection of elements

Direct Known Subclasses

HashMap::List

Defined Under Namespace

Classes: Range

Constant Summary

Constants inherited from Composite

Composite::CAMEL_CASE_DECORATOR, Composite::DEFINITIONS, Composite::PRIVATE, Composite::SNAKE_CASE_DECORATOR

Constants included from Entity

Entity::ReferenceSet

Constants included from STD

STD::ASSERT_H, STD::BOOL, STD::CHAR, STD::COMPLEX, STD::COMPLEX_H, STD::DOUBLE, STD::DOUBLE_COMPLEX, STD::DOUBLE_T, STD::FLOAT, STD::FLOAT_COMPLEX, STD::FLOAT_T, STD::INT, STD::INTMAX_T, STD::INTPTR_T, STD::INTTYPES_H, STD::LONG, STD::LONG_DOUBLE, STD::LONG_DOUBLE_COMPLEX, STD::LONG_LONG, STD::MALLOC_H, STD::MATH_H, STD::PTRDIFF_T, STD::SHORT, STD::SIGNED_CHAR, STD::SIZE_T, STD::STDBOOL_H, STD::STDDEF_H, STD::STDLIB_H, STD::STRING_H, STD::UINTMAX_T, STD::UINTPTR_T, STD::UNSIGNED, STD::UNSIGNED_CHAR, STD::UNSIGNED_LONG, STD::UNSIGNED_LONG_LONG, STD::UNSIGNED_SHORT, STD::WCHAR_T

Instance Attribute Summary collapse

Attributes inherited from Collection

#element

Attributes inherited from Composite

#_master, #visibility

Attributes inherited from Type

#signature

Instance Method Summary collapse

Methods inherited from Collection

#comparable?, #copyable?, #destructible?, #hashable?, new, #orderable?, #type_tag

Methods inherited from Composite

allocator, allocator=, #const_lvalue, #const_rvalue, decorator, decorator=, #defgroup, hasher, #hasher, hasher=, #identifier, #ingroup, #inspect, #lvalue, #memory, new, #prefix, #public?, #respond_to_missing?, #rvalue, #to_value, #type_tag

Methods included from Entity

#<=>, #complexity, #dependencies, #forward_declarations, #implementation, #interface, #position, #references, #total_dependencies, #total_references

Methods inherited from Type

abstract, #comparable?, #constructible?, #copy, #copyable?, #custom_constructible?, #custom_create, #default_constructible?, #default_create, #destroy, #destructible?, #hashable?, #inspect, #orderable?, #to_s, #to_type

Constructor Details

#initialize(*args, maintain_size: true, **kws) ⇒ List

maintain_size:

true: managed size field (extra memory consumption)
false: computing #size function (slow, O(N))


29
30
31
32
33
34
35
# File 'lib/autoc/list.rb', line 29

def initialize(*args, maintain_size: true, **kws)
  super(*args, **kws)
  @_node = identifier(:_node, abbreviate: true)
  @_node_p = _node.lvalue
  @_node_pp = "#{_node}*".lvalue
  @maintain_size = maintain_size
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AutoC::Composite

Instance Attribute Details

#_nodeObject (readonly)

Returns the value of attribute _node.



24
25
26
# File 'lib/autoc/list.rb', line 24

def _node
  @_node
end

#_node_pObject (readonly)

Returns the value of attribute _node_p.



24
25
26
# File 'lib/autoc/list.rb', line 24

def _node_p
  @_node_p
end

#_node_ppObject (readonly)

Returns the value of attribute _node_pp.



24
25
26
# File 'lib/autoc/list.rb', line 24

def _node_pp
  @_node_pp
end

Instance Method Details

#_locate_node_equal(eq) ⇒ Object

Code template for locating the list node satisfying custom equality condition



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/autoc/list.rb', line 100

def _locate_node_equal(eq)
  %{
    #{_node_p} curr;
    #{_node_p} prev;
    assert(target);
    assert(prev_p);
    assert(curr_p);
    prev = NULL;
    curr = target->front;
    while(curr) {
      if(#{eq}) {
        #ifndef NDEBUG
          if(prev)
            assert(prev->next == curr);
          else
            assert(target->front == curr);
        #endif
        *prev_p = prev;
        *curr_p = curr;
        return 1;
      }
      prev = curr;
      curr = curr->next;
    }
    return 0;
  }
end

#_range_classObject



20
# File 'lib/autoc/list.rb', line 20

def _range_class = Range

#_remove_first(locator) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/autoc/list.rb', line 288

def _remove_first(locator)
  %{
    #{_node_p} curr;
    #{_node_p} prev;
    assert(target);
    if(#{locator}) {
      assert(curr);
      if(prev) {
        prev->next = curr->next;
      } else {
        target->front = curr->next;
      }
      #{element.destroy.('curr->element') if element.destructible?};
      #{memory.free(:curr)};
      #{'--target->size;' if maintain_size?}
      return 1;
    }
    return 0;
  }
end

#maintain_size?Boolean



37
# File 'lib/autoc/list.rb', line 37

def maintain_size? = @maintain_size

#rangeObject



22
# File 'lib/autoc/list.rb', line 22

def range = @range ||= _range_class.new(self, visibility: visibility)

#render_interface(stream) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/autoc/list.rb', line 39

def render_interface(stream)
  stream << %{
    /** @private */
    typedef struct #{_node} #{_node};
  }
  if public?
    stream << %{
      /**
        #{defgroup}
  
        @brief Singly linked list of elements of type #{element}
  
        For iteration over the list elements refer to @ref #{range}.
  
        @see C++ [std::forward_list<T>](https://en.cppreference.com/w/cpp/container/forward_list)
  
        @since 2.0
      */
    }
    stream << %{
      /**
        #{ingroup}

        @brief Opaque structure holding state of the list

        @since 2.0
      */
    }
  else
    stream << PRIVATE
  end
  stream << %{
    typedef struct {
      #{_node_p} front; /**< @private */
      #{'size_t size; /**< @private */' if maintain_size?}
    } #{signature};
    /** @private */
    struct #{_node} {
      #{element} element;
      #{_node_p} next;
    };
  }
end