Sqlite : format an integer - sqlite

Is it possible to format an integer column to have a comma as a thousandth digit separator in Sqlite? I would like the query result to be as follows.
Id Name Price
1 Product1 1,000
2 Product2 2,500

There is not an internal number formatting function built into SQLite, but you can create a user-defined function to create one.
Here's a working example. Uses integers and will need adjustment for floats.
#import <sqlite3.h>
static char *sepnum(int num, char *buf, size_t buflen, char sepch)
{
int len = 0, negative = 0;
if (!buf || buflen == 0) return buf;
if (num < 0)
{
negative = 1;
num = abs(num);
}
for (int i=1;;i++)
{
if (buflen>len+1) buf[len++] = '0' + (num % 10);
num /= 10;
if (num==0) break;
if ((i % 3) == 0 && buflen>len+1) buf[len++] = sepch;
}
if (negative && buflen>len+1) buf[len++] = '-';
for (int i=0;i<len/2;i++)
{
buf[len] = buf[i];
buf[i] = buf[len-i-1];
buf[len-i-1] = buf[len];
}
if (buflen>len) buf[len] = '\0';
else buf[0] = '\0';
return buf;
}
static void nformat(sqlite3_context *context, int argc, sqlite3_value **argv)
{
if (argc == 1)
{
int num = sqlite3_value_int(argv[0]);
char buf[500] = "";
if (sepnum(num, buf, sizeof(buf), ','))
{
sqlite3_result_text(context, buf, -1, SQLITE_TRANSIENT);
return;
}
}
sqlite3_result_null(context);
}
int main(int argc, char *argv[])
{
sqlite3 *db;
sqlite3_open(":memory:", &db);
sqlite3_stmt *stmt;
sqlite3_create_function(db, "nformat", 1, SQLITE_UTF8, NULL, &nformat, NULL, NULL);
sqlite3_prepare(db, "select nformat(1234)", -1, &stmt, NULL);
sqlite3_step(stmt);
printf("[%s]\n", sqlite3_column_text(stmt, 0));
}

If your data is stored in a table and you are using SQLite version 3.8.3 or higher, then it is possible to accomplish what you want using a recursive CTE.
Below is a working SQL example of what I'm referring to.
WITH
NBR_LIST_ETL AS
(
SELECT
A.*,
CAST(REPLACE(NBR_LENGTH % 3, 0, 3) AS INTEGER)
AS SUB_STRING_LENGTH
FROM
(
SELECT
ID, -- INTEGER PRIMARY KEY COLUMN
NBR, -- INTEGER COLUMN YOU WANT TO FORMAT W/ COMMAS
LENGTH(NBR)
AS NBR_LENGTH
FROM
NBR_LIST A -- REPLACE NBR_LIST W/ YOUR TABLE NAME
) A
),
NBR_FORMAT_RECURSIVE AS
(
SELECT
ID,
SUBSTR(NBR, 1, SUB_STRING_LENGTH)
AS NBR_SEGMENT,
SUBSTR(NBR, SUB_STRING_LENGTH + 1)
AS NBR_REMAINING,
NBR_LENGTH - SUB_STRING_LENGTH
AS NBR_LENGTH
FROM
NBR_LIST_ETL
UNION ALL
SELECT
ID,
SUBSTR(NBR_REMAINING, 1, 3)
AS NBR_SEGMENT,
SUBSTR(NBR_REMAINING, 4)
AS NBR_REMAINING,
NBR_LENGTH - 3
AS NBR_LENGTH
FROM
NBR_FORMAT_RECURSIVE
WHERE
NBR_LENGTH >= 3
)
SELECT
GROUP_CONCAT(NBR_SEGMENT)
AS NBR_FORMATTED
FROM
NBR_FORMAT_RECURSIVE
GROUP BY
ID
If your table contained the following numbers -
NBR
25271
7
29
75438175
342
212
4758
863
2313917
Then the query would output -
NBR_FORMATTED
25,271
7
29
75,438,175
342
212
4,758
863
2,313,917

Related

how to tokenize a string in arduino

i am using arduino due. what i am trying to do is to receive a string at serial. like this one:
COMSTEP 789 665 432 END
if the string starts with comstep, then to tokenize the string and get an integer array {789, 665, 432}.
is there anyway to do that?
P.S: im a noob at programming, so any help is appreciated.
I have a function that I wrote long ago to parse strings up in an easy manner. It is in use on several of my Arduino projects.
Sample usage:
char pinStr[3];
char valueStr[7];
int pinNumber, value;
getstrfld (parms_in, 0, 0, (char *)",", pinStr);
getstrfld (parms_in, 1, 0, (char *)",", valueStr);
pinNumber = atoi (pinStr);
value = atoi (valueStr);
The functions:
// My old stand-by to break delimited strings up.
char * getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr)
{
char *offset, *strptr;
int curfld;
offset = strptr = (char *)NULL;
curfld = 0;
strbuf += ofset;
while (*strbuf) {
strptr = !offset ? strbuf : offset;
offset = strpbrk ((!offset ? strbuf : offset), sep);
if (offset) {
offset++;
} else if (curfld != fldno) {
*retstr = 0;
break;
}
if (curfld == fldno) {
strncpy (retstr, strptr,
(int)(!offset ? strlen (strptr)+ 1 :
(int)(offset - strptr)));
if (offset)
retstr[offset - strptr - 1] = 0;
break;
}
curfld++;
}
return retstr;
}
// Included because strpbrk is not in the arduino gcc/g++ libraries
// Or I just could not find it :)
char * strpbrk (const char *s1, const char *s2)
{
const char *c = s2;
if (!*s1) {
return (char *) NULL;
}
while (*s1) {
for (c = s2; *c; c++) {
if (*s1 == *c)
break;
}
if (*c)
break;
s1++;
}
if (*c == '\0')
s1 = NULL;
return (char *) s1;
}
A light-weight approach (no strict checks on valid parses of the integers and ignoring any list elements past a fixed maximum):
char buf[32] = "COMSTEP 789 665 432 END"; // assume this has just been read
int res[8], nres = 0;
bool inlist = false;
for (char *p = strtok(buf, " "); p; p = strtok(0, " "))
if (inlist)
{
if (!strcmp(p, "END"))
{
inlist = false;
break;
}
else if (nres < sizeof(res) / sizeof(*res))
res[nres++] = atoi(p);
}
else if (!strcmp(p, "COMSTEP"))
inlist = true;
if (!inlist)
for (size_t i = 0; i < nres; ++i)
printf("%d%s", res[i], i + 1 < nres ? " " : "\n"); // do whatever

String to hex splitting

I am having a QString :
QString str = "1000140035";
I want to extract each byte from above string like this :--
Unsigned char a = 0x10
Unsigned char b = 0x00
Unsigned char c = 0x14
Unsigned char d = 0x00
Unsigned char e = 0x35
Please suggest how to do this ... tried but failed many times.
QByteArray::fromHex + QString::toLatin1()
const QString str = "1000140035";
const QByteArray data = QByteArray::fromHex( str.toLatin1() );
unsigned char a = data[0];
unsigned char b = data[1];
unsigned char c = data[2];
unsigned char d = data[3];
unsigned char e = data[4];
You can use QString::toInt and set the base to 16 to convert a hex-string to int (or QString::toUInt).
QString str = "1000140035";
while(!str.isEmpty())
{
unsigned char byte = str.left(2).toInt(0, 16);
// do something with byte, store it somewhere or whatever...
str.remove(0, 2);
}
Perhaps this is what you're looking for? The code will parse an arbitrary length hexadecimal string and return an array of 2-digit hexadecimal numbers along with their count. It allocates memory for the result, so it is your responsibility to release it afterwards. The number of extracted numbers is returned through the third argument.
#include <QString>
#include <cmath>
unsigned HexToNum(char c)
{
if(c >= '0' && c <= '9')
return c - '0';
if(c >= 'A' && c <= 'F')
return 15 + c - 'A';
if(c >= 'a' && c <= 'f')
return 15 + c - 'a';
//Non hex char passed, return 0
return 0;
}
void ExtractBytes(const QString& hexString, unsigned char*& result, int& resultNumbersCount)
{
resultNumbersCount = static_cast<int>(ceil(hexString.length() / 2));
result = new unsigned char[resultNumbersCount];
int i, j = -1;
for(i = 0; i < resultNumbersCount; ++i)
result[i] = 0;
for(i = 0; i < hexString.length(); ++i)
{
if(i % 2 == 0)
++j;
result[j] = result[j] * 16 + HexToNum(hexString.at(i).toLatin1());
}
}

convert HEX string to Decimal in arduino

i have an Hex String like this : "0005607947" and want to convert it to Decimal number , i test it on this site and it correctly convert to decimal number and answer is : "90208583" but when i use this code i get wrong value !
where of my code is wrong or did have any one , some new code for this problem ?
long int decimal_answer = getDEC("0005607947") ;
long int getDEC(String str110) {
long int ID = 0 ;
int len = str110.length() ;
char buff[len] ;
int power = 0 ;
for(int i = 0 ; i <len ; i++) { buff[i] = str110.charAt(i); }
for(int i = (len-1) ; i >=0 ; i--) {
int num = buff[i] - '0' ;
ID = ID + num * pow(16 , power) ;
power = power + 1 ;
}
Serial.println(String(ID , DEC));
return ID ;
}
// thanks , i also use this but , get error : invalid conversion from 'void*' to 'char**' [-fpermissive]
unsigned int SiZe = sizeof(F_value) ;
char charBuf[SiZe];
F_value.toCharArray(charBuf , SiZe);
long decimal_answer = strtol(charBuf , NULL , 16);
Serial.println(decimal_answer , DEC);
Drop all that code, and just use 'strtol' from the standard library.
#include <stdlib.h>
long strtol (const char *__nptr, char **__endptr, int __base)
For your use:
long decimal_answer = strtol("0005607947", NULL, 16);
You are trying to store the value 90208583 in an int. Arduino has a 2 byte int size meaning that the largest number you can store is 2^16-1 (65535). You have a couple of options:
Use an unsigned int
min number: 0
max number: 4,294,967,295
cons: can only be used for positive numbers
Use a long int
min number: -2,147,483,648
max number: 2,147,483,647

SSE2 intrinsics - comparing 2 __m128i's containing 4 int32's each to see how many are equal

I'm diving in SSE2 intrinsics for the first time and I'm not sure how to do this.
I want to compare 4 int32's to 4 other int32's and count how many are equal.
So I read my first 4 int32's, set them in a __m128i, do the same for the second set, and use _mm_cmpeq_epi32 for the comparison.
This should result in a __m128i containing 4 int32's, each one either 0xffffffff or 0 depending on whether the ints were equal.
But I have no idea how to get from that resulting __m128i to a count specifying how many were actually equal.
Can anyone point me in the right direction ?
The code as far as I'm piecing it together :
int* source = blah;
int* reference = otherblah;
// Load the 4 source int32's (they are actually 4 int32s apart)
__m128i first_4_int32s = _mm_set_epi32(*(source + 12), *(source + 8), *(source + 4), *(source));
// Load the 4 source int32's (also actually 4 int32s apart)
__m128i second_4_int32s = _mm_set_epi32(*(reference + 12), *(reference + 8), *(reference + 4), *(reference));
// Compare the int32's
__m128i result = _mm_cmpeq_epi32(first_4_int32s, second_4_int32s);
// Perform magic here that counts whether 0, 1, 2, 3 or all 4 ints were equal ?!?!
You can AND the compare result with a vector of ones to create a vector of zeros and ones. Then use a horizontal add operation to count the ones. Here are some possibilities.
#include "stdio.h"
#include "stdint.h"
#include "intrin.h"
//----------------------------------------------------------------------------
// non-SSE method (reference for result check)
static int method0 (__m128i value)
{
int index, total = 0;
uint32_t *buffer = (void *) &value;
for (index = 0; index < 4; index++)
total += buffer [index] == 0xFFFFFFFF;
return total;
}
//----------------------------------------------------------------------------
//
// horizontalAddBytes - return integer total of all 16 bytes in xmm argument
//
static int horizontalAddBytes (__m128i byteArray)
{
__m128i total;
const __m128i zero = _mm_setzero_si128 ();
total = _mm_sad_epu8 (byteArray, zero);
return _mm_cvtsi128_si64 (_mm_add_epi32 (total, _mm_shuffle_epi32 (total, 0xAA)));
}
//----------------------------------------------------------------------------
// requires SSE2
static int method1 (__m128i value)
{
return horizontalAddBytes (_mm_srli_epi32 (value, 31));
}
//----------------------------------------------------------------------------
// requires SSE3
static int method2 (__m128i value)
{
__m128 count;
const __m128 mask = _mm_set1_ps (1);
count = _mm_and_ps (_mm_castsi128_ps (value), mask);
count = _mm_hadd_ps (count, count);
count = _mm_hadd_ps (count, count);
return _mm_cvtss_si32 (count);
}
//----------------------------------------------------------------------------
// requires SSSE3
static int method3 (__m128i value)
{
__m128i count;
count = _mm_srli_epi32 (value, 31);
count = _mm_hadd_epi32 (count, count);
count = _mm_hadd_epi32 (count, count);
return _mm_cvtsi128_si32 (count);
}
//----------------------------------------------------------------------------
static void createTestData (uint32_t *data, int mask)
{
int index;
for (index = 0; index < 4; index++)
data [index * 4] = (mask & (1 << index)) != 0;
}
//----------------------------------------------------------------------------
int main (void)
{
int index1, index2, expected, result1, result2, result3;
uint32_t source [16];
uint32_t reference [16];
for (index1 = 0; index1 < 16; index1++)
for (index2 = 0; index2 < 16; index2++)
{
__m128i first_4_int32s, second_4_int32s, result;
createTestData (source, index1);
createTestData (reference, index2);
// Load the 4 source int32's (they are actually 4 int32s apart)
first_4_int32s = _mm_set_epi32(*(source + 12), *(source + 8), *(source + 4), *(source));
// Load the 4 source int32's (also actually 4 int32s apart)
second_4_int32s = _mm_set_epi32(*(reference + 12), *(reference + 8), *(reference + 4), *(reference));
// Compare the int32's
result = _mm_cmpeq_epi32(first_4_int32s, second_4_int32s);
expected = method0 (result);
result1 = method1 (result);
result2 = method2 (result);
result3 = method3 (result);
if (result1 != expected) printf ("method1, index %d,%d expected %d, actual %d\n", index1, index2, expected, result1);
if (result2 != expected) printf ("method2, index %d,%d expected %d, actual %d\n", index1, index2, expected, result2);
if (result3 != expected) printf ("method3, index %d,%d expected %d, actual %d\n", index1, index2, expected, result3);
}
return 0;
}
//----------------------------------------------------------------------------

Spellcheck program using MPI

So, my assignment is to write a spell check program and then parallelize it using openMPI. My take was to load the words from a text file into my array called dict[] and this is used as my dictionary. Next, I get input from the user and then it's supposed go through the dictionary array and check whether the current word is within the threshold percentage, if it is, print it out. But I'm only supposed to print out a certain amount of words. My problem is, is that, my suggestions[] array, doesn't seem to fill up the way I need it to, and it gets a lot of blank spots in it, whereas, I thought at least, is that the way I wrote it, is to just fill it when a word is within threshold. So it shouldn't get any blanks in it until there are no more words being added. I think it's close to being finished but I can't seem to figure this part out. Any help is appreciated.
#include <stdio.h>
#include <mpi.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 30
#define max(x,y) (((x) > (y)) ? (x) : (y))
char *dict[50000];
char *suggestions[50000];
char enterWord[50];
char *myWord;
int wordsToPrint = 20;
int threshold = 40;
int i;
int words_added = 0;
int levenshtein(const char *word1, int len1, const char *word2, int len2){
int matrix[len1 + 1][len2 + 1];
int a;
for(a=0; a<= len1; a++){
matrix[a][0] = a;
}
for(a=0;a<=len2;a++){
matrix[0][a] = a;
}
for(a = 1; a <= len1; a++){
int j;
char c1;
c1 = word1[a-1];
for(j = 1; j <= len2; j++){
char c2;
c2 = word2[j-1];
if(c1 == c2){
matrix[a][j] = matrix[a-1][j-1];
}
else{
int delete, insert, substitute, minimum;
delete = matrix[a-1][j] +1;
insert = matrix[a][j-1] +1;
substitute = matrix[a-1][j-1] +1;
minimum = delete;
if(insert < minimum){
minimum = insert;
}
if(substitute < minimum){
minimum = substitute;
}
matrix[a][j] = minimum;
}//else
}//for
}//for
return matrix[len1][len2];
}//levenshtein
void prompt(){
printf("Enter word to search for: \n");
scanf("%s", &enterWord);
}
int p0_compute_output(int num_processes, char *word1){
int totalNumber = 0;
int k = 0;
int chunk = 50000 / num_processes;
for(i = 0; i < chunk; i++){
int minedits = levenshtein(word1, strlen(word1), dict[i], strlen(dict[i]));
int thresholdPercentage = (100 * minedits) / max(strlen(word1), strlen(dict[i]));
if(thresholdPercentage < threshold){
suggestions[totalNumber] = dict[i];
totalNumber = totalNumber + 1;
}
}//for
return totalNumber;
}//p0_compute_output
void p0_receive_output(int next_addition){
int num_to_add;
MPI_Comm comm;
MPI_Status status;
MPI_Recv(&num_to_add,1,MPI_INT,MPI_ANY_SOURCE, MPI_ANY_TAG,MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("--%d\n", num_to_add);
suggestions[next_addition] = dict[num_to_add];
next_addition = next_addition + 1;
}
void compute_output(int num_processes, int me, char *word1){
int chunk = 0;
int last_chunk = 0;
MPI_Comm comm;
if(50000 % num_processes == 0){
chunk = 50000 / num_processes;
last_chunk = chunk;
int start = me * chunk;
int end = me * chunk + chunk;
for(i = start; i < end;i++){
int minedits = levenshtein(word1, strlen(word1), dict[i], strlen(dict[i]));
int thresholdPercentage = (100 * minedits) / max(strlen(word1), strlen(dict[i]));
if(thresholdPercentage < threshold){
int number_to_send = i;
MPI_Send(&number_to_send, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
}
}
else{
chunk = 50000 / num_processes;
last_chunk = 50000 - ((num_processes - 1) * chunk);
if(me != num_processes){
int start = me * chunk;
int end = me * chunk + chunk;
for(i = start; i < end; i++){
int minedits = levenshtein(word1, strlen(word1), dict[i], strlen(dict[i]));
int thresholdPercentage = (100 * minedits) / max(strlen(word1), strlen(dict[i]));
if(thresholdPercentage < threshold){
int number_to_send = i;
MPI_Send(&number_to_send, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
}//if
}//for
}//if me != num_processes
else{
int start = me * chunk;
int end = 50000 - start;
for(i = start; i < end; i++){
int minedits = levenshtein(word1, strlen(word1), dict[i], strlen(dict[i]));
int thresholdPercentage = (100 * minedits) / max(strlen(word1), strlen(dict[i]));
if(thresholdPercentage < threshold){
int number_to_send = i;
MPI_Send(&number_to_send, 1, MPI_INT, 0, 1, MPI_COMM_WORLD);
}
}
}//me == num_processes
}//BIG else
return;
}//COMPUTE OUTPUT
void set_data(){
prompt();
MPI_Bcast(&enterWord,20 ,MPI_CHAR, 0, MPI_COMM_WORLD);
}//p0_send_inpui
//--------------------------MAIN-----------------------------//
main(int argc, char **argv){
int ierr, num_procs, my_id, loop;
FILE *myFile;
loop = 0;
for(i=0;i<50000;i++){
suggestions[i] = calloc(SIZE, sizeof(char));
}
ierr = MPI_Init(NULL, NULL);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
printf("Check in from %d of %d processors\n", my_id, num_procs);
set_data();
myWord = enterWord;
myFile = fopen("words", "r");
if(myFile != NULL){
for(i=0;i<50000;i++){
dict[i] = calloc(SIZE, sizeof(char));
fscanf(myFile, "%s", dict[i]);
}//for
fclose(myFile);
}//read word list into dictionary
else printf("File not found");
if(my_id == 0){
words_added = p0_compute_output(num_procs, enterWord);
printf("words added so far: %d\n", words_added);
p0_receive_output(words_added);
printf("Threshold: %d\nWords To print: %d\n%s\n", threshold, wordsToPrint, myWord);
ierr = MPI_Finalize();
}
else{
printf("my word %s*\n", enterWord);
compute_output(num_procs, my_id, enterWord);
// printf("Process %d terminating...\n", my_id);
ierr = MPI_Finalize();
}
for(i=0;i<wordsToPrint;i++){
printf("*%s\n", suggestions[i]);
}//print suggestions
return (0);
}//END MAIN
Here are a few problems I see with what you're doing:
prompt() should only be called by rank 0.
The dictionary file should be read only by rank 0, then broadcast the array out to the other ranks
Alternatively, have rank 1 read the file while rank 0 is waiting for input, broadcast input and dictionary afterwards.
You're making the compute_output step overly complex. You can merge p0_compute_output and compute_output into one routine.
Store an array of indices into dict in each rank
This array will not be the same size in every rank, so the simplest way to do this would be to send from each rank a single integer indicating the size of the array, then send the array with this size. (The receiving rank must know how much data to expect). You could also use the sizes for MPI_Gatherv, but I expect this is more than you're wanting to do right now.
Once you have a single array of indices in rank 0, then use this to fill suggestions.
Save the MPI_Finalize call until immediately before the return call
For the final printf call, only rank 0 should be printing that. I suspect this is causing a large part of the "incorrect" result. As you have it, all ranks are printing suggestions, but it is only filled in rank 0. So the others will all be printing blank entries.
Try some of these changes, especially the last one, and see if that helps.

Resources