summaryrefslogtreecommitdiffstats
path: root/src/armadillo/include/armadillo_bits/fn_randperm.hpp
blob: 19623a75ee8ec594bba22c0577739ae23dcfac70 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// SPDX-License-Identifier: Apache-2.0
// 
// Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2008-2016 National ICT Australia (NICTA)
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------


//! \addtogroup fn_randperm
//! @{



template<typename obj_type>
inline
void
internal_randperm_helper(obj_type& x, const uword N, const uword N_keep)
  {
  arma_extra_debug_sigprint();
  
  typedef typename obj_type::elem_type eT;
  
  // see op_sort_index_bones.hpp for the definition of arma_sort_index_packet
  // and the associated comparison functor
  
  typedef arma_sort_index_packet<int> packet;
  
  std::vector<packet> packet_vec(N);
  
  for(uword i=0; i < N; ++i)
    {
    packet_vec[i].val   = int(arma_rng::randi<int>());
    packet_vec[i].index = i;
    }
  
  arma_sort_index_helper_ascend<int> comparator;
  
  if(N >= 2)
    {
    if(N_keep < N)
      {
      typename std::vector<packet>::iterator first    = packet_vec.begin();
      typename std::vector<packet>::iterator nth      = first + N_keep;
      typename std::vector<packet>::iterator pastlast = packet_vec.end();
      
      std::partial_sort(first, nth, pastlast, comparator);
      }
    else
      {
      std::sort( packet_vec.begin(), packet_vec.end(), comparator );
      }
    }
  
  if(is_Row<obj_type>::value)
    {
    x.set_size(1,N_keep);
    }
  else
    {
    x.set_size(N_keep,1);
    }
  
  eT* x_mem = x.memptr();
  
  for(uword i=0; i < N_keep; ++i)
    {
    x_mem[i] = eT( packet_vec[i].index );
    }
  }



template<typename obj_type>
arma_warn_unused
inline
typename enable_if2< is_Mat<obj_type>::value, obj_type >::result
randperm(const uword N)
  {
  arma_extra_debug_sigprint();
  
  obj_type x;
  
  if(N > 0)  { internal_randperm_helper(x, N, N); }
  
  return x;
  }



arma_warn_unused
inline
uvec
randperm(const uword N)
  {
  arma_extra_debug_sigprint();
  
  uvec x;
  
  if(N > 0)  { internal_randperm_helper(x, N, N); }
  
  return x;
  }



template<typename obj_type>
arma_warn_unused
inline
typename enable_if2< is_Mat<obj_type>::value, obj_type >::result
randperm(const uword N, const uword M)
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( (M > N), "randperm(): 'M' must be less than or equal to 'N'" );
  
  obj_type x;
  
  if( (N > 0) && (M > 0) )  { internal_randperm_helper(x, N, M); }
  
  return x;
  }



arma_warn_unused
inline
uvec
randperm(const uword N, const uword M)
  {
  arma_extra_debug_sigprint();
  
  arma_debug_check( (M > N), "randperm(): 'M' must be less than or equal to 'N'" );
  
  uvec x;
  
  if( (N > 0) && (M > 0) )  { internal_randperm_helper(x, N, M); }
  
  return x;
  }



//! @}