| 1 |
/* |
| 2 |
* spvec.c - Sparse vector |
| 3 |
* |
| 4 |
* Copyright (c) 2009-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "spvec.h" |
| 35 |
|
| 36 |
#define INDEX_CHECK(n) /*empty*/ |
| 37 |
|
| 38 |
/*=================================================================== |
| 39 |
* Generic stuff |
| 40 |
* NB: The generic constructor is defined near the bottom of the source, |
| 41 |
* since it should come after static descriptor definitions. |
| 42 |
*/ |
| 43 |
|
| 44 |
/* Common CPL */ |
| 45 |
ScmClass *spvec_cpl[] = { |
| 46 |
SCM_CLASS_STATIC_PTR(Scm_SparseVectorBaseClass), |
| 47 |
SCM_CLASS_STATIC_PTR(Scm_DictionaryClass), |
| 48 |
SCM_CLASS_STATIC_PTR(Scm_CollectionClass), |
| 49 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 50 |
NULL |
| 51 |
}; |
| 52 |
|
| 53 |
ScmClass *spmat_cpl[] = { |
| 54 |
SCM_CLASS_STATIC_PTR(Scm_SparseMatrixBaseClass), |
| 55 |
SCM_CLASS_STATIC_PTR(Scm_CollectionClass), |
| 56 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 57 |
NULL |
| 58 |
}; |
| 59 |
|
| 60 |
SCM_DEFINE_BUILTIN_CLASS(Scm_SparseVectorBaseClass, |
| 61 |
NULL, NULL, NULL, NULL, |
| 62 |
spvec_cpl+1); |
| 63 |
|
| 64 |
SCM_DEFINE_BUILTIN_CLASS(Scm_SparseMatrixBaseClass, |
| 65 |
NULL, NULL, NULL, NULL, |
| 66 |
spmat_cpl+1); |
| 67 |
|
| 68 |
ScmObj SparseVectorRef(SparseVector *sv, u_long index, ScmObj fallback) |
| 69 |
{ |
| 70 |
INDEX_CHECK(index); |
| 71 |
Leaf *leaf = CompactTrieGet(&sv->trie, index >> sv->desc->shift); |
| 72 |
if (leaf == NULL) return fallback; |
| 73 |
ScmObj v = sv->desc->ref(leaf, index); |
| 74 |
if (SCM_UNBOUNDP(v)) return fallback; |
| 75 |
else return v; |
| 76 |
} |
| 77 |
|
| 78 |
void SparseVectorSet(SparseVector *sv, u_long index, ScmObj value) |
| 79 |
{ |
| 80 |
INDEX_CHECK(index); |
| 81 |
Leaf *leaf = CompactTrieAdd(&sv->trie, index >> sv->desc->shift, |
| 82 |
sv->desc->allocate, sv); |
| 83 |
/* set returns TRUE if this is a new entry */ |
| 84 |
if (sv->desc->set(leaf, index, value)) sv->numEntries++; |
| 85 |
} |
| 86 |
|
| 87 |
/* returns value of the deleted entry, or SCM_UNBOUND if there's no entry */ |
| 88 |
ScmObj SparseVectorDelete(SparseVector *sv, u_long index) |
| 89 |
{ |
| 90 |
INDEX_CHECK(index); |
| 91 |
Leaf *leaf = CompactTrieGet(&sv->trie, index >> sv->desc->shift); |
| 92 |
if (leaf == NULL) return SCM_UNBOUND; |
| 93 |
ScmObj r = sv->desc->delete(leaf, index); |
| 94 |
if (!SCM_UNBOUNDP(r)) sv->numEntries--; |
| 95 |
return r; |
| 96 |
} |
| 97 |
|
| 98 |
void SparseVectorClear(SparseVector *sv) |
| 99 |
{ |
| 100 |
sv->numEntries = 0; |
| 101 |
CompactTrieClear(&sv->trie, sv->desc->clear, sv->desc); |
| 102 |
} |
| 103 |
|
| 104 |
ScmObj SparseVectorCopy(const SparseVector *src) |
| 105 |
{ |
| 106 |
SparseVector *dst = |
| 107 |
(SparseVector*)MakeSparseVector(Scm_ClassOf(SCM_OBJ(src)), |
| 108 |
src->defaultValue, |
| 109 |
src->flags); |
| 110 |
CompactTrieCopy(&dst->trie, &src->trie, src->desc->copy, src->desc); |
| 111 |
dst->numEntries = src->numEntries; |
| 112 |
return SCM_OBJ(dst); |
| 113 |
} |
| 114 |
|
| 115 |
void SparseVectorIterInit(SparseVectorIter *iter, SparseVector *sv) |
| 116 |
{ |
| 117 |
iter->sv = sv; |
| 118 |
iter->leaf = NULL; |
| 119 |
CompactTrieIterInit(&iter->citer, &sv->trie); |
| 120 |
iter->leafIndex = -1; |
| 121 |
} |
| 122 |
|
| 123 |
ScmObj SparseVectorIterNext(SparseVectorIter *iter) |
| 124 |
{ |
| 125 |
ScmObj (*iterproc)(Leaf*,int*) = iter->sv->desc->iter; |
| 126 |
for (;;) { |
| 127 |
if (iter->leaf) { |
| 128 |
ScmObj r = iterproc(iter->leaf, &iter->leafIndex); |
| 129 |
if (!SCM_UNBOUNDP(r)) { |
| 130 |
u_long ind = ((leaf_key(iter->leaf) << iter->sv->desc->shift) |
| 131 |
+ iter->leafIndex); |
| 132 |
return Scm_Cons(Scm_MakeIntegerU(ind), r); |
| 133 |
} |
| 134 |
} |
| 135 |
iter->leaf = CompactTrieIterNext(&iter->citer); |
| 136 |
if (iter->leaf == NULL) return SCM_FALSE; /* we're at the end */ |
| 137 |
iter->leafIndex = -1; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* special routine for uniform numeric sparse vectors */ |
| 142 |
/* TODO: Allow clamp arg */ |
| 143 |
ScmObj SparseVectorInc(SparseVector *sv, u_long index, |
| 144 |
ScmObj delta, /* number */ |
| 145 |
ScmObj fallback) /* number or unbound */ |
| 146 |
{ |
| 147 |
if (!SCM_NUMBERP(fallback)) { |
| 148 |
if (SCM_NUMBERP(sv->defaultValue)) { |
| 149 |
fallback = sv->defaultValue; |
| 150 |
} else { |
| 151 |
fallback = SCM_MAKE_INT(0); |
| 152 |
} |
| 153 |
} |
| 154 |
INDEX_CHECK(index); |
| 155 |
Leaf *leaf = CompactTrieGet(&sv->trie, index >> sv->desc->shift); |
| 156 |
if (leaf == NULL) { |
| 157 |
ScmObj v = Scm_Add(fallback, delta); |
| 158 |
SparseVectorSet(sv, index, v); |
| 159 |
return v; |
| 160 |
} else { |
| 161 |
ScmObj v = sv->desc->ref(leaf, index); |
| 162 |
if (SCM_UNBOUNDP(v)) v = fallback; |
| 163 |
v = Scm_Add(v, delta); |
| 164 |
sv->desc->set(leaf, index, v); |
| 165 |
return v; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void SparseVectorDump(SparseVector *sv) |
| 170 |
{ |
| 171 |
CompactTrieDump(SCM_CUROUT, &sv->trie, sv->desc->dump, sv->desc); |
| 172 |
} |
| 173 |
|
| 174 |
/*=================================================================== |
| 175 |
* Individual vector types |
| 176 |
*/ |
| 177 |
|
| 178 |
/*------------------------------------------------------------------- |
| 179 |
* General vector |
| 180 |
*/ |
| 181 |
|
| 182 |
typedef struct GLeafRec { |
| 183 |
Leaf hdr; |
| 184 |
ScmObj val[2]; |
| 185 |
} GLeaf; |
| 186 |
|
| 187 |
static ScmObj g_ref(Leaf *leaf, u_long index) |
| 188 |
{ |
| 189 |
return ((GLeaf*)leaf)->val[index&1]; |
| 190 |
} |
| 191 |
|
| 192 |
static Leaf *g_allocate(void *data SCM_UNUSED) |
| 193 |
{ |
| 194 |
GLeaf *z = SCM_NEW(GLeaf); |
| 195 |
z->val[0] = z->val[1] = SCM_UNBOUND; |
| 196 |
return (Leaf*)z; |
| 197 |
} |
| 198 |
|
| 199 |
static int g_set(Leaf *leaf, u_long index, ScmObj value) |
| 200 |
{ |
| 201 |
ScmObj v = ((GLeaf*)leaf)->val[index&1]; |
| 202 |
((GLeaf*)leaf)->val[index&1] = value; |
| 203 |
return SCM_UNBOUNDP(v); |
| 204 |
} |
| 205 |
|
| 206 |
static ScmObj g_delete(Leaf *leaf, u_long index) |
| 207 |
{ |
| 208 |
ScmObj v = ((GLeaf*)leaf)->val[index&1]; |
| 209 |
((GLeaf*)leaf)->val[index&1] = SCM_UNBOUND; |
| 210 |
return v; |
| 211 |
} |
| 212 |
|
| 213 |
static void g_clear(Leaf *leaf, void *data SCM_UNUSED) |
| 214 |
{ |
| 215 |
GLeaf *z = (GLeaf*)leaf; |
| 216 |
z->val[0] = z->val[1] = NULL; |
| 217 |
} |
| 218 |
|
| 219 |
static Leaf *g_copy(Leaf *leaf, void *data SCM_UNUSED) |
| 220 |
{ |
| 221 |
GLeaf *dst = SCM_NEW(GLeaf); |
| 222 |
*dst = *(GLeaf*)leaf; |
| 223 |
return (Leaf*)dst; |
| 224 |
} |
| 225 |
|
| 226 |
static ScmObj g_iter(Leaf *leaf, int *index) |
| 227 |
{ |
| 228 |
GLeaf *z = (GLeaf*)leaf; |
| 229 |
if (++(*index) == 0) { |
| 230 |
if (!SCM_UNBOUNDP(z->val[0])) return z->val[0]; |
| 231 |
(*index)++; |
| 232 |
/*FALLTHROUGH*/ |
| 233 |
} |
| 234 |
if (*index == 1) { |
| 235 |
if (!SCM_UNBOUNDP(z->val[1])) return z->val[1]; |
| 236 |
(*index)++; |
| 237 |
} |
| 238 |
return SCM_UNBOUND; |
| 239 |
} |
| 240 |
|
| 241 |
static void g_dump(ScmPort *out, Leaf *leaf, int indent, void *data SCM_UNUSED) |
| 242 |
{ |
| 243 |
GLeaf *z = (GLeaf*)leaf; |
| 244 |
for (int i=0; i<2; i++) { |
| 245 |
if (!SCM_UNBOUNDP(z->val[i])) { |
| 246 |
Scm_Printf(out, "\n %*s%2d: %25.1S", indent, "", i, z->val[i]); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static SparseVectorDescriptor g_desc = { |
| 252 |
g_ref, g_set, g_allocate, g_delete, g_clear, g_copy, g_iter, g_dump, 1 |
| 253 |
}; |
| 254 |
|
| 255 |
SCM_DEFINE_BUILTIN_CLASS(Scm_SparseVectorClass, NULL, NULL, NULL, NULL, |
| 256 |
spvec_cpl); |
| 257 |
|
| 258 |
SCM_DEFINE_BUILTIN_CLASS(Scm_SparseMatrixClass, NULL, NULL, NULL, NULL, |
| 259 |
spmat_cpl); |
| 260 |
|
| 261 |
/*------------------------------------------------------------------- |
| 262 |
* Uniform sparse vector common stuff |
| 263 |
*/ |
| 264 |
|
| 265 |
typedef struct ULeafRec { |
| 266 |
Leaf hdr; /* LEAF_DATA has bitmap of used entries */ |
| 267 |
union { |
| 268 |
ScmWord dummy[2]; /* placeholder */ |
| 269 |
int8_t s8[2*SIZEOF_LONG]; |
| 270 |
uint8_t u8[2*SIZEOF_LONG]; |
| 271 |
int16_t s16[SIZEOF_LONG]; |
| 272 |
uint16_t u16[SIZEOF_LONG]; |
| 273 |
int32_t s32[SIZEOF_LONG/2]; |
| 274 |
uint32_t u32[SIZEOF_LONG/2]; |
| 275 |
int64_t s64[SIZEOF_LONG/4]; |
| 276 |
uint64_t u64[SIZEOF_LONG/4]; |
| 277 |
ScmHalfFloat f16[SIZEOF_LONG]; |
| 278 |
float f32[SIZEOF_LONG/2]; |
| 279 |
double f64[SIZEOF_LONG/4]; |
| 280 |
}; |
| 281 |
} ULeaf; |
| 282 |
|
| 283 |
#define ULEAF(leaf) ((ULeaf*)leaf) |
| 284 |
|
| 285 |
#if SIZEOF_LONG == 4 |
| 286 |
#define SHIFT8 3 |
| 287 |
#define SHIFT16 2 |
| 288 |
#define SHIFT32 1 |
| 289 |
#define SHIFT64 0 |
| 290 |
#else /* SIZEOF_LONG != 4 */ |
| 291 |
#define SHIFT8 4 |
| 292 |
#define SHIFT16 3 |
| 293 |
#define SHIFT32 2 |
| 294 |
#define SHIFT64 1 |
| 295 |
#endif /* SIZEOF_LONG != 4 */ |
| 296 |
|
| 297 |
#define MASK8 ((1UL<<SHIFT8)-1) |
| 298 |
#define MASK16 ((1UL<<SHIFT16)-1) |
| 299 |
#define MASK32 ((1UL<<SHIFT32)-1) |
| 300 |
#define MASK64 ((1UL<<SHIFT64)-1) |
| 301 |
|
| 302 |
#define U_HAS_ENTRY(leaf, ind, mask) leaf_data_bit_test(leaf, (ind)&(mask)) |
| 303 |
#define U_SET_ENTRY(leaf, ind, mask) leaf_data_bit_set(leaf, (ind)&(mask)) |
| 304 |
#define U_RESET_ENTRY(leaf, ind, mask) leaf_data_bit_reset(leaf, (ind)&(mask)) |
| 305 |
|
| 306 |
static Leaf *u_allocate(void *data SCM_UNUSED) |
| 307 |
{ |
| 308 |
ULeaf *z = SCM_NEW_ATOMIC(ULeaf); |
| 309 |
z->dummy[0] = z->dummy[1] = 0; |
| 310 |
return (Leaf*)z; |
| 311 |
} |
| 312 |
|
| 313 |
static void u_clear(Leaf *leaf SCM_UNUSED, void *data SCM_UNUSED) |
| 314 |
{ |
| 315 |
/* nothing to do */ |
| 316 |
} |
| 317 |
|
| 318 |
static Leaf *u_copy(Leaf *leaf, void *data SCM_UNUSED) |
| 319 |
{ |
| 320 |
ULeaf *z = SCM_NEW_ATOMIC(ULeaf); |
| 321 |
memcpy(z, leaf, sizeof(ULeaf)); |
| 322 |
return (Leaf*)z; |
| 323 |
} |
| 324 |
|
| 325 |
/*------------------------------------------------------------------- |
| 326 |
* Uniform Sparse Vector Ref |
| 327 |
*/ |
| 328 |
|
| 329 |
#define REF_CHECK(leaf, index, mask) \ |
| 330 |
do { if (!U_HAS_ENTRY(leaf, index, mask)) return SCM_UNBOUND; } while(0) |
| 331 |
|
| 332 |
#define U_REF(tag, mask, box) \ |
| 333 |
static ScmObj SCM_CPP_CAT(tag,_ref)(Leaf *leaf, u_long index) \ |
| 334 |
{ \ |
| 335 |
REF_CHECK(leaf, index, mask); \ |
| 336 |
return box(ULEAF(leaf)->tag[index&mask]); \ |
| 337 |
} |
| 338 |
|
| 339 |
#define F16BOX(v) Scm_VMReturnFlonum(Scm_HalfToDouble(v)) |
| 340 |
#define F32BOX(v) Scm_VMReturnFlonum((double)(v)) |
| 341 |
|
| 342 |
U_REF(s8, MASK8, SCM_MAKE_INT) |
| 343 |
U_REF(u8, MASK8, SCM_MAKE_INT) |
| 344 |
U_REF(s16, MASK16, SCM_MAKE_INT) |
| 345 |
U_REF(u16, MASK16, SCM_MAKE_INT) |
| 346 |
U_REF(s32, MASK32, Scm_MakeInteger) |
| 347 |
U_REF(u32, MASK32, Scm_MakeIntegerU) |
| 348 |
U_REF(s64, MASK64, Scm_MakeInteger64) |
| 349 |
U_REF(u64, MASK64, Scm_MakeIntegerU64) |
| 350 |
U_REF(f16, MASK16, F16BOX) |
| 351 |
U_REF(f32, MASK32, F32BOX) |
| 352 |
U_REF(f64, MASK64, Scm_VMReturnFlonum) |
| 353 |
|
| 354 |
/*------------------------------------------------------------------- |
| 355 |
* Uniform Sparse Vector Set |
| 356 |
*/ |
| 357 |
|
| 358 |
#define U_SET_INT(elt, mask, getter) \ |
| 359 |
do { \ |
| 360 |
ULEAF(leaf)->elt[index&mask] = getter(val, SCM_CLAMP_ERROR, NULL); \ |
| 361 |
U_SET_CHECK(mask); \ |
| 362 |
} while(0) |
| 363 |
|
| 364 |
#define U_SET_CHECK(mask) \ |
| 365 |
do { \ |
| 366 |
int z = U_HAS_ENTRY(leaf, index, mask); \ |
| 367 |
U_SET_ENTRY(leaf, index, mask); \ |
| 368 |
return !z; \ |
| 369 |
} while (0) |
| 370 |
|
| 371 |
static int s8_set(Leaf *leaf, u_long index, ScmObj val) |
| 372 |
{ |
| 373 |
U_SET_INT(s8, MASK8, Scm_GetInteger8Clamp); |
| 374 |
} |
| 375 |
|
| 376 |
static int u8_set(Leaf *leaf, u_long index, ScmObj val) |
| 377 |
{ |
| 378 |
U_SET_INT(u8, MASK8, Scm_GetIntegerU8Clamp); |
| 379 |
} |
| 380 |
|
| 381 |
static int s16_set(Leaf *leaf, u_long index, ScmObj val) |
| 382 |
{ |
| 383 |
U_SET_INT(s16, MASK16, Scm_GetInteger16Clamp); |
| 384 |
} |
| 385 |
|
| 386 |
static int u16_set(Leaf *leaf, u_long index, ScmObj val) |
| 387 |
{ |
| 388 |
U_SET_INT(u16, MASK16, Scm_GetIntegerU16Clamp); |
| 389 |
} |
| 390 |
|
| 391 |
static int s32_set(Leaf *leaf, u_long index, ScmObj val) |
| 392 |
{ |
| 393 |
U_SET_INT(s32, MASK32, Scm_GetInteger32Clamp); |
| 394 |
} |
| 395 |
|
| 396 |
static int u32_set(Leaf *leaf, u_long index, ScmObj val) |
| 397 |
{ |
| 398 |
U_SET_INT(u32, MASK32, Scm_GetIntegerU32Clamp); |
| 399 |
} |
| 400 |
|
| 401 |
static int s64_set(Leaf *leaf, u_long index, ScmObj val) |
| 402 |
{ |
| 403 |
U_SET_INT(s64, MASK64, Scm_GetInteger64Clamp); |
| 404 |
} |
| 405 |
|
| 406 |
static int u64_set(Leaf *leaf, u_long index, ScmObj val) |
| 407 |
{ |
| 408 |
U_SET_INT(u64, MASK64, Scm_GetIntegerU64Clamp); |
| 409 |
} |
| 410 |
|
| 411 |
static int f16_set(Leaf *leaf, u_long index, ScmObj val) |
| 412 |
{ |
| 413 |
ULEAF(leaf)->f16[index&MASK16] = Scm_DoubleToHalf(Scm_GetDouble(val)); |
| 414 |
U_SET_CHECK(MASK16); |
| 415 |
} |
| 416 |
|
| 417 |
static int f32_set(Leaf *leaf, u_long index, ScmObj val) |
| 418 |
{ |
| 419 |
ULEAF(leaf)->f32[index&MASK32] = (float)Scm_GetDouble(val); |
| 420 |
U_SET_CHECK(MASK32); |
| 421 |
} |
| 422 |
|
| 423 |
static int f64_set(Leaf *leaf, u_long index, ScmObj val) |
| 424 |
{ |
| 425 |
ULEAF(leaf)->f64[index&MASK64] = Scm_GetDouble(val); |
| 426 |
U_SET_CHECK(MASK64); |
| 427 |
} |
| 428 |
|
| 429 |
/*------------------------------------------------------------------- |
| 430 |
* Uniform Sparse Vector Delete |
| 431 |
*/ |
| 432 |
|
| 433 |
#define U_DEL(ref, mask) \ |
| 434 |
do { \ |
| 435 |
ScmObj r = ref(leaf, index); \ |
| 436 |
if (SCM_UNBOUNDP(r)) return r; \ |
| 437 |
U_RESET_ENTRY(leaf, index, mask); \ |
| 438 |
return r; \ |
| 439 |
} while (0); |
| 440 |
|
| 441 |
static ScmObj s8_delete(Leaf *leaf, u_long index) |
| 442 |
{ |
| 443 |
U_DEL(s8_ref, MASK8); |
| 444 |
} |
| 445 |
|
| 446 |
static ScmObj u8_delete(Leaf *leaf, u_long index) |
| 447 |
{ |
| 448 |
U_DEL(u8_ref, MASK8); |
| 449 |
} |
| 450 |
|
| 451 |
static ScmObj s16_delete(Leaf *leaf, u_long index) |
| 452 |
{ |
| 453 |
U_DEL(s16_ref, MASK16); |
| 454 |
} |
| 455 |
|
| 456 |
static ScmObj u16_delete(Leaf *leaf, u_long index) |
| 457 |
{ |
| 458 |
U_DEL(u16_ref, MASK16); |
| 459 |
} |
| 460 |
|
| 461 |
static ScmObj s32_delete(Leaf *leaf, u_long index) |
| 462 |
{ |
| 463 |
U_DEL(s32_ref, MASK32); |
| 464 |
} |
| 465 |
|
| 466 |
static ScmObj u32_delete(Leaf *leaf, u_long index) |
| 467 |
{ |
| 468 |
U_DEL(u32_ref, MASK32); |
| 469 |
} |
| 470 |
|
| 471 |
static ScmObj s64_delete(Leaf *leaf, u_long index) |
| 472 |
{ |
| 473 |
U_DEL(s64_ref, MASK64); |
| 474 |
} |
| 475 |
|
| 476 |
static ScmObj u64_delete(Leaf *leaf, u_long index) |
| 477 |
{ |
| 478 |
U_DEL(u64_ref, MASK64); |
| 479 |
} |
| 480 |
|
| 481 |
static ScmObj f16_delete(Leaf *leaf, u_long index) |
| 482 |
{ |
| 483 |
U_DEL(f16_ref, MASK16); |
| 484 |
} |
| 485 |
|
| 486 |
static ScmObj f32_delete(Leaf *leaf, u_long index) |
| 487 |
{ |
| 488 |
U_DEL(f32_ref, MASK32); |
| 489 |
} |
| 490 |
|
| 491 |
static ScmObj f64_delete(Leaf *leaf, u_long index) |
| 492 |
{ |
| 493 |
U_DEL(f64_ref, MASK64); |
| 494 |
} |
| 495 |
|
| 496 |
/*------------------------------------------------------------------- |
| 497 |
* Uniform Sparse Vector Iter |
| 498 |
*/ |
| 499 |
|
| 500 |
static ScmObj u_iter_sub(Leaf *leaf, ScmObj (*ref)(Leaf*, u_long), |
| 501 |
int *index, int mask) |
| 502 |
{ |
| 503 |
int i = *index; |
| 504 |
for (i++; i<=mask; i++) { |
| 505 |
if (U_HAS_ENTRY(leaf, i, mask)) { |
| 506 |
*index = i; |
| 507 |
return ref(leaf, i); |
| 508 |
} |
| 509 |
} |
| 510 |
*index = i; |
| 511 |
return SCM_UNBOUND; |
| 512 |
} |
| 513 |
|
| 514 |
#define U_ITER(tag, mask) \ |
| 515 |
static ScmObj SCM_CPP_CAT(tag,_iter)(Leaf *leaf, int *index) { \ |
| 516 |
return u_iter_sub(leaf, SCM_CPP_CAT(tag,_ref), index, mask); \ |
| 517 |
} |
| 518 |
|
| 519 |
U_ITER(s8, MASK8) |
| 520 |
U_ITER(u8, MASK8) |
| 521 |
U_ITER(s16, MASK16) |
| 522 |
U_ITER(u16, MASK16) |
| 523 |
U_ITER(s32, MASK32) |
| 524 |
U_ITER(u32, MASK32) |
| 525 |
U_ITER(s64, MASK64) |
| 526 |
U_ITER(u64, MASK64) |
| 527 |
U_ITER(f16, MASK16) |
| 528 |
U_ITER(f32, MASK32) |
| 529 |
U_ITER(f64, MASK64) |
| 530 |
|
| 531 |
/*------------------------------------------------------------------- |
| 532 |
* Uniform Sparse Vector Descriptors and constructors |
| 533 |
*/ |
| 534 |
|
| 535 |
#define U_DECL(tag, TAG, shift) \ |
| 536 |
static SparseVectorDescriptor SCM_CPP_CAT(tag, _desc) = { \ |
| 537 |
SCM_CPP_CAT(tag,_ref), \ |
| 538 |
SCM_CPP_CAT(tag,_set), \ |
| 539 |
u_allocate, \ |
| 540 |
SCM_CPP_CAT(tag,_delete), \ |
| 541 |
u_clear, \ |
| 542 |
u_copy, \ |
| 543 |
SCM_CPP_CAT(tag,_iter), \ |
| 544 |
NULL, shift, \ |
| 545 |
}; \ |
| 546 |
SCM_DEFINE_BUILTIN_CLASS(SCM_CPP_CAT3(Scm_Sparse,TAG,VectorClass), \ |
| 547 |
NULL, NULL, NULL, NULL, spvec_cpl); \ |
| 548 |
SCM_DEFINE_BUILTIN_CLASS(SCM_CPP_CAT3(Scm_Sparse,TAG,MatrixClass), \ |
| 549 |
NULL, NULL, NULL, NULL, spmat_cpl) |
| 550 |
|
| 551 |
U_DECL(s8, S8, SHIFT8); |
| 552 |
U_DECL(u8, U8, SHIFT8); |
| 553 |
U_DECL(s16, S16, SHIFT16); |
| 554 |
U_DECL(u16, U16, SHIFT16); |
| 555 |
U_DECL(s32, S32, SHIFT32); |
| 556 |
U_DECL(u32, U32, SHIFT32); |
| 557 |
U_DECL(s64, S64, SHIFT64); |
| 558 |
U_DECL(u64, U64, SHIFT64); |
| 559 |
U_DECL(f16, F16, SHIFT16); |
| 560 |
U_DECL(f32, F32, SHIFT32); |
| 561 |
U_DECL(f64, F64, SHIFT64); |
| 562 |
|
| 563 |
/*=================================================================== |
| 564 |
* Generic constructor |
| 565 |
*/ |
| 566 |
|
| 567 |
ScmObj MakeSparseVector(ScmClass *klass, ScmObj defaultValue, u_long flags) |
| 568 |
{ |
| 569 |
SparseVectorDescriptor *desc = NULL; |
| 570 |
|
| 571 |
if (SCM_EQ(SCM_CLASS_SPARSE_VECTOR, klass)) desc = &g_desc; |
| 572 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S8VECTOR, klass)) desc = &s8_desc; |
| 573 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U8VECTOR, klass)) desc = &u8_desc; |
| 574 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S16VECTOR, klass)) desc = &s16_desc; |
| 575 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U16VECTOR, klass)) desc = &u16_desc; |
| 576 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S32VECTOR, klass)) desc = &s32_desc; |
| 577 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U32VECTOR, klass)) desc = &u32_desc; |
| 578 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S64VECTOR, klass)) desc = &s64_desc; |
| 579 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U64VECTOR, klass)) desc = &u64_desc; |
| 580 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F16VECTOR, klass)) desc = &f16_desc; |
| 581 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F32VECTOR, klass)) desc = &f32_desc; |
| 582 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F64VECTOR, klass)) desc = &f64_desc; |
| 583 |
else if (SCM_EQ(SCM_CLASS_SPARSE_MATRIX, klass)) desc = &g_desc; |
| 584 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S8MATRIX, klass)) desc = &s8_desc; |
| 585 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U8MATRIX, klass)) desc = &u8_desc; |
| 586 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S16MATRIX, klass)) desc = &s16_desc; |
| 587 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U16MATRIX, klass)) desc = &u16_desc; |
| 588 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S32MATRIX, klass)) desc = &s32_desc; |
| 589 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U32MATRIX, klass)) desc = &u32_desc; |
| 590 |
else if (SCM_EQ(SCM_CLASS_SPARSE_S64MATRIX, klass)) desc = &s64_desc; |
| 591 |
else if (SCM_EQ(SCM_CLASS_SPARSE_U64MATRIX, klass)) desc = &u64_desc; |
| 592 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F16MATRIX, klass)) desc = &f16_desc; |
| 593 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F32MATRIX, klass)) desc = &f32_desc; |
| 594 |
else if (SCM_EQ(SCM_CLASS_SPARSE_F64MATRIX, klass)) desc = &f64_desc; |
| 595 |
else { |
| 596 |
Scm_TypeError("class", "subclass of <sparse-vector-base>", |
| 597 |
SCM_OBJ(klass)); |
| 598 |
} |
| 599 |
|
| 600 |
SparseVector *v = SCM_NEW(SparseVector); |
| 601 |
SCM_SET_CLASS(v, klass); |
| 602 |
CompactTrieInit(&v->trie); |
| 603 |
v->numEntries = 0; |
| 604 |
v->desc = desc; |
| 605 |
v->flags = flags; |
| 606 |
v->defaultValue = defaultValue; |
| 607 |
return SCM_OBJ(v); |
| 608 |
} |
| 609 |
|
| 610 |
/*=================================================================== |
| 611 |
* Initialization |
| 612 |
*/ |
| 613 |
|
| 614 |
void Scm_Init_spvec(ScmModule *mod) |
| 615 |
{ |
| 616 |
#define INITC(klass, name) \ |
| 617 |
Scm_InitStaticClass(&klass, name, mod, NULL, 0) |
| 618 |
|
| 619 |
INITC(Scm_SparseVectorBaseClass, "<sparse-vector-base>"); |
| 620 |
INITC(Scm_SparseVectorClass, "<sparse-vector>"); |
| 621 |
INITC(Scm_SparseS8VectorClass, "<sparse-s8vector>"); |
| 622 |
INITC(Scm_SparseU8VectorClass, "<sparse-u8vector>"); |
| 623 |
INITC(Scm_SparseS16VectorClass, "<sparse-s16vector>"); |
| 624 |
INITC(Scm_SparseU16VectorClass, "<sparse-u16vector>"); |
| 625 |
INITC(Scm_SparseS32VectorClass, "<sparse-s32vector>"); |
| 626 |
INITC(Scm_SparseU32VectorClass, "<sparse-u32vector>"); |
| 627 |
INITC(Scm_SparseS64VectorClass, "<sparse-s64vector>"); |
| 628 |
INITC(Scm_SparseU64VectorClass, "<sparse-u64vector>"); |
| 629 |
INITC(Scm_SparseF16VectorClass, "<sparse-f16vector>"); |
| 630 |
INITC(Scm_SparseF32VectorClass, "<sparse-f32vector>"); |
| 631 |
INITC(Scm_SparseF64VectorClass, "<sparse-f64vector>"); |
| 632 |
|
| 633 |
INITC(Scm_SparseMatrixBaseClass, "<sparse-matrix-base>"); |
| 634 |
INITC(Scm_SparseMatrixClass, "<sparse-matrix>"); |
| 635 |
INITC(Scm_SparseS8MatrixClass, "<sparse-s8matrix>"); |
| 636 |
INITC(Scm_SparseU8MatrixClass, "<sparse-u8matrix>"); |
| 637 |
INITC(Scm_SparseS16MatrixClass, "<sparse-s16matrix>"); |
| 638 |
INITC(Scm_SparseU16MatrixClass, "<sparse-u16matrix>"); |
| 639 |
INITC(Scm_SparseS32MatrixClass, "<sparse-s32matrix>"); |
| 640 |
INITC(Scm_SparseU32MatrixClass, "<sparse-u32matrix>"); |
| 641 |
INITC(Scm_SparseS64MatrixClass, "<sparse-s64matrix>"); |
| 642 |
INITC(Scm_SparseU64MatrixClass, "<sparse-u64matrix>"); |
| 643 |
INITC(Scm_SparseF16MatrixClass, "<sparse-f16matrix>"); |
| 644 |
INITC(Scm_SparseF32MatrixClass, "<sparse-f32matrix>"); |
| 645 |
INITC(Scm_SparseF64MatrixClass, "<sparse-f64matrix>"); |
| 646 |
} |