1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
127
128
129
130
131
132
133
134
135
136
// Copyright 2020-2021 bluss and ndarray developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::mem;
use std::ptr::NonNull;

use crate::imp_prelude::*;
use crate::OwnedRepr;

use super::Baseiter;
use crate::impl_owned_array::drop_unreachable_raw;


/// By-value iterator for an array
pub struct IntoIter<A, D>
where
    D: Dimension,
{
    array_data: OwnedRepr<A>,
    inner: Baseiter<A, D>,
    data_len: usize,
    /// first memory address of an array element
    array_head_ptr: NonNull<A>,
    // if true, the array owns elements that are not reachable by indexing
    // through all the indices of the dimension.
    has_unreachable_elements: bool,
}

impl<A, D> IntoIter<A, D> 
where
    D: Dimension,
{
    /// Create a new by-value iterator that consumes `array`
    pub(crate) fn new(mut array: Array<A, D>) -> Self {
        unsafe {
            let array_head_ptr = array.ptr;
            let ptr = array.as_mut_ptr();
            let mut array_data = array.data;
            let data_len = array_data.release_all_elements();
            debug_assert!(data_len >= array.dim.size());
            let has_unreachable_elements = array.dim.size() != data_len;
            let inner = Baseiter::new(ptr, array.dim, array.strides);

            IntoIter {
                array_data,
                inner,
                data_len,
                array_head_ptr,
                has_unreachable_elements,
            }
        }
    }
}

impl<A, D: Dimension> Iterator for IntoIter<A, D> {
    type Item = A;

    #[inline]
    fn next(&mut self) -> Option<A> {
        self.inner.next().map(|p| unsafe { p.read() })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<A, D: Dimension> ExactSizeIterator for IntoIter<A, D> {
    fn len(&self) -> usize { self.inner.len() }
}

impl<A, D> Drop for IntoIter<A, D>
where
    D: Dimension
{
    fn drop(&mut self) {
        if !self.has_unreachable_elements || mem::size_of::<A>() == 0 || !mem::needs_drop::<A>() {
            return;
        }

        // iterate til the end
        while let Some(_) = self.next() { }

        unsafe {
            let data_ptr = self.array_data.as_ptr_mut();
            let view = RawArrayViewMut::new(self.array_head_ptr, self.inner.dim.clone(),
                                            self.inner.strides.clone());
            debug_assert!(self.inner.dim.size() < self.data_len, "data_len {} and dim size {}",
                          self.data_len, self.inner.dim.size());
            drop_unreachable_raw(view, data_ptr, self.data_len);
        }
    }
}

impl<A, D> IntoIterator for Array<A, D>
where
    D: Dimension
{
    type Item = A;
    type IntoIter = IntoIter<A, D>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self)
    }
}

impl<A, D> IntoIterator for ArcArray<A, D>
where
    D: Dimension,
    A: Clone,
{
    type Item = A;
    type IntoIter = IntoIter<A, D>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self.into_owned())
    }
}

impl<A, D> IntoIterator for CowArray<'_, A, D>
where
    D: Dimension,
    A: Clone,
{
    type Item = A;
    type IntoIter = IntoIter<A, D>;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self.into_owned())
    }
}