-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdyadicmemoryCL.c
140 lines (103 loc) · 3.56 KB
/
dyadicmemoryCL.c
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
/*
dyadicmemoryCL.c
Dyadic Memory Command Line wrapper
Copyright (c) 2022-2024 Peter Overmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "triadicmemory.h"
static int VERSIONMAJOR = 2;
static int VERSIONMINOR = 0;
static void print_help(void)
{
printf("dyadicmemory %d.%d\n\n", VERSIONMAJOR, VERSIONMINOR);
printf("Sparse distributed memory (SDM) for storing associations x->y of sparse binary hypervectors x and y.\n");
printf("A hypervector of dimension n is given by an ordered set of p integers with values from 1 to n which represent its \"1\" bits.\n");
printf("\n");
printf("Command line arguments:\n\n");
printf("dyadicmemory n p (n is the dimension of x and y, p is the target sparse population of y)\n");
printf("dyadicmemory nx ny p (nx and ny are the dimensions of x and y, p is the target sparse population of y)\n\n");
printf("Usage examples:\n\n");
printf("Store x->y:\n");
printf("1 20 195 355 371 471 603 814 911 999, 13 29 41 182 590 711 714 773 925 967\n\n");
printf("Recall y for a given x:\n");
printf("1 20 195 355 371 471 603 814 911 999\n\n");
printf("Print this help text:\n");
printf("help\n\n");
printf("Show version number:\n");
printf("version\n\n");
printf("Terminate process:\n");
printf("quit\n\n");
}
int main(int argc, char *argv[])
{
char *buf, inputline[10000];
int Nx, Ny, P; // vector dimension and target sparse population
if (argc == 3)
{
sscanf( argv[1], "%d", &Nx); Ny = Nx;
sscanf( argv[2], "%d", &P);
}
else if (argc == 4)
{
sscanf( argv[1], "%d", &Nx);
sscanf( argv[2], "%d", &Ny);
sscanf( argv[3], "%d", &P);
}
else {
print_help();
exit(1);
}
if (Nx > NMAX)
{
printf("vector dimension %d exceeds maximum value %d\n", Nx, NMAX);
exit(20);
}
SDR *x = sdr_new(Nx);
SDR *y = sdr_new(Ny);
DyadicMemory *D = dyadicmemory_new(Nx, Ny, P);
while ( fgets(inputline, sizeof(inputline), stdin) != NULL)
{
if ( strcmp(inputline, "quit\n") == 0)
return 0;
if ( strcmp(inputline, "version\n") == 0)
printf("%d.%d\n", VERSIONMAJOR, VERSIONMINOR);
else if ( strcmp(inputline, "help\n") == 0)
print_help();
else // parse x
{
buf = sdr_parse(inputline, x);
if (*buf == SEPARATOR) // parse y
{
sdr_parse(buf+1, y);
dyadicmemory_write (D, x,y);
}
else if (*buf == 0) // query
{
dyadicmemory_read (D, x, y);
sdr_print(y);
}
else
{
printf("invalid input\n");
exit(5);
}
}
}
return 0;
}