36 #include "compress_utils.h" 38 template<
typename I>
class Moc
46 static Moc fromNewRangeSet(
const rangeset<I> &rngset)
54 const rangeset<I> &Rs() {
return rs; }
55 tsize maxOrder()
const 58 for (tsize i=0; i<rs.nranges(); ++i)
59 combo|=rs.ivbegin(i)|rs.ivend(i);
60 return maxorder-(trailingZeros(combo)>>1);
62 Moc degradedToOrder (
int order,
bool keepPartialCells)
const 64 int shift=2*(maxorder-order);
65 I ofs=(I(1)<<shift)-1;
67 I adda = keepPartialCells ? I(0) : ofs,
68 addb = keepPartialCells ? ofs : I(0);
70 for (tsize i=0; i<rs.nranges(); ++i)
72 I a=(rs.ivbegin(i)+adda)&mask;
73 I b=(rs.ivend (i)+addb)&mask;
74 if (b>a) rs2.append(a,b);
76 return fromNewRangeSet(rs2);
78 void addPixelRange (
int order, I p1, I p2)
80 int shift=2*(maxorder-order);
81 rs.add(p1<<shift,p2<<shift);
83 void appendPixelRange (
int order, I p1, I p2)
85 int shift=2*(maxorder-order);
86 rs.append(p1<<shift,p2<<shift);
88 void appendPixel (
int order, I p)
89 { appendPixelRange(order,p,p+1); }
91 Moc op_or (
const Moc &other)
const 92 {
return fromNewRangeSet(rs.op_or(other.rs)); }
95 Moc op_and (
const Moc &other)
const 96 {
return fromNewRangeSet(rs.op_and(other.rs)); }
97 Moc op_xor (
const Moc &other)
const 98 {
return fromNewRangeSet(rs.op_xor(other.rs)); }
101 Moc op_andnot (
const Moc &other)
const 102 {
return fromNewRangeSet(rs.op_andnot(other.rs)); }
104 Moc complement()
const 106 rangeset<I> full; full.append(I(0),I(12)*(I(1)<<(2*maxorder)));
107 return fromNewRangeSet(full.op_andnot(rs));
110 bool contains(
const Moc &other)
const 111 {
return rs.contains(other.rs); }
114 bool overlaps(
const Moc &other)
const 115 {
return rs.overlaps(other.rs); }
120 std::vector<I> toUniq()
const 123 std::vector<std::vector<I> > buf(maxorder+1);
124 for (tsize i=0; i<rs.nranges(); ++i)
126 I start=rs.ivbegin(i), end=rs.ivend(i);
129 int logstep=std::min<int>(maxorder,trailingZeros(start)>>1);
130 logstep=std::min(logstep,ilog2(end-start)>>1);
131 buf[maxorder-logstep].push_back(start);
132 start+=I(1)<<(2*logstep);
135 for (
int o=0; o<=maxorder; ++o)
138 int shift=2*(maxorder-o);
139 for (tsize j=0; j<buf[o].size(); ++j)
140 res.push_back((buf[o][j]>>shift)+ofs);
145 static Moc fromUniq (
const std::vector<I> &vu)
149 int shift=2*maxorder;
150 for (tsize i=0; i<vu.size(); ++i)
152 int order = ilog2(vu[i]>>2)>>1;
153 if (order!=lastorder)
158 shift=2*(maxorder-order);
160 I pix = vu[i]-(I(1)<<(2*order+2));
161 rtmp.append (pix<<shift,(pix+1)<<shift);
164 return fromNewRangeSet(r);
167 static void uniq_nest2peano(std::vector<I> &vu)
170 if (vu.empty())
return;
176 for (tsize j=0; j<vu.size(); ++j)
178 int neworder=ilog2(vu[j]>>2)>>1;
181 sort(vu.begin()+start,vu.begin()+j);
185 offset=I(1)<<(2*order+2);
189 sort(vu.begin()+start,vu.end());
191 static void uniq_peano2nest(std::vector<I> &vu)
194 if (vu.empty())
return;
200 for (tsize j=0; j<vu.size(); ++j)
202 int neworder=ilog2(vu[j]>>2)>>1;
205 sort(vu.begin()+start,vu.begin()+j);
209 offset=I(1)<<(2*order+2);
213 sort(vu.begin()+start,vu.end());
216 std::vector<uint8> toCompressed()
const 219 interpol_encode(rs.data().begin(),rs.data().end(),obs);
222 static Moc fromCompressed(
const std::vector<uint8> &data)
224 ibitstream ibs(data);
226 interpol_decode(v,ibs);
232 bool operator==(
const Moc &other)
const 239 tsize nranges()
const 240 {
return rs.nranges(); }
242 {
return rs.nval(); }
I nest2peano(I pix) const
void Set(int order, Healpix_Ordering_Scheme scheme)
I peano2nest(I pix) const