Quicksort dll and example.

git-svn-id: svn://kolibrios.org@3402 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Albom 2013-03-22 17:53:23 +00:00
parent 18616a95fe
commit d21d1d7b9c
12 changed files with 690 additions and 0 deletions

View File

@ -0,0 +1,2 @@
gcc -c -o qs.obj qs.c
pause

View File

@ -0,0 +1,4 @@
gcc -c example.c
ld -nostdlib -T kolibri.ld -o example.kex start.o kolibri.o stdlib.o string.o example.o
objcopy example.kex -O binary
pause

View File

@ -0,0 +1,88 @@
///===========================
#define CON_COLOR_BLUE 1
#define CON_COLOR_GREEN 2
#define CON_COLOR_RED 4
#define CON_COLOR_BRIGHT 8
/* öâåò ôîíà */
#define CON_BGR_BLUE 0x10
#define CON_BGR_GREEN 0x20
#define CON_BGR_RED 0x40
#define CON_BGR_BRIGHT 0x80
///===========================
void (* _stdcall con_init)(unsigned w_w, unsigned w_h, unsigned s_w, unsigned s_h, const char* t);
void (* _cdecl printf)(const char* format,...);
void (* _stdcall _exit)(char bCloseWindow);
void (* __stdcall gets)(char* str, int n);
int (* __stdcall getch)(void);
int (* __stdcall con_get_font_height)(void);
int (* __stdcall con_set_cursor_height)(int new_height);
unsigned (*__stdcall con_get_flags)(void);
unsigned (*__stdcall con_set_flags)(unsigned new_flags);
void (*__stdcall con_cls)(void);
///===========================
void CONSOLE_INIT(char title[])
{
kol_struct_import *imp;
imp = kol_cofflib_load("/sys/lib/console.obj");
if (imp == NULL)
kol_exit();
con_init = ( _stdcall void (*)(unsigned, unsigned, unsigned, unsigned, const char*))
kol_cofflib_procload (imp, "con_init");
if (con_init == NULL)
kol_exit();
printf = ( _cdecl void (*)(const char*,...))
kol_cofflib_procload (imp, "con_printf");
if (printf == NULL)
kol_exit();
_exit = ( _stdcall void (*)(char))
kol_cofflib_procload (imp, "con_exit");
if (_exit == NULL)
kol_exit();
gets = ( _stdcall void (*)(char*, int))
kol_cofflib_procload (imp, "con_gets");
if (gets == NULL)
kol_exit();
getch = ( _stdcall int (*)(void))
kol_cofflib_procload (imp, "con_getch2");
if (getch == NULL)
kol_exit();
con_get_font_height = ( _stdcall int (*)(void))
kol_cofflib_procload (imp, "con_get_font_height");
if (con_get_font_height == NULL)
kol_exit();
con_set_cursor_height = ( _stdcall int (*)(int))
kol_cofflib_procload (imp, "con_set_cursor_height");
if (con_set_cursor_height == NULL)
kol_exit();
con_get_flags = ( _stdcall unsigned (*)(void))
kol_cofflib_procload (imp, "con_get_flags");
if (con_get_flags == NULL)
kol_exit();
con_set_flags = ( _stdcall unsigned (*)(unsigned))
kol_cofflib_procload (imp, "con_set_flags");
if (con_set_flags == NULL)
kol_exit();
con_cls = ( _stdcall void (*)(void))
kol_cofflib_procload (imp, "con_cls");
if (con_cls == NULL)
kol_exit();
con_init(-1, -1, -1, -1, title);
}

View File

@ -0,0 +1,61 @@
#include "kolibri.h"
#include "stdlib.h"
#include "string.h"
#include "console.c"
void (* __stdcall qsi)(int* d, int n);
/// ===========================================================
void kol_main()
{
#define NUM 20000
kol_struct_import *imp_qs;
int *a;
int i;
CONSOLE_INIT("Example");
imp_qs = kol_cofflib_load("/sys/lib/qs.obj");
qsi = ( _stdcall void (*)(int*, int))
kol_cofflib_procload (imp_qs, "qsi");
a = malloc(NUM*sizeof(int));
for (i = 0; i < NUM; i++)
*(a+i) = random(10000);
for (i = 0; i < 5; i++)
printf("%7d", *(a+i));
printf (" ...");
for (i = NUM-5; i < NUM; i++)
printf("%7d", *(a+i));
qsi(a, NUM);
printf ("\n");
for (i = 0; i < 5; i++)
printf("%7d", *(a+i));
printf (" ...");
for (i = NUM-5; i < NUM; i++)
printf("%7d", *(a+i));
free(a);
_exit(0);
kol_exit();
}
/// ===========================================================

View File

@ -0,0 +1,90 @@
#define NULL ((void*)0)
typedef struct
{
unsigned p00 __attribute__((packed));
unsigned p04 __attribute__((packed));
unsigned p08 __attribute__((packed));
unsigned p12 __attribute__((packed));
unsigned p16 __attribute__((packed));
char p20 __attribute__((packed));
char *p21 __attribute__((packed));
} kol_struct70 __attribute__((packed));
typedef struct
{
unsigned p00 __attribute__((packed));
char p04 __attribute__((packed));
char p05[3] __attribute__((packed));
unsigned p08 __attribute__((packed));
unsigned p12 __attribute__((packed));
unsigned p16 __attribute__((packed));
unsigned p20 __attribute__((packed));
unsigned p24 __attribute__((packed));
unsigned p28 __attribute__((packed));
unsigned p32[2] __attribute__((packed));
unsigned p40 __attribute__((packed));
} kol_struct_BDVK __attribute__((packed));
typedef struct
{
char *name __attribute__((packed));
void *data __attribute__((packed));
} kol_struct_import __attribute__((packed));
void kol_exit();
void kol_sleep(unsigned d);
void kol_wnd_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned c);
void kol_wnd_move(unsigned x, unsigned y);
void kol_wnd_caption(char *s);
void kol_event_mask(unsigned e);
unsigned kol_event_wait();
unsigned kol_event_wait_time(unsigned time);
unsigned kol_event_check();
void kol_paint_start();
void kol_paint_end();
void kol_paint_pixel(unsigned x, unsigned y, unsigned c);
void kol_paint_bar(unsigned x, unsigned y, unsigned w, unsigned h, unsigned c);
void kol_paint_line(unsigned x1, unsigned y1, unsigned x2, unsigned y2, unsigned c);
void kol_paint_string(unsigned x, unsigned y, char *s, unsigned c);
void kol_paint_image(unsigned x, unsigned y, unsigned w, unsigned h, char *d);
void kol_paint_image_pal(unsigned x, unsigned y, unsigned w, unsigned h, char *d, unsigned *palette);
unsigned kol_key_get();
unsigned kol_key_control();
void kol_key_lang_set(unsigned lang);
unsigned kol_key_lang_get();
void kol_key_mode_set(unsigned mode);
unsigned kol_key_mode_get();
void kol_btn_define(unsigned x, unsigned y, unsigned w, unsigned h, unsigned d, unsigned c);
unsigned kol_btn_get();
void kol_btn_type(unsigned t);
unsigned kol_mouse_pos();
unsigned kol_mouse_posw();
unsigned kol_mouse_btn();
void kol_board_putc(char c);
void kol_board_puts(char *s);
void kol_board_puti(int n);
int kol_file_70(kol_struct70 *k);
kol_struct_import* kol_cofflib_load(char *name);
void* kol_cofflib_procload (kol_struct_import *imp, char *name);
unsigned kol_cofflib_procnum (kol_struct_import *imp);
void kol_cofflib_procname (kol_struct_import *imp, char *name, unsigned n);
unsigned kol_system_end(unsigned param);
unsigned kol_system_cpufreq();
unsigned kol_system_mem();
unsigned kol_system_memfree();
unsigned kol_system_time_get();
unsigned kol_system_date_get();
void kol_path_file2dir(char *dir, char *fname);
void kol_path_full(char *full, char *fname);
void kol_screen_wait_rr();
void kol_screen_get_size(unsigned *w, unsigned *h);
unsigned kol_skin_height();
unsigned kol_thread_start(unsigned start, unsigned stack);
unsigned kol_time_tick();
unsigned kol_sound_speaker(char data[]);
unsigned kol_process_info(unsigned slot, char buf1k[]);
int kol_process_kill_pid(unsigned process);

View File

@ -0,0 +1,20 @@
/*OUTPUT_FORMAT("binary")*/
ENTRY(Start)
SECTIONS
{
.text 0x000000:
{
*(.text)
}
.data : {
*(.data)
hEnd = . ;
}
.bss :
{
*(.bss)
}
Memory = . ;
}

View File

@ -0,0 +1,14 @@
#define RAND_MAX 0x7FFFU
#define isspace(c) ((c)==' ')
#define abs(i) (((i)<0)?(-(i)):(i))
#define random(num) ((rand()*(num))/((RAND_MAX+1)))
void* malloc(unsigned size);
void free(void *pointer);
void* realloc(void* pointer, unsigned size);
void srand (unsigned seed);
int rand (void);

View File

@ -0,0 +1,12 @@
#define NULL ((void*)0)
void* memset(void *mem, int c, unsigned size);
void* memcpy(void *dst, const void *src, unsigned size);
void strcat(char strDest[], char strSource[]);
int strcmp(const char* string1, const char* string2);
void strcpy(char strDest[], const char strSource[]);
char* strncpy(char *strDest, const char *strSource, unsigned n);
int strlen(const char* string);
char *strchr(const char* string, int c);

View File

@ -0,0 +1,339 @@
///===========================================
///
/// Áèáëèîòåêà ôóíêöèé áûñòðîé ñîðòèðîâêè
///
///
/// Áàçîâûé êîä áûë âçÿò ñ ñàéòà algolist.manual.ru
///
/// Ñêîìïîíîâàë À. Áîãîìàç aka Albom (albom85@yandex.ru)
///===========================================
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà int (4 áàéòà)
///===========================================
void qsi(int *a, int n)
{
int i, j;
int temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsi(a, j);
if ( n > i )
qsi(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà short int (2 áàéòà)
///===========================================
void qss(short *a, int n)
{
int i, j;
short temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qss(a, j);
if ( n > i )
qss(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà char (1 áàéò)
///===========================================
void qsc(char *a, int n)
{
int i, j;
char temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsc(a, j);
if ( n > i )
qsc(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned int (4 áàéòà)
///===========================================
void qsui(unsigned *a, int n)
{
int i, j;
unsigned temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsui(a, j);
if ( n > i )
qsui(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned short int (2 áàéòà)
///===========================================
void qsus(unsigned short *a, int n)
{
int i, j;
unsigned short temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsus(a, j);
if ( n > i )
qsus(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned char (1 áàéò)
///===========================================
void qsuc(unsigned char *a, int n)
{
int i, j;
unsigned char temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsuc(a, j);
if ( n > i )
qsuc(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà float (4 áàéòà)
///===========================================
void qsf(float *a, int n)
{
int i, j;
float temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsf(a, j);
if ( n > i )
qsf(a+i, n-i);
}
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà double (8 áàéò)
///===========================================
void qsd(double *a, int n)
{
int i, j;
double temp, p;
p = *(a+(n>>1));
i = 0;
j = n;
do
{
while ( *(a+i) < p ) i++;
while ( *(a+j) > p ) j--;
if (i <= j)
{
temp = *(a+i);
*(a+i) = *(a+j);
*(a+j) = temp;
i++;
j--;
}
} while ( i<=j );
if ( j > 0 )
qsd(a, j);
if ( n > i )
qsd(a+i, n-i);
}
///===========================================
#define NULL ((void*)0)
typedef struct
{
void *name;
void *function;
} export_t;
char szQsi[] = "qsi";
char szQss[] = "qss";
char szQsc[] = "qsc";
char szQsui[] = "qsui";
char szQsus[] = "qsus";
char szQsuc[] = "qsuc";
char szQsf[] = "qsf";
char szQsd[] = "qsd";
export_t EXPORTS[] =
{
{ szQsi, (void*) qsi },
{ szQss, (void*) qss },
{ szQsc, (void*) qsc },
{ szQsui, (void*) qsui },
{ szQsus, (void*) qsus },
{ szQsuc, (void*) qsuc },
{ szQsf, (void*) qsf },
{ szQsd, (void*) qsd },
{ NULL, NULL },
};

View File

@ -0,0 +1,51 @@
///===========================================
///
/// Áèáëèîòåêà ôóíêöèé áûñòðîé ñîðòèðîâêè
///
///
/// Áàçîâûé êîä áûë âçÿò ñ ñàéòà algolist.manual.ru
///
/// Ñêîìïîíîâàë À. Áîãîìàç aka Albom (albom85@yandex.ru)
///===========================================
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà int (4 áàéòà)
///===========================================
void qsi(int *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà short int (2 áàéòà)
///===========================================
void qss(short *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà char (1 áàéò)
///===========================================
void qsc(char *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned int (4 áàéòà)
///===========================================
void qsui(unsigned *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned short int (2 áàéòà)
///===========================================
void qsus(unsigned short *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà unsigned char (1 áàéò)
///===========================================
void qsuc(unsigned char *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà float (4 áàéòà)
///===========================================
void qsf(float *a, int n);
///===========================================
/// Ñîðòèðîâêà äëÿ ïåðåìåííûõ òèïà double (8 áàéò)
///===========================================
void qsd(double *a, int n);

View File

@ -0,0 +1,9 @@
Áèáëèîòåêà ôóíêöèé áûñòðîé ñîðòèðîâêè qs version 0.1
Áàçîâûé êîä áûë âçÿò ñ ñàéòà algolist.manual.ru
Ñêîìïîíîâàë À. Áîãîìàç aka Albom (albom85@yandex.ru)

Binary file not shown.