├── .gitignore ├── LICENSE ├── README.md ├── cmake-build-debug └── CMakeFiles │ └── clion-log.txt ├── examples_pc ├── README ├── contents ├── examples │ ├── bistree │ │ ├── bistree.mak │ │ └── ex-1.c │ ├── bit │ │ ├── bit.mak │ │ └── ex-1.c │ ├── bitree │ │ ├── bitree.mak │ │ └── ex-1.c │ ├── checker │ ├── chtbl │ │ ├── chtbl.mak │ │ └── ex-1.c │ ├── clist │ │ ├── clist.mak │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ └── page.mak │ ├── compress │ │ ├── compress.mak │ │ ├── ex-1.c │ │ └── sample.txt │ ├── dlist │ │ ├── dlist.mak │ │ └── ex-1.c │ ├── encrypt │ │ ├── cbc.mak │ │ ├── encrypt.mak │ │ ├── ex-1.c │ │ └── ex-2.c │ ├── geometry │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ ├── geodist.mak │ │ └── geometry.mak │ ├── graph │ │ ├── bfs.mak │ │ ├── dfs.mak │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ ├── ex-3.c │ │ └── graph.mak │ ├── graphalg │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ ├── graphalg.mak │ │ └── route.mak │ ├── heap │ │ ├── ex-1.c │ │ └── heap.mak │ ├── list │ │ ├── ex-1.c │ │ └── list.mak │ ├── nummeths │ │ ├── ex-1.c │ │ └── nummeths.mak │ ├── ohtbl │ │ ├── ex-1.c │ │ └── ohtbl.mak │ ├── pqueue │ │ ├── ex-1.c │ │ └── pqueue.mak │ ├── queue │ │ ├── ex-1.c │ │ └── queue.mak │ ├── recurse │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ ├── fact.mak │ │ └── factor.mak │ ├── search │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ ├── search.mak │ │ └── spell.mak │ ├── set │ │ ├── cover.mak │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ └── set.mak │ ├── sort │ │ ├── directls.mak │ │ ├── ex-1.c │ │ ├── ex-2.c │ │ └── sort.mak │ └── stack │ │ ├── ex-1.c │ │ └── stack.mak ├── include │ ├── bfs.h │ ├── bistree.h │ ├── bit.h │ ├── bitree.h │ ├── cbc.h │ ├── chtbl.h │ ├── clist.h │ ├── compress.h │ ├── cover.h │ ├── dfs.h │ ├── directls.h │ ├── dlist.h │ ├── encrypt.h │ ├── event.h │ ├── events.h │ ├── fact.h │ ├── factor.h │ ├── facttail.h │ ├── frames.h │ ├── geodist.h │ ├── geometry.h │ ├── graph.h │ ├── graphalg.h │ ├── hashpjw.h │ ├── heap.h │ ├── lex.h │ ├── list.h │ ├── nummeths.h │ ├── ohtbl.h │ ├── page.h │ ├── parcel.h │ ├── parcels.h │ ├── pqueue.h │ ├── queue.h │ ├── route.h │ ├── search.h │ ├── set.h │ ├── sort.h │ ├── spell.h │ ├── stack.h │ ├── symbol.h │ ├── transfer.h │ └── traverse.h └── source │ ├── arclen.c │ ├── bfs.c │ ├── bisearch.c │ ├── bistree.c │ ├── bit.c │ ├── bitree.c │ ├── cbc.c │ ├── chtbl.c │ ├── clist.c │ ├── cover.c │ ├── ctsort.c │ ├── cvxhull.c │ ├── des.c │ ├── dfs.c │ ├── directls.c │ ├── dlist.c │ ├── events.c │ ├── fact.c │ ├── factor.c │ ├── facttail.c │ ├── frames.c │ ├── geodist.c │ ├── graph.c │ ├── hashpjw.c │ ├── heap.c │ ├── huffman.c │ ├── interpol.c │ ├── issort.c │ ├── lex.c │ ├── lint.c │ ├── list.c │ ├── lsqe.c │ ├── lz77.c │ ├── mgsort.c │ ├── mst.c │ ├── ohtbl.c │ ├── page.c │ ├── parcels.c │ ├── qksort.c │ ├── queue.c │ ├── root.c │ ├── route.c │ ├── rsa.c │ ├── rxsort.c │ ├── set.c │ ├── shortest.c │ ├── spell.c │ ├── stack.c │ ├── transfer.c │ ├── traverse.c │ └── tsp.c └── examples_unix ├── README ├── contents ├── examples ├── bistree │ ├── bistree.mak │ └── ex-1.c ├── bit │ ├── bit.mak │ └── ex-1.c ├── bitree │ ├── bitree.mak │ └── ex-1.c ├── checker ├── chtbl │ ├── chtbl.mak │ └── ex-1.c ├── clist │ ├── clist.mak │ ├── ex-1.c │ ├── ex-2.c │ └── page.mak ├── compress │ ├── compress.mak │ ├── ex-1.c │ └── sample.txt ├── dlist │ ├── dlist.mak │ └── ex-1.c ├── encrypt │ ├── cbc.mak │ ├── encrypt.mak │ ├── ex-1.c │ └── ex-2.c ├── geometry │ ├── ex-1.c │ ├── ex-2.c │ ├── geodist.mak │ └── geometry.mak ├── graph │ ├── bfs.mak │ ├── dfs.mak │ ├── ex-1.c │ ├── ex-2.c │ ├── ex-3.c │ └── graph.mak ├── graphalg │ ├── ex-1.c │ ├── ex-2.c │ ├── graphalg.mak │ └── route.mak ├── heap │ ├── ex-1.c │ └── heap.mak ├── list │ ├── ex-1.c │ └── list.mak ├── nummeths │ ├── ex-1.c │ └── nummeths.mak ├── ohtbl │ ├── ex-1.c │ └── ohtbl.mak ├── pqueue │ ├── ex-1.c │ └── pqueue.mak ├── queue │ ├── ex-1.c │ └── queue.mak ├── recurse │ ├── ex-1.c │ ├── ex-2.c │ ├── fact.mak │ └── factor.mak ├── search │ ├── ex-1.c │ ├── ex-2.c │ ├── search.mak │ └── spell.mak ├── set │ ├── cover.mak │ ├── ex-1.c │ ├── ex-2.c │ └── set.mak ├── sort │ ├── directls.mak │ ├── ex-1.c │ ├── ex-2.c │ └── sort.mak └── stack │ ├── ex-1.c │ └── stack.mak ├── include ├── bfs.h ├── bistree.h ├── bit.h ├── bitree.h ├── cbc.h ├── chtbl.h ├── clist.h ├── compress.h ├── cover.h ├── dfs.h ├── directls.h ├── dlist.h ├── encrypt.h ├── event.h ├── events.h ├── fact.h ├── factor.h ├── facttail.h ├── frames.h ├── geodist.h ├── geometry.h ├── graph.h ├── graphalg.h ├── hashpjw.h ├── heap.h ├── lex.h ├── list.h ├── nummeths.h ├── ohtbl.h ├── page.h ├── parcel.h ├── parcels.h ├── pqueue.h ├── queue.h ├── route.h ├── search.h ├── set.h ├── sort.h ├── spell.h ├── stack.h ├── symbol.h ├── transfer.h └── traverse.h └── source ├── arclen.c ├── bfs.c ├── bisearch.c ├── bistree.c ├── bit.c ├── bitree.c ├── cbc.c ├── chtbl.c ├── clist.c ├── cover.c ├── ctsort.c ├── cvxhull.c ├── des.c ├── dfs.c ├── directls.c ├── dlist.c ├── events.c ├── fact.c ├── factor.c ├── facttail.c ├── frames.c ├── geodist.c ├── graph.c ├── hashpjw.c ├── heap.c ├── huffman.c ├── interpol.c ├── issort.c ├── lex.c ├── lint.c ├── list.c ├── lsqe.c ├── lz77.c ├── mgsort.c ├── mst.c ├── ohtbl.c ├── page.c ├── parcels.c ├── qksort.c ├── queue.c ├── root.c ├── route.c ├── rsa.c ├── rxsort.c ├── set.c ├── shortest.c ├── spell.c ├── stack.c ├── transfer.c ├── traverse.c └── tsp.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Joseph Michael Casey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mastering-Algorithms-with-C 2 | This repository contains example files organized by chapters in Mastering Algorithms with C, by Kyle Loudon. 3 | 4 | Being one of my favorite books, it felt necessary to ensure there was a repository for this on Github. Originally ported over from the [Gitlab repository](https://resources.oreilly.com/examples/9781565924536/tree/master) 5 | 6 | If you find this code interesting, you can check out the book associated with this code [here](https://www.amazon.com/Mastering-Algorithms-Techniques-Sorting-Encryption-ebook/dp/B0043EWV5Q) -------------------------------------------------------------------------------- /cmake-build-debug/CMakeFiles/clion-log.txt: -------------------------------------------------------------------------------- 1 | CMakeLists.txt not found in C:\Users\Joseph\ClionProjects\Mastering-Algorithms-with-C 2 | -------------------------------------------------------------------------------- /examples_pc/contents: -------------------------------------------------------------------------------- 1 | source: 2 | arclen.c bfs.c bisearch.c bistree.c bit.c bitree.c 3 | cbc.c chtbl.c clist.c cover.c ctsort.c cvxhull.c 4 | des.c dfs.c directls.c dlist.c events.c fact.c 5 | factor.c facttail.c frames.c geodist.c graph.c hashpjw.c 6 | heap.c huffman.c interpol.c issort.c lex.c lint.c 7 | list.c lsqe.c lz77.c mgsort.c mst.c ohtbl.c 8 | page.c parcels.c qksort.c queue.c root.c route.c 9 | rsa.c rxsort.c set.c shortest.c spell.c stack.c 10 | transfer.c traverse.c tsp.c 11 | 12 | include: 13 | bfs.h bistree.h bit.h bitree.h cbc.h chtbl.h 14 | clist.h compress.h cover.h dfs.h directls.h dlist.h 15 | encrypt.h event.h events.h fact.h factor.h facttail.h 16 | frames.h geodist.h geometry.h graph.h graphalg.h hashpjw.h 17 | heap.h lex.h list.h nummeths.h ohtbl.h page.h 18 | parcel.h parcels.h pqueue.h queue.h route.h search.h 19 | set.h sort.h spell.h stack.h symbol.h transfer.h 20 | traverse.h 21 | 22 | examples: 23 | bistree bit bitree chtbl clist compress 24 | dlist encrypt geometry graph graphalg heap 25 | list nummeths ohtbl pqueue queue recurse 26 | search set sort stack 27 | 28 | examples/bistree: 29 | bistree.mak ex-1.c 30 | 31 | examples/bit: 32 | bit.mak ex-1.c 33 | 34 | examples/bitree: 35 | bitree.mak ex-1.c 36 | 37 | examples/chtbl: 38 | chtbl.mak ex-1.c 39 | 40 | examples/clist: 41 | clist.mak page.mak ex-1.c ex-2.c 42 | 43 | examples/compress: 44 | compress.mak ex-1.c sample.txt 45 | 46 | examples/dlist: 47 | dlist.mak ex-1.c 48 | 49 | examples/encrypt: 50 | encrypt.mak cbc.mak ex-1.c ex-2.c 51 | 52 | examples/geometry: 53 | geometry.mak geodist.mak ex-1.c ex-2.c 54 | 55 | examples/graph: 56 | graph.mak bfs.mak dfs.mak ex-1.c ex-2.c ex-3.c 57 | 58 | examples/graphalg: 59 | graphalg.mak route.mak ex-1.c ex-2.c 60 | 61 | examples/heap: 62 | heap.mak ex-1.c 63 | 64 | examples/list: 65 | list.mak ex-1.c 66 | 67 | examples/nummeths: 68 | nummeths.mak ex-1.c 69 | 70 | examples/ohtbl: 71 | ohtbl.mak ex-1.c 72 | 73 | examples/pqueue: 74 | pqueue.mak ex-1.c 75 | 76 | examples/queue: 77 | ex-1.c 78 | queue.mak 79 | 80 | examples/recurse: 81 | fact.mak factor.mak ex-1.c ex-2.c 82 | 83 | examples/search: 84 | search.mak spell.mak ex-1.c ex-2.c 85 | 86 | examples/set: 87 | set.mak cover.mak ex-1.c ex-2.c 88 | 89 | examples/sort: 90 | directls.mak sort.mak ex-1.c ex-2.c 91 | 92 | examples/stack: 93 | stack.mak ex-1.c 94 | -------------------------------------------------------------------------------- /examples_pc/examples/checker: -------------------------------------------------------------------------------- 1 | for a in `ls -1` 2 | do 3 | echo $a: 4 | if [ -d $a ] 5 | then 6 | cd $a 7 | grep -n including *-*.c 8 | cd .. 9 | fi 10 | done 11 | -------------------------------------------------------------------------------- /examples_pc/examples/encrypt/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates using DES in CBC mode (see Chapter 15). * 7 | * * 8 | *****************************************************************************/ 9 | 10 | #include 11 | #include 12 | 13 | #include "cbc.h" 14 | 15 | /***************************************************************************** 16 | * * 17 | * Define the size of text to encipher. * 18 | * * 19 | *****************************************************************************/ 20 | 21 | #define TXTSIZ 1000 22 | 23 | /***************************************************************************** 24 | * * 25 | * --------------------------------- main --------------------------------- * 26 | * * 27 | *****************************************************************************/ 28 | 29 | int main(int argc, char **argv) { 30 | 31 | unsigned char destmp[TXTSIZ], 32 | desptx[TXTSIZ], 33 | desctx[TXTSIZ], 34 | deskey[8]; 35 | 36 | int i; 37 | 38 | /***************************************************************************** 39 | * * 40 | * Encipher some data using DES in CBC mode. * 41 | * * 42 | *****************************************************************************/ 43 | 44 | fprintf(stdout, "Enciphering with DES in CBC mode\n"); 45 | 46 | deskey[0] = 0x01; 47 | deskey[1] = 0xf1; 48 | deskey[2] = 0x23; 49 | deskey[3] = 0x4a; 50 | deskey[4] = 0x1f; 51 | deskey[5] = 0x22; 52 | deskey[6] = 0x00; 53 | deskey[7] = 0xee; 54 | 55 | for (i = 0; i < TXTSIZ; i++) 56 | destmp[i] = (i * 23) % 256; 57 | 58 | cbc_encipher(destmp, desctx, deskey, TXTSIZ); 59 | cbc_decipher(desctx, desptx, deskey, TXTSIZ); 60 | 61 | fprintf(stdout, "deskey: %02x %02x %02x %02x %02x %02x %02x %02x\n", 62 | deskey[0], deskey[1], deskey[2], deskey[3], deskey[4], deskey[5], 63 | deskey[6], deskey[7]); 64 | 65 | for (i = 0; i < TXTSIZ; i++) { 66 | 67 | if (destmp[i] == desptx[i]) 68 | fprintf(stdout, "destmp[%03d]=%02x, desctx[%03d]=%02x, desptx[%03d]=" 69 | "%02x (OK)\n", i, destmp[i], i, desctx[i], i, desptx[i]); 70 | else 71 | fprintf(stdout, "destmp[%03d]=%02x, desctx[%03d]=%02x, desptx[%03d]=" 72 | "%02x (ERROR)\n", i, destmp[i], i, desctx[i], i, desptx[i]); 73 | 74 | } 75 | 76 | return 0; 77 | 78 | } 79 | -------------------------------------------------------------------------------- /examples_pc/examples/geometry/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing great-circle distances (see Chapter * 7 | * 17). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "geodist.h" 14 | 15 | /***************************************************************************** 16 | * * 17 | * --------------------------------- main --------------------------------- * 18 | * * 19 | *****************************************************************************/ 20 | 21 | int main(int argc, char **argv) { 22 | 23 | double lat1, 24 | lon1, 25 | lat2, 26 | lon2, 27 | distance; 28 | 29 | /***************************************************************************** 30 | * * 31 | * Test computing distances between points on Earth. * 32 | * * 33 | *****************************************************************************/ 34 | 35 | fprintf(stdout, "Computing distances between points on Earth\n"); 36 | 37 | /* SFO (San Francisco) */ 38 | lat1 = 37.62; 39 | lon1 = 122.38; 40 | 41 | /* LAX (Los Angeles) */ 42 | lat2 = 33.94; 43 | lon2 = 118.41; 44 | 45 | if (geodist(lat1, lon1, lat2, lon2, &distance) != 0) 46 | return 1; 47 | 48 | fprintf(stdout, "SFO: (%+07.2lf,%+07.2lf)\n", lat1, lon1); 49 | fprintf(stdout, "LAX: (%+07.2lf,%+07.2lf)\n", lat2, lon2); 50 | fprintf(stdout, "distance=%d\n", (int)distance); 51 | 52 | /* CDG (Paris) */ 53 | lat1 = 49.01; 54 | lon1 = -2.55; 55 | 56 | /* PER (Perth) */ 57 | lat2 = -31.94; 58 | lon2 = -115.97; 59 | 60 | if (geodist(lat1, lon1, lat2, lon2, &distance) != 0) 61 | return 1; 62 | 63 | fprintf(stdout, "CDG: (%+07.2lf,%+07.2lf)\n", lat1, lon1); 64 | fprintf(stdout, "PER: (%+07.2lf,%+07.2lf)\n", lat2, lon2); 65 | fprintf(stdout, "distance=%d\n", (int)distance); 66 | 67 | return 0; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /examples_pc/examples/recurse/ex-1.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-1.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing a factorial using recursion and tail * 7 | * recursion (see Chapter 3). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "fact.h" 14 | #include "facttail.h" 15 | 16 | /***************************************************************************** 17 | * * 18 | * --------------------------------- main --------------------------------- * 19 | * * 20 | *****************************************************************************/ 21 | 22 | int main(int argc, char **argv) { 23 | 24 | int n; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Computer the factorials of several numbers. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | for (n = 0; n <= 13; n++) { 33 | 34 | fprintf(stdout, "%2d! recursive: %-10d ", n, fact(n)); 35 | fprintf(stdout, "tail recursive: %-10d\n", facttail(n, 1)); 36 | 37 | } 38 | 39 | return 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /examples_pc/examples/recurse/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing the prime factors of numbers using * 7 | * recursion (see Chapter 3). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "factor.h" 14 | 15 | /***************************************************************************** 16 | * * 17 | * --------------------------------- main --------------------------------- * 18 | * * 19 | *****************************************************************************/ 20 | 21 | int main(int argc, char **argv) { 22 | 23 | int n; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Compute the prime factors of several numbers. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | for (n = 1; n <= 10000; n++) { 32 | 33 | fprintf(stdout, "Factoring %d\n", n); 34 | factor(n, n, 2); 35 | 36 | } 37 | 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /examples_pc/examples/sort/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates sorting a directory listing (see Chapter 12). * 7 | * * 8 | *****************************************************************************/ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "directls.h" 15 | 16 | /***************************************************************************** 17 | * * 18 | * --------------------------------- main --------------------------------- * 19 | * * 20 | *****************************************************************************/ 21 | 22 | int main(int argc, char **argv) { 23 | 24 | Directory *dir; 25 | 26 | char buffer[MAXPATHLEN]; 27 | 28 | int count, 29 | i; 30 | 31 | /***************************************************************************** 32 | * * 33 | * Get the directory listing. * 34 | * * 35 | *****************************************************************************/ 36 | 37 | if (argc > 1) { 38 | 39 | if ((count = directls(argv[1], &dir)) < 0) { 40 | 41 | fprintf(stdout, "Could not read directory\n"); 42 | exit(1); 43 | 44 | } 45 | 46 | } 47 | 48 | else { 49 | 50 | if ((count = directls(getcwd(buffer, MAXPATHLEN), &dir)) < 0) { 51 | 52 | fprintf(stdout, "Could not read directory\n"); 53 | exit(1); 54 | 55 | } 56 | 57 | } 58 | 59 | /***************************************************************************** 60 | * * 61 | * Display the directory listing. * 62 | * * 63 | *****************************************************************************/ 64 | 65 | for (i = 0; i < count; i++) 66 | fprintf(stdout, "%s\n", dir[i].name); 67 | 68 | fprintf(stdout, "%d found\n", count); 69 | free(dir); 70 | 71 | return 0; 72 | 73 | } 74 | -------------------------------------------------------------------------------- /examples_pc/include/bfs.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- bfs.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BFS_H 8 | #define BFS_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in breadth-first search. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct BfsVertex_ { 20 | 21 | void *data; 22 | 23 | VertexColor color; 24 | int hops; 25 | 26 | } BfsVertex; 27 | 28 | /***************************************************************************** 29 | * * 30 | * --------------------------- Public Interface --------------------------- * 31 | * * 32 | *****************************************************************************/ 33 | 34 | int bfs(Graph *graph, BfsVertex *start, List *hops); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /examples_pc/include/bistree.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- bistree.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BISTREE_H 8 | #define BISTREE_H 9 | 10 | #include "bitree.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define balance factors for AVL trees. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | #define AVL_LFT_HEAVY 1 19 | #define AVL_BALANCED 0 20 | #define AVL_RGT_HEAVY -1 21 | 22 | /***************************************************************************** 23 | * * 24 | * Define a structure for nodes in AVL trees. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | typedef struct AvlNode_ { 29 | 30 | void *data; 31 | int hidden; 32 | int factor; 33 | 34 | } AvlNode; 35 | 36 | /***************************************************************************** 37 | * * 38 | * Implement binary search trees as binary trees. * 39 | * * 40 | *****************************************************************************/ 41 | 42 | typedef BiTree BisTree; 43 | 44 | /***************************************************************************** 45 | * * 46 | * --------------------------- Public Interface --------------------------- * 47 | * * 48 | *****************************************************************************/ 49 | 50 | void bistree_init(BisTree *tree, int (*compare)(const void *key1, const void 51 | *key2), void (*destroy)(void *data)); 52 | 53 | void bistree_destroy(BisTree *tree); 54 | 55 | int bistree_insert(BisTree *tree, const void *data); 56 | 57 | int bistree_remove(BisTree *tree, const void *data); 58 | 59 | int bistree_lookup(BisTree *tree, void **data); 60 | 61 | #define bistree_size(tree) ((tree)->size) 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /examples_pc/include/bit.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- bit.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BIT_H 8 | #define BIT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int bit_get(const unsigned char *bits, int pos); 17 | 18 | void bit_set(unsigned char *bits, int pos, int state); 19 | 20 | void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned 21 | char *bitsx, int size); 22 | 23 | void bit_rot_left(unsigned char *bits, int size, int count); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /examples_pc/include/bitree.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- bitree.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BITREE_H 8 | #define BITREE_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for binary tree nodes. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct BiTreeNode_ { 19 | 20 | void *data; 21 | struct BiTreeNode_ *left; 22 | struct BiTreeNode_ *right; 23 | 24 | } BiTreeNode; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Define a structure for binary trees. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | typedef struct BiTree_ { 33 | 34 | int size; 35 | 36 | int (*compare)(const void *key1, const void *key2); 37 | void (*destroy)(void *data); 38 | 39 | BiTreeNode *root; 40 | 41 | } BiTree; 42 | 43 | /***************************************************************************** 44 | * * 45 | * --------------------------- Public Interface --------------------------- * 46 | * * 47 | *****************************************************************************/ 48 | 49 | void bitree_init(BiTree *tree, void (*destroy)(void *data)); 50 | 51 | void bitree_destroy(BiTree *tree); 52 | 53 | int bitree_ins_left(BiTree *tree, BiTreeNode *node, const void *data); 54 | 55 | int bitree_ins_right(BiTree *tree, BiTreeNode *node, const void *data); 56 | 57 | void bitree_rem_left(BiTree *tree, BiTreeNode *node); 58 | 59 | void bitree_rem_right(BiTree *tree, BiTreeNode *node); 60 | 61 | int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data); 62 | 63 | #define bitree_size(tree) ((tree)->size) 64 | 65 | #define bitree_root(tree) ((tree)->root) 66 | 67 | #define bitree_is_eob(node) ((node) == NULL) 68 | 69 | #define bitree_is_leaf(node) ((node)->left == NULL && (node)->right == NULL) 70 | 71 | #define bitree_data(node) ((node)->data) 72 | 73 | #define bitree_left(node) ((node)->left) 74 | 75 | #define bitree_right(node) ((node)->right) 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /examples_pc/include/cbc.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- cbc.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CBC_H 8 | #define CBC_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | void cbc_encipher(const unsigned char *plaintext, unsigned char *ciphertext, 17 | const unsigned char *key, int size); 18 | 19 | void cbc_decipher(const unsigned char *ciphertext, unsigned char *plaintext, 20 | const unsigned char *key, int size); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_pc/include/chtbl.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- chtbl.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CHTBL_H 8 | #define CHTBL_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Define a structure for chained hash tables. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef struct CHTbl_ { 21 | 22 | int buckets; 23 | 24 | int (*h)(const void *key); 25 | int (*match)(const void *key1, const void *key2); 26 | void (*destroy)(void *data); 27 | 28 | int size; 29 | List *table; 30 | 31 | } CHTbl; 32 | 33 | /***************************************************************************** 34 | * * 35 | * --------------------------- Public Interface --------------------------- * 36 | * * 37 | *****************************************************************************/ 38 | 39 | int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int 40 | (*match)(const void *key1, const void *key2), void (*destroy)(void *data)); 41 | 42 | void chtbl_destroy(CHTbl *htbl); 43 | 44 | int chtbl_insert(CHTbl *htbl, const void *data); 45 | 46 | int chtbl_remove(CHTbl *htbl, void **data); 47 | 48 | int chtbl_lookup(const CHTbl *htbl, void **data); 49 | 50 | #define chtbl_size(htbl) ((htbl)->size) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /examples_pc/include/clist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- clist.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CLIST_H 8 | #define CLIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for circular list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct CListElmt_ { 19 | 20 | void *data; 21 | struct CListElmt_ *next; 22 | 23 | } CListElmt; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Define a structure for circular lists. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | typedef struct CList_ { 32 | 33 | int size; 34 | 35 | int (*match)(const void *key1, const void *key2); 36 | void (*destroy)(void *data); 37 | 38 | CListElmt *head; 39 | 40 | } CList; 41 | 42 | /***************************************************************************** 43 | * * 44 | * --------------------------- Public Interface --------------------------- * 45 | * * 46 | *****************************************************************************/ 47 | 48 | void clist_init(CList *list, void (*destroy)(void *data)); 49 | 50 | void clist_destroy(CList *list); 51 | 52 | int clist_ins_next(CList *list, CListElmt *element, const void *data); 53 | 54 | int clist_rem_next(CList *list, CListElmt *element, void **data); 55 | 56 | #define clist_size(list) ((list)->size) 57 | 58 | #define clist_head(list) ((list)->head) 59 | 60 | #define clist_data(element) ((element)->data) 61 | 62 | #define clist_next(element) ((element)->next) 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /examples_pc/include/cover.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- cover.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef COVER_H 8 | #define COVER_H 9 | 10 | #include "set.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for subsets identified by a key. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct KSet_ { 19 | 20 | void *key; 21 | Set set; 22 | 23 | } KSet; 24 | 25 | /***************************************************************************** 26 | * * 27 | * --------------------------- Public Interface --------------------------- * 28 | * * 29 | *****************************************************************************/ 30 | 31 | int cover(Set *members, Set *subsets, Set *covering); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /examples_pc/include/dfs.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- dfs.h --------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DFS_H 8 | #define DFS_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in depth-first search. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct DfsVertex_ { 20 | 21 | void *data; 22 | 23 | VertexColor color; 24 | 25 | } DfsVertex; 26 | 27 | /***************************************************************************** 28 | * * 29 | * --------------------------- Public Interface --------------------------- * 30 | * * 31 | *****************************************************************************/ 32 | 33 | int dfs(Graph *graph, List *ordered); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /examples_pc/include/directls.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ directls.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DIRECTLS_H 8 | #define DIRECTLS_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for directory entries. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct Directory_ { 19 | 20 | char name[MAXNAMLEN + 1]; 21 | 22 | } Directory; 23 | 24 | /***************************************************************************** 25 | * * 26 | * --------------------------- Public Interface --------------------------- * 27 | * * 28 | *****************************************************************************/ 29 | 30 | int directls(const char *path, Directory **dir); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /examples_pc/include/dlist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- dlist.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DLIST_H 8 | #define DLIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for doubly-linked list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct DListElmt_ { 19 | 20 | void *data; 21 | struct DListElmt_ *prev; 22 | struct DListElmt_ *next; 23 | 24 | } DListElmt; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Define a structure for doubly-linked lists. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | typedef struct DList_ { 33 | 34 | int size; 35 | 36 | int (*match)(const void *key1, const void *key2); 37 | void (*destroy)(void *data); 38 | 39 | DListElmt *head; 40 | DListElmt *tail; 41 | 42 | } DList; 43 | 44 | /***************************************************************************** 45 | * * 46 | * --------------------------- Public Interface --------------------------- * 47 | * * 48 | *****************************************************************************/ 49 | 50 | void dlist_init(DList *list, void (*destroy)(void *data)); 51 | 52 | void dlist_destroy(DList *list); 53 | 54 | int dlist_ins_next(DList *list, DListElmt *element, const void *data); 55 | 56 | int dlist_ins_prev(DList *list, DListElmt *element, const void *data); 57 | 58 | int dlist_remove(DList *list, DListElmt *element, void **data); 59 | 60 | #define dlist_size(list) ((list)->size) 61 | 62 | #define dlist_head(list) ((list)->head) 63 | 64 | #define dlist_tail(list) ((list)->tail) 65 | 66 | #define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0) 67 | 68 | #define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0) 69 | 70 | #define dlist_data(element) ((element)->data) 71 | 72 | #define dlist_next(element) ((element)->next) 73 | 74 | #define dlist_prev(element) ((element)->prev) 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /examples_pc/include/encrypt.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- encrypt.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef ENCRYPT_H 8 | #define ENCRYPT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * In a secure implementation, Huge should be at least 400 decimal digits, * 13 | * instead of the 10 below (ULONG_MAX = 4294967295). * 14 | * * 15 | *****************************************************************************/ 16 | 17 | typedef unsigned long Huge; 18 | 19 | /***************************************************************************** 20 | * * 21 | * Define a structure for RSA public keys. * 22 | * * 23 | *****************************************************************************/ 24 | 25 | typedef struct RsaPubKey_ { 26 | 27 | Huge e; 28 | Huge n; 29 | 30 | } RsaPubKey; 31 | 32 | /***************************************************************************** 33 | * * 34 | * Define a structure for RSA private keys. * 35 | * * 36 | *****************************************************************************/ 37 | 38 | typedef struct RsaPriKey_ { 39 | 40 | Huge d; 41 | Huge n; 42 | 43 | } RsaPriKey; 44 | 45 | /***************************************************************************** 46 | * * 47 | * --------------------------- Public Interface --------------------------- * 48 | * * 49 | *****************************************************************************/ 50 | 51 | void des_encipher(const unsigned char *plaintext, unsigned char *ciphertext, 52 | const unsigned char *key); 53 | 54 | void des_decipher(const unsigned char *ciphertext, unsigned char *plaintext, 55 | const unsigned char *key); 56 | 57 | void rsa_encipher(Huge plaintext, Huge *ciphertext, RsaPubKey pubkey); 58 | 59 | void rsa_decipher(Huge ciphertext, Huge *plaintext, RsaPriKey prikey); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /examples_pc/include/event.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- event.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef EVENT_H 8 | #define EVENT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define an event structure for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Event_ { 17 | 18 | int type; 19 | 20 | } Event; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_pc/include/events.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- events.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef EVENTS_H 8 | #define EVENTS_H 9 | 10 | #include "event.h" 11 | #include "queue.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int receive_event(Queue *events, const Event *event); 20 | 21 | int process_event(Queue *events, int (*dispatch)(Event *event)); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples_pc/include/fact.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- fact.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACT_H 8 | #define FACT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int fact(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_pc/include/factor.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- factor.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACTOR_H 8 | #define FACTOR_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | void factor(int number, int n, int j); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_pc/include/facttail.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ facttail.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACTTAIL_H 8 | #define FACTTAIL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int facttail(int n, int a); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_pc/include/frames.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- frames.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FRAMES_H 8 | #define FRAMES_H 9 | 10 | #include "list.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * --------------------------- Public Interface --------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int alloc_frame(List *frames); 19 | 20 | int free_frame(List *frames, int frame_number); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_pc/include/geodist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- geodist.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef GEODIST_H 8 | #define GEODIST_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define the radius of the earth in nautical miles. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define EARTH_RADIUS 3440.065 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int geodist(double lat1, double lon1, double lat2, double lon2, double *d); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_pc/include/graphalg.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ graphalg.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef GRAPHALG_H 8 | #define GRAPHALG_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in minimum spanning trees. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct MstVertex_ { 20 | 21 | void *data; 22 | double weight; 23 | 24 | VertexColor color; 25 | double key; 26 | 27 | struct MstVertex_ *parent; 28 | 29 | } MstVertex; 30 | 31 | /***************************************************************************** 32 | * * 33 | * Define a structure for vertices in shortest-path problems. * 34 | * * 35 | *****************************************************************************/ 36 | 37 | typedef struct PathVertex_ { 38 | 39 | void *data; 40 | double weight; 41 | 42 | VertexColor color; 43 | double d; 44 | 45 | struct PathVertex_ *parent; 46 | 47 | } PathVertex; 48 | 49 | /***************************************************************************** 50 | * * 51 | * Define a structure for vertices in traveling-salesman problems. * 52 | * * 53 | *****************************************************************************/ 54 | 55 | typedef struct TspVertex_ { 56 | 57 | void *data; 58 | 59 | double x, 60 | y; 61 | 62 | VertexColor color; 63 | 64 | } TspVertex; 65 | 66 | /***************************************************************************** 67 | * * 68 | * --------------------------- Public Interface --------------------------- * 69 | * * 70 | *****************************************************************************/ 71 | 72 | int mst(Graph *graph, const MstVertex *start, List *span, int (*match)(const 73 | void *key1, const void *key2)); 74 | 75 | int shortest(Graph *graph, const PathVertex *start, List *paths, int (*match) 76 | (const void *key1, const void *key2)); 77 | 78 | int tsp(List *vertices, const TspVertex *start, List *tour, int (*match) 79 | (const void *key1, const void *key2)); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /examples_pc/include/hashpjw.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- hashpjw.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef HASHPJW_H 8 | #define HASHPJW_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a table size for demonstration purposes only. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define PRIME_TBLSIZ 1699 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int hashpjw(const void *key); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_pc/include/heap.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- heap.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef HEAP_H 8 | #define HEAP_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a structure for heaps. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Heap_ { 17 | 18 | int size; 19 | 20 | int (*compare)(const void *key1, const void *key2); 21 | void (*destroy)(void *data); 22 | 23 | void **tree; 24 | 25 | } Heap; 26 | 27 | /***************************************************************************** 28 | * * 29 | * --------------------------- Public Interface --------------------------- * 30 | * * 31 | *****************************************************************************/ 32 | 33 | void heap_init(Heap *heap, int (*compare)(const void *key1, const void *key2), 34 | void (*destroy)(void *data)); 35 | 36 | void heap_destroy(Heap *heap); 37 | 38 | int heap_insert(Heap *heap, const void *data); 39 | 40 | int heap_extract(Heap *heap, void **data); 41 | 42 | #define heap_size(heap) ((heap)->size) 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /examples_pc/include/lex.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- lex.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef LEX_H 8 | #define LEX_H 9 | 10 | #include "chtbl.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define the token types recognized by the lexical analyzer. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef enum Token_ {lexit, error, digit, other} Token; 19 | 20 | /***************************************************************************** 21 | * * 22 | * --------------------------- Public Interface --------------------------- * 23 | * * 24 | *****************************************************************************/ 25 | 26 | Token lex(const char *istream, CHTbl *symtbl); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /examples_pc/include/list.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- list.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef LIST_H 8 | #define LIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for linked list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct ListElmt_ { 19 | 20 | void *data; 21 | struct ListElmt_ *next; 22 | 23 | } ListElmt; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Define a structure for linked lists. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | typedef struct List_ { 32 | 33 | int size; 34 | 35 | int (*match)(const void *key1, const void *key2); 36 | void (*destroy)(void *data); 37 | 38 | ListElmt *head; 39 | ListElmt *tail; 40 | 41 | } List; 42 | 43 | /***************************************************************************** 44 | * * 45 | * --------------------------- Public Interface --------------------------- * 46 | * * 47 | *****************************************************************************/ 48 | 49 | void list_init(List *list, void (*destroy)(void *data)); 50 | 51 | void list_destroy(List *list); 52 | 53 | int list_ins_next(List *list, ListElmt *element, const void *data); 54 | 55 | int list_rem_next(List *list, ListElmt *element, void **data); 56 | 57 | #define list_size(list) ((list)->size) 58 | 59 | #define list_head(list) ((list)->head) 60 | 61 | #define list_tail(list) ((list)->tail) 62 | 63 | #define list_is_head(list, element) ((element) == (list)->head ? 1 : 0) 64 | 65 | #define list_is_tail(element) ((element)->next == NULL ? 1 : 0) 66 | 67 | #define list_data(element) ((element)->data) 68 | 69 | #define list_next(element) ((element)->next) 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /examples_pc/include/nummeths.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ nummeths.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef NUMMETHS_H 8 | #define NUMMETHS_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int interpol(const double *x, const double *fx, int n, double *z, double *pz, 17 | int m); 18 | 19 | void lsqe(const double *x, const double *y, int n, double *b1, double *b0); 20 | 21 | int root(double (*f)(double x), double (*g)(double x), double *x, int *n, 22 | double delta); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /examples_pc/include/ohtbl.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- ohtbl.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef OHTBL_H 8 | #define OHTBL_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for open-addressed hash tables. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct OHTbl_ { 19 | 20 | int positions; 21 | void *vacated; 22 | 23 | int (*h1)(const void *key); 24 | int (*h2)(const void *key); 25 | int (*match)(const void *key1, const void *key2); 26 | void (*destroy)(void *data); 27 | 28 | int size; 29 | void **table; 30 | 31 | } OHTbl; 32 | 33 | /***************************************************************************** 34 | * * 35 | * --------------------------- Public Interface --------------------------- * 36 | * * 37 | *****************************************************************************/ 38 | 39 | int ohtbl_init(OHTbl *htbl, int positions, int (*h1)(const void *key), int 40 | (*h2)(const void *key), int (*match)(const void *key1, const void *key2), 41 | void (*destroy)(void *data)); 42 | 43 | void ohtbl_destroy(OHTbl *htbl); 44 | 45 | int ohtbl_insert(OHTbl *htbl, const void *data); 46 | 47 | int ohtbl_remove(OHTbl *htbl, void **data); 48 | 49 | int ohtbl_lookup(const OHTbl *htbl, void **data); 50 | 51 | #define ohtbl_size(htbl) ((htbl)->size) 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /examples_pc/include/page.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- page.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PAGE_H 8 | #define PAGE_H 9 | 10 | #include "clist.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for information about pages. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct Page_ { 19 | 20 | int number; 21 | int reference; 22 | 23 | } Page; 24 | 25 | /***************************************************************************** 26 | * * 27 | * --------------------------- Public Interface --------------------------- * 28 | * * 29 | *****************************************************************************/ 30 | 31 | int replace_page(CListElmt **current); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /examples_pc/include/parcel.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- parcel.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PARCEL_H 8 | #define PARCEL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a parcel structure for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Parcel_ { 17 | 18 | int priority; 19 | 20 | } Parcel; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_pc/include/parcels.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- parcels.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PARCELS_H 8 | #define PARCELS_H 9 | 10 | #include "parcel.h" 11 | #include "pqueue.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int get_parcel(PQueue *parcels, Parcel *parcel); 20 | 21 | int put_parcel(PQueue *parcels, const Parcel *parcel); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples_pc/include/pqueue.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- pqueue.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PQUEUE_H 8 | #define PQUEUE_H 9 | 10 | #include "heap.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Implement priority queues as heaps. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef Heap PQueue; 19 | 20 | /***************************************************************************** 21 | * * 22 | * --------------------------- Public Interface --------------------------- * 23 | * * 24 | *****************************************************************************/ 25 | 26 | #define pqueue_init heap_init 27 | 28 | #define pqueue_destroy heap_destroy 29 | 30 | #define pqueue_insert heap_insert 31 | 32 | #define pqueue_extract heap_extract 33 | 34 | #define pqueue_peek(pqueue) ((pqueue)->tree == NULL ? NULL : (pqueue)->tree[0]) 35 | 36 | #define pqueue_size heap_size 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /examples_pc/include/queue.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- queue.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef QUEUE_H 8 | #define QUEUE_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement queues as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Queue; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | #define queue_init list_init 29 | 30 | #define queue_destroy list_destroy 31 | 32 | int queue_enqueue(Queue *queue, const void *data); 33 | 34 | int queue_dequeue(Queue *queue, void **data); 35 | 36 | #define queue_peek(queue) ((queue)->head == NULL ? NULL : (queue)->head->data) 37 | 38 | #define queue_size list_size 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples_pc/include/route.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- route.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef ROUTE_H 8 | #define ROUTE_H 9 | 10 | #include "graphalg.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int route(List *paths, PathVertex *destination, PathVertex **next, int 20 | (*match)(const void *key1, const void *key2)); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_pc/include/search.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- search.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SEARCH_H 8 | #define SEARCH_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int bisearch(void *sorted, const void *target, int size, int esize, int 17 | (*compare)(const void *key1, const void *key2)); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /examples_pc/include/set.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- set.h --------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SET_H 8 | #define SET_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement sets as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Set; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | void set_init(Set *set, int (*match)(const void *key1, const void *key2), 29 | void (*destroy)(void *data)); 30 | 31 | #define set_destroy list_destroy 32 | 33 | int set_insert(Set *set, const void *data); 34 | 35 | int set_remove(Set *set, void **data); 36 | 37 | int set_union(Set *setu, const Set *set1, const Set *set2); 38 | 39 | int set_intersection(Set *seti, const Set *set1, const Set *set2); 40 | 41 | int set_difference(Set *setd, const Set *set1, const Set *set2); 42 | 43 | int set_is_member(const Set *set, const void *data); 44 | 45 | int set_is_subset(const Set *set1, const Set *set2); 46 | 47 | int set_is_equal(const Set *set1, const Set *set2); 48 | 49 | #define set_size(set) ((set)->size) 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /examples_pc/include/sort.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- sort.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SORT_H 8 | #define SORT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int issort(void *data, int size, int esize, int (*compare)(const void *key1, 17 | const void *key2)); 18 | 19 | int qksort(void *data, int size, int esize, int i, int k, int (*compare) 20 | (const void *key1, const void *key2)); 21 | 22 | int mgsort(void *data, int size, int esize, int i, int k, int (*compare) 23 | (const void *key1, const void *key2)); 24 | 25 | int ctsort(int *data, int size, int k); 26 | 27 | int rxsort(int *data, int size, int p, int k); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples_pc/include/spell.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- spell.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SPELL_H 8 | #define SPELL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define the maximum size for words in the dictionary. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define SPELL_SIZE 31 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_pc/include/stack.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- stack.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement stacks as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Stack; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | #define stack_init list_init 29 | 30 | #define stack_destroy list_destroy 31 | 32 | int stack_push(Stack *stack, const void *data); 33 | 34 | int stack_pop(Stack *stack, void **data); 35 | 36 | #define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data) 37 | 38 | #define stack_size list_size 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples_pc/include/symbol.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- symbol.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SYMBOL_H 8 | #define SYMBOL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define next_token for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | static char *next_token(const char *istream) { 17 | 18 | return NULL; 19 | 20 | } 21 | 22 | /***************************************************************************** 23 | * * 24 | * Define a symbol structure for demonstration purposes. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | typedef struct Symbol_ { 29 | 30 | char *lexeme; 31 | Token token; 32 | 33 | } Symbol; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /examples_pc/include/transfer.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ transfer.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef TRANSFER_H 8 | #define TRANSFER_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int send_comp(int s, const unsigned char *data, int size, int flags); 17 | 18 | int recv_comp(int s, unsigned char **data, int *size, int flags); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /examples_pc/include/traverse.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ traverse.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef TRAVERSE_H 8 | #define TRAVERSE_H 9 | 10 | #include "bitree.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int preorder(const BiTreeNode *node, List *list); 20 | 21 | int inorder(const BiTreeNode *node, List *list); 22 | 23 | int postorder(const BiTreeNode *node, List *list); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /examples_pc/source/arclen.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- arclen.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "geometry.h" 10 | 11 | /***************************************************************************** 12 | * * 13 | * -------------------------------- arclen -------------------------------- * 14 | * * 15 | *****************************************************************************/ 16 | 17 | void arclen(SPoint p1, SPoint p2, double *length) { 18 | 19 | Point p1_rct, 20 | p2_rct; 21 | 22 | double alpha, 23 | dot; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Convert the spherical coordinates to rectilinear coordinates. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | p1_rct.x = p1.rho * sin(p1.phi) * cos(p1.theta); 32 | p1_rct.y = p1.rho * sin(p1.phi) * sin(p1.theta); 33 | p1_rct.z = p1.rho * cos(p1.phi); 34 | 35 | p2_rct.x = p2.rho * sin(p2.phi) * cos(p2.theta); 36 | p2_rct.y = p2.rho * sin(p2.phi) * sin(p2.theta); 37 | p2_rct.z = p2.rho * cos(p2.phi); 38 | 39 | /***************************************************************************** 40 | * * 41 | * Get the angle between the line segments from the origin to each point. * 42 | * * 43 | *****************************************************************************/ 44 | 45 | dot = (p1_rct.x * p2_rct.x) + (p1_rct.y * p2_rct.y) + (p1_rct.z * p2_rct.z); 46 | alpha = acos(dot / pow(p1.rho, 2.0)); 47 | 48 | /***************************************************************************** 49 | * * 50 | * Compute the length of the arc along the spherical surface. * 51 | * * 52 | *****************************************************************************/ 53 | 54 | *length = alpha * p1.rho; 55 | 56 | return; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /examples_pc/source/fact.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- fact.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "fact.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * --------------------------------- fact --------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int fact(int n) { 16 | 17 | /***************************************************************************** 18 | * * 19 | * Compute a factorial recursively. * 20 | * * 21 | *****************************************************************************/ 22 | 23 | if (n < 0) 24 | return 0; 25 | else if (n == 0) 26 | return 1; 27 | else if (n == 1) 28 | return 1; 29 | else 30 | return n * fact(n - 1); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /examples_pc/source/factor.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- factor.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | #include 9 | 10 | #include "factor.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * -------------------------------- factor -------------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | void factor(int number, int n, int j) { 19 | 20 | int i; 21 | 22 | /***************************************************************************** 23 | * * 24 | * 1 is neither prime nor composite. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | if (n == 1) { 29 | 30 | printf("1 is a unit\n"); 31 | return; 32 | 33 | } 34 | 35 | /***************************************************************************** 36 | * * 37 | * Determine the prime factors of n. * 38 | * * 39 | *****************************************************************************/ 40 | 41 | i = j; 42 | 43 | while (i <= (int)(sqrt((double)n))) { 44 | 45 | if (n % i == 0) { 46 | 47 | /*********************************************************************** 48 | * * 49 | * We have found a prime factor of n. Print it and factor n / i. * 50 | * * 51 | ***********************************************************************/ 52 | 53 | fprintf(stdout, "%d\n", i); 54 | factor(number, (int)(n / i), i); 55 | return; 56 | 57 | } 58 | 59 | else { 60 | 61 | i++; 62 | 63 | } 64 | 65 | } 66 | 67 | /***************************************************************************** 68 | * * 69 | * If this point is reached, n is prime. * 70 | * * 71 | *****************************************************************************/ 72 | 73 | if (n == number) 74 | printf("%d is prime\n", number); 75 | else 76 | printf("%d\n", n); 77 | 78 | return; 79 | 80 | } 81 | -------------------------------------------------------------------------------- /examples_pc/source/facttail.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ facttail.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "facttail.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * ------------------------------- facttail ------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int facttail(int n, int a) { 16 | 17 | /***************************************************************************** 18 | * * 19 | * Compute a factorial in a tail-recursive manner. * 20 | * * 21 | *****************************************************************************/ 22 | 23 | if (n < 0) 24 | return 0; 25 | else if (n == 0) 26 | return 1; 27 | else if (n == 1) 28 | return a; 29 | else 30 | return facttail(n - 1, n * a); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /examples_pc/source/geodist.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- geodist.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "geodist.h" 8 | #include "geometry.h" 9 | 10 | /***************************************************************************** 11 | * * 12 | * -------------------------------- geodist ------------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int geodist(double lat1, double lon1, double lat2, double lon2, double *d) { 17 | 18 | SPoint p1, 19 | p2; 20 | 21 | /***************************************************************************** 22 | * * 23 | * Validate the coordinates. * 24 | * * 25 | *****************************************************************************/ 26 | 27 | if (lat1 < -90.0 || lat1 > 90.0 || lat2 < -90.0 || lat2 > 90.0) 28 | return -1; 29 | 30 | if (lon1 < -180.0 || lon1 > 180.0 || lon2 < -180.0 || lon2 > 180.0) 31 | return -1; 32 | 33 | /***************************************************************************** 34 | * * 35 | * Convert each latitude and longitude to spherical coordinates in radians * 36 | * using the earth's radius for rho. * 37 | * * 38 | *****************************************************************************/ 39 | 40 | p1.rho = EARTH_RADIUS; 41 | p1.theta = -1.0 * DEGTORAD(lon1); 42 | p1.phi = (DEGTORAD(-1.0 * lat1)) + DEGTORAD(90.0); 43 | 44 | p2.rho = EARTH_RADIUS; 45 | p2.theta = -1.0 * DEGTORAD(lon2); 46 | p2.phi = (DEGTORAD(-1.0 * lat2)) + DEGTORAD(90.0); 47 | 48 | /***************************************************************************** 49 | * * 50 | * Compute the distance between the points. * 51 | * * 52 | *****************************************************************************/ 53 | 54 | arclen(p1, p2, d); 55 | 56 | return 0; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /examples_pc/source/hashpjw.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- hashpjw.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "hashpjw.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * -------------------------------- hashpjw ------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int hashpjw(const void *key) { 16 | 17 | const char *ptr; 18 | 19 | int val; 20 | 21 | /***************************************************************************** 22 | * * 23 | * Hash the key by performing a number of bit operations on it. * 24 | * * 25 | *****************************************************************************/ 26 | 27 | val = 0; 28 | ptr = key; 29 | 30 | while (*ptr != '\0') { 31 | 32 | int tmp; 33 | 34 | val = (val << 4) + (*ptr); 35 | 36 | if (tmp = (val & 0xf0000000)) { 37 | 38 | val = val ^ (tmp >> 24); 39 | val = val ^ tmp; 40 | 41 | } 42 | 43 | ptr++; 44 | 45 | } 46 | 47 | /***************************************************************************** 48 | * * 49 | * In practice, replace PRIME_TBLSIZ with the actual table size. * 50 | * * 51 | *****************************************************************************/ 52 | 53 | return val % PRIME_TBLSIZ; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /examples_pc/source/issort.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- issort.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | #include 9 | 10 | #include "sort.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * -------------------------------- issort -------------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int issort(void *data, int size, int esize, int (*compare)(const void *key1, 19 | const void *key2)) { 20 | 21 | char *a = data; 22 | 23 | void *key; 24 | 25 | int i, 26 | j; 27 | 28 | /***************************************************************************** 29 | * * 30 | * Allocate storage for the key element. * 31 | * * 32 | *****************************************************************************/ 33 | 34 | if ((key = (char *)malloc(esize)) == NULL) 35 | return -1; 36 | 37 | /***************************************************************************** 38 | * * 39 | * Repeatedly insert a key element among the sorted elements. * 40 | * * 41 | *****************************************************************************/ 42 | 43 | for (j = 1; j < size; j++) { 44 | 45 | memcpy(key, &a[j * esize], esize); 46 | i = j - 1; 47 | 48 | /************************************************************************** 49 | * * 50 | * Determine the position at which to insert the key element. * 51 | * * 52 | **************************************************************************/ 53 | 54 | while (i >= 0 && compare(&a[i * esize], key) > 0) { 55 | 56 | memcpy(&a[(i + 1) * esize], &a[i * esize], esize); 57 | i--; 58 | 59 | } 60 | 61 | memcpy(&a[(i + 1) * esize], key, esize); 62 | 63 | } 64 | 65 | /***************************************************************************** 66 | * * 67 | * Free the storage allocated for sorting. * 68 | * * 69 | *****************************************************************************/ 70 | 71 | free(key); 72 | 73 | return 0; 74 | 75 | } 76 | -------------------------------------------------------------------------------- /examples_pc/source/lsqe.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- lsqe.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "nummeths.h" 10 | 11 | /***************************************************************************** 12 | * * 13 | * --------------------------------- lsqe --------------------------------- * 14 | * * 15 | *****************************************************************************/ 16 | 17 | void lsqe(const double *x, const double *y, int n, double *b1, double *b0) { 18 | 19 | double sumx, 20 | sumy, 21 | sumx2, 22 | sumxy; 23 | 24 | int i; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Compute the required summations. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | sumx = 0.0; 33 | sumy = 0.0; 34 | sumx2 = 0.0; 35 | sumxy = 0.0; 36 | 37 | for (i = 0; i < n; i++) { 38 | 39 | sumx = sumx + x[i]; 40 | sumy = sumy + y[i]; 41 | sumx2 = sumx2 + pow(x[i], 2.0); 42 | sumxy = sumxy + (x[i] * y[i]); 43 | 44 | } 45 | 46 | /***************************************************************************** 47 | * * 48 | * Compute the least-squares estimators. * 49 | * * 50 | *****************************************************************************/ 51 | 52 | *b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n)); 53 | *b0 = (sumy - ((*b1) * sumx)) / (double)n; 54 | 55 | return; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /examples_pc/source/page.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- page.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "clist.h" 8 | #include "page.h" 9 | 10 | /***************************************************************************** 11 | * * 12 | * ----------------------------- replace_page ----------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int replace_page(CListElmt **current) { 17 | 18 | /***************************************************************************** 19 | * * 20 | * Circle through the list of pages until one is found to replace. * 21 | * * 22 | *****************************************************************************/ 23 | 24 | while (((Page *)(*current)->data)->reference != 0) { 25 | 26 | ((Page *)(*current)->data)->reference = 0; 27 | *current = clist_next(*current); 28 | 29 | } 30 | 31 | return ((Page *)(*current)->data)->number; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /examples_pc/source/queue.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- queue.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "list.h" 10 | #include "queue.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ----------------------------- queue_enqueue ---------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int queue_enqueue(Queue *queue, const void *data) { 19 | 20 | /***************************************************************************** 21 | * * 22 | * Enqueue the data. * 23 | * * 24 | *****************************************************************************/ 25 | 26 | return list_ins_next(queue, list_tail(queue), data); 27 | 28 | } 29 | 30 | /***************************************************************************** 31 | * * 32 | * ----------------------------- queue_dequeue ---------------------------- * 33 | * * 34 | *****************************************************************************/ 35 | 36 | int queue_dequeue(Queue *queue, void **data) { 37 | 38 | /***************************************************************************** 39 | * * 40 | * Dequeue the data. * 41 | * * 42 | *****************************************************************************/ 43 | 44 | return list_rem_next(queue, NULL, data); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /examples_pc/source/spell.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- spell.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "search.h" 10 | #include "spell.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ------------------------------ compare_str ----------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | static int compare_str(const void *str1, const void *str2) { 19 | 20 | int retval; 21 | 22 | if ((retval = strcmp((const char *)str1, (const char *)str2)) > 0) 23 | return 1; 24 | else if (retval < 0) 25 | return -1; 26 | else 27 | return 0; 28 | 29 | } 30 | 31 | /***************************************************************************** 32 | * * 33 | * --------------------------------- spell -------------------------------- * 34 | * * 35 | *****************************************************************************/ 36 | 37 | int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word) { 38 | 39 | /***************************************************************************** 40 | * * 41 | * Look up the word. * 42 | * * 43 | *****************************************************************************/ 44 | 45 | if (bisearch(dictionary, word, size, SPELL_SIZE, compare_str) >= 0) 46 | return 1; 47 | else 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /examples_pc/source/stack.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- stack.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "list.h" 10 | #include "stack.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ------------------------------ stack_push ------------------------------ * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int stack_push(Stack *stack, const void *data) { 19 | 20 | /***************************************************************************** 21 | * * 22 | * Push the data onto the stack. * 23 | * * 24 | *****************************************************************************/ 25 | 26 | return list_ins_next(stack, NULL, data); 27 | 28 | } 29 | 30 | /***************************************************************************** 31 | * * 32 | * ------------------------------ stack_pop ------------------------------- * 33 | * * 34 | *****************************************************************************/ 35 | 36 | int stack_pop(Stack *stack, void **data) { 37 | 38 | /***************************************************************************** 39 | * * 40 | * Pop the data off the stack. * 41 | * * 42 | *****************************************************************************/ 43 | 44 | return list_rem_next(stack, NULL, data); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /examples_unix/contents: -------------------------------------------------------------------------------- 1 | source: 2 | arclen.c bfs.c bisearch.c bistree.c bit.c bitree.c 3 | cbc.c chtbl.c clist.c cover.c ctsort.c cvxhull.c 4 | des.c dfs.c directls.c dlist.c events.c fact.c 5 | factor.c facttail.c frames.c geodist.c graph.c hashpjw.c 6 | heap.c huffman.c interpol.c issort.c lex.c lint.c 7 | list.c lsqe.c lz77.c mgsort.c mst.c ohtbl.c 8 | page.c parcels.c qksort.c queue.c root.c route.c 9 | rsa.c rxsort.c set.c shortest.c spell.c stack.c 10 | transfer.c traverse.c tsp.c 11 | 12 | include: 13 | bfs.h bistree.h bit.h bitree.h cbc.h chtbl.h 14 | clist.h compress.h cover.h dfs.h directls.h dlist.h 15 | encrypt.h event.h events.h fact.h factor.h facttail.h 16 | frames.h geodist.h geometry.h graph.h graphalg.h hashpjw.h 17 | heap.h lex.h list.h nummeths.h ohtbl.h page.h 18 | parcel.h parcels.h pqueue.h queue.h route.h search.h 19 | set.h sort.h spell.h stack.h symbol.h transfer.h 20 | traverse.h 21 | 22 | examples: 23 | bistree bit bitree chtbl clist compress 24 | dlist encrypt geometry graph graphalg heap 25 | list nummeths ohtbl pqueue queue recurse 26 | search set sort stack 27 | 28 | examples/bistree: 29 | bistree.mak ex-1.c 30 | 31 | examples/bit: 32 | bit.mak ex-1.c 33 | 34 | examples/bitree: 35 | bitree.mak ex-1.c 36 | 37 | examples/chtbl: 38 | chtbl.mak ex-1.c 39 | 40 | examples/clist: 41 | clist.mak page.mak ex-1.c ex-2.c 42 | 43 | examples/compress: 44 | compress.mak ex-1.c sample.txt 45 | 46 | examples/dlist: 47 | dlist.mak ex-1.c 48 | 49 | examples/encrypt: 50 | encrypt.mak cbc.mak ex-1.c ex-2.c 51 | 52 | examples/geometry: 53 | geometry.mak geodist.mak ex-1.c ex-2.c 54 | 55 | examples/graph: 56 | graph.mak bfs.mak dfs.mak ex-1.c ex-2.c ex-3.c 57 | 58 | examples/graphalg: 59 | graphalg.mak route.mak ex-1.c ex-2.c 60 | 61 | examples/heap: 62 | heap.mak ex-1.c 63 | 64 | examples/list: 65 | list.mak ex-1.c 66 | 67 | examples/nummeths: 68 | nummeths.mak ex-1.c 69 | 70 | examples/ohtbl: 71 | ohtbl.mak ex-1.c 72 | 73 | examples/pqueue: 74 | pqueue.mak ex-1.c 75 | 76 | examples/queue: 77 | ex-1.c 78 | queue.mak 79 | 80 | examples/recurse: 81 | fact.mak factor.mak ex-1.c ex-2.c 82 | 83 | examples/search: 84 | search.mak spell.mak ex-1.c ex-2.c 85 | 86 | examples/set: 87 | set.mak cover.mak ex-1.c ex-2.c 88 | 89 | examples/sort: 90 | directls.mak sort.mak ex-1.c ex-2.c 91 | 92 | examples/stack: 93 | stack.mak ex-1.c 94 | -------------------------------------------------------------------------------- /examples_unix/examples/checker: -------------------------------------------------------------------------------- 1 | for a in `ls -1` 2 | do 3 | echo $a: 4 | if [ -d $a ] 5 | then 6 | cd $a 7 | grep -n including *-*.c 8 | cd .. 9 | fi 10 | done 11 | -------------------------------------------------------------------------------- /examples_unix/examples/geometry/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing great-circle distances (see Chapter * 7 | * 17). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "geodist.h" 14 | 15 | /***************************************************************************** 16 | * * 17 | * --------------------------------- main --------------------------------- * 18 | * * 19 | *****************************************************************************/ 20 | 21 | int main(int argc, char **argv) { 22 | 23 | double lat1, 24 | lon1, 25 | lat2, 26 | lon2, 27 | distance; 28 | 29 | /***************************************************************************** 30 | * * 31 | * Test computing distances between points on Earth. * 32 | * * 33 | *****************************************************************************/ 34 | 35 | fprintf(stdout, "Computing distances between points on Earth\n"); 36 | 37 | /* SFO (San Francisco) */ 38 | lat1 = 37.62; 39 | lon1 = 122.38; 40 | 41 | /* LAX (Los Angeles) */ 42 | lat2 = 33.94; 43 | lon2 = 118.41; 44 | 45 | if (geodist(lat1, lon1, lat2, lon2, &distance) != 0) 46 | return 1; 47 | 48 | fprintf(stdout, "SFO: (%+07.2lf,%+07.2lf)\n", lat1, lon1); 49 | fprintf(stdout, "LAX: (%+07.2lf,%+07.2lf)\n", lat2, lon2); 50 | fprintf(stdout, "distance=%d\n", (int)distance); 51 | 52 | /* CDG (Paris) */ 53 | lat1 = 49.01; 54 | lon1 = -2.55; 55 | 56 | /* PER (Perth) */ 57 | lat2 = -31.94; 58 | lon2 = -115.97; 59 | 60 | if (geodist(lat1, lon1, lat2, lon2, &distance) != 0) 61 | return 1; 62 | 63 | fprintf(stdout, "CDG: (%+07.2lf,%+07.2lf)\n", lat1, lon1); 64 | fprintf(stdout, "PER: (%+07.2lf,%+07.2lf)\n", lat2, lon2); 65 | fprintf(stdout, "distance=%d\n", (int)distance); 66 | 67 | return 0; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /examples_unix/examples/recurse/ex-1.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-1.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing a factorial using recursion and tail * 7 | * recursion (see Chapter 3). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "fact.h" 14 | #include "facttail.h" 15 | 16 | /***************************************************************************** 17 | * * 18 | * --------------------------------- main --------------------------------- * 19 | * * 20 | *****************************************************************************/ 21 | 22 | int main(int argc, char **argv) { 23 | 24 | int n; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Computer the factorials of several numbers. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | for (n = 0; n <= 13; n++) { 33 | 34 | fprintf(stdout, "%2d! recursive: %-10d ", n, fact(n)); 35 | fprintf(stdout, "tail recursive: %-10d\n", facttail(n, 1)); 36 | 37 | } 38 | 39 | return 0; 40 | 41 | } 42 | -------------------------------------------------------------------------------- /examples_unix/examples/recurse/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates computing the prime factors of numbers using * 7 | * recursion (see Chapter 3). * 8 | * * 9 | *****************************************************************************/ 10 | 11 | #include 12 | 13 | #include "factor.h" 14 | 15 | /***************************************************************************** 16 | * * 17 | * --------------------------------- main --------------------------------- * 18 | * * 19 | *****************************************************************************/ 20 | 21 | int main(int argc, char **argv) { 22 | 23 | int n; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Compute the prime factors of several numbers. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | for (n = 1; n <= 10000; n++) { 32 | 33 | fprintf(stdout, "Factoring %d\n", n); 34 | factor(n, n, 2); 35 | 36 | } 37 | 38 | return 0; 39 | 40 | } 41 | -------------------------------------------------------------------------------- /examples_unix/examples/sort/ex-2.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ex-2.c * 4 | * ====== * 5 | * * 6 | * Description: Illustrates sorting a directory listing (see Chapter 12). * 7 | * * 8 | *****************************************************************************/ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "directls.h" 15 | 16 | /***************************************************************************** 17 | * * 18 | * --------------------------------- main --------------------------------- * 19 | * * 20 | *****************************************************************************/ 21 | 22 | int main(int argc, char **argv) { 23 | 24 | Directory *dir; 25 | 26 | char buffer[MAXPATHLEN]; 27 | 28 | int count, 29 | i; 30 | 31 | /***************************************************************************** 32 | * * 33 | * Get the directory listing. * 34 | * * 35 | *****************************************************************************/ 36 | 37 | if (argc > 1) { 38 | 39 | if ((count = directls(argv[1], &dir)) < 0) { 40 | 41 | fprintf(stdout, "Could not read directory\n"); 42 | exit(1); 43 | 44 | } 45 | 46 | } 47 | 48 | else { 49 | 50 | if ((count = directls(getcwd(buffer, MAXPATHLEN), &dir)) < 0) { 51 | 52 | fprintf(stdout, "Could not read directory\n"); 53 | exit(1); 54 | 55 | } 56 | 57 | } 58 | 59 | /***************************************************************************** 60 | * * 61 | * Display the directory listing. * 62 | * * 63 | *****************************************************************************/ 64 | 65 | for (i = 0; i < count; i++) 66 | fprintf(stdout, "%s\n", dir[i].name); 67 | 68 | fprintf(stdout, "%d found\n", count); 69 | free(dir); 70 | 71 | return 0; 72 | 73 | } 74 | -------------------------------------------------------------------------------- /examples_unix/include/bfs.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- bfs.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BFS_H 8 | #define BFS_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in breadth-first search. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct BfsVertex_ { 20 | 21 | void *data; 22 | 23 | VertexColor color; 24 | int hops; 25 | 26 | } BfsVertex; 27 | 28 | /***************************************************************************** 29 | * * 30 | * --------------------------- Public Interface --------------------------- * 31 | * * 32 | *****************************************************************************/ 33 | 34 | int bfs(Graph *graph, BfsVertex *start, List *hops); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /examples_unix/include/bistree.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- bistree.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BISTREE_H 8 | #define BISTREE_H 9 | 10 | #include "bitree.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define balance factors for AVL trees. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | #define AVL_LFT_HEAVY 1 19 | #define AVL_BALANCED 0 20 | #define AVL_RGT_HEAVY -1 21 | 22 | /***************************************************************************** 23 | * * 24 | * Define a structure for nodes in AVL trees. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | typedef struct AvlNode_ { 29 | 30 | void *data; 31 | int hidden; 32 | int factor; 33 | 34 | } AvlNode; 35 | 36 | /***************************************************************************** 37 | * * 38 | * Implement binary search trees as binary trees. * 39 | * * 40 | *****************************************************************************/ 41 | 42 | typedef BiTree BisTree; 43 | 44 | /***************************************************************************** 45 | * * 46 | * --------------------------- Public Interface --------------------------- * 47 | * * 48 | *****************************************************************************/ 49 | 50 | void bistree_init(BisTree *tree, int (*compare)(const void *key1, const void 51 | *key2), void (*destroy)(void *data)); 52 | 53 | void bistree_destroy(BisTree *tree); 54 | 55 | int bistree_insert(BisTree *tree, const void *data); 56 | 57 | int bistree_remove(BisTree *tree, const void *data); 58 | 59 | int bistree_lookup(BisTree *tree, void **data); 60 | 61 | #define bistree_size(tree) ((tree)->size) 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /examples_unix/include/bit.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- bit.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BIT_H 8 | #define BIT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int bit_get(const unsigned char *bits, int pos); 17 | 18 | void bit_set(unsigned char *bits, int pos, int state); 19 | 20 | void bit_xor(const unsigned char *bits1, const unsigned char *bits2, unsigned 21 | char *bitsx, int size); 22 | 23 | void bit_rot_left(unsigned char *bits, int size, int count); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /examples_unix/include/bitree.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- bitree.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef BITREE_H 8 | #define BITREE_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for binary tree nodes. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct BiTreeNode_ { 19 | 20 | void *data; 21 | struct BiTreeNode_ *left; 22 | struct BiTreeNode_ *right; 23 | 24 | } BiTreeNode; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Define a structure for binary trees. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | typedef struct BiTree_ { 33 | 34 | int size; 35 | 36 | int (*compare)(const void *key1, const void *key2); 37 | void (*destroy)(void *data); 38 | 39 | BiTreeNode *root; 40 | 41 | } BiTree; 42 | 43 | /***************************************************************************** 44 | * * 45 | * --------------------------- Public Interface --------------------------- * 46 | * * 47 | *****************************************************************************/ 48 | 49 | void bitree_init(BiTree *tree, void (*destroy)(void *data)); 50 | 51 | void bitree_destroy(BiTree *tree); 52 | 53 | int bitree_ins_left(BiTree *tree, BiTreeNode *node, const void *data); 54 | 55 | int bitree_ins_right(BiTree *tree, BiTreeNode *node, const void *data); 56 | 57 | void bitree_rem_left(BiTree *tree, BiTreeNode *node); 58 | 59 | void bitree_rem_right(BiTree *tree, BiTreeNode *node); 60 | 61 | int bitree_merge(BiTree *merge, BiTree *left, BiTree *right, const void *data); 62 | 63 | #define bitree_size(tree) ((tree)->size) 64 | 65 | #define bitree_root(tree) ((tree)->root) 66 | 67 | #define bitree_is_eob(node) ((node) == NULL) 68 | 69 | #define bitree_is_leaf(node) ((node)->left == NULL && (node)->right == NULL) 70 | 71 | #define bitree_data(node) ((node)->data) 72 | 73 | #define bitree_left(node) ((node)->left) 74 | 75 | #define bitree_right(node) ((node)->right) 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /examples_unix/include/cbc.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- cbc.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CBC_H 8 | #define CBC_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | void cbc_encipher(const unsigned char *plaintext, unsigned char *ciphertext, 17 | const unsigned char *key, int size); 18 | 19 | void cbc_decipher(const unsigned char *ciphertext, unsigned char *plaintext, 20 | const unsigned char *key, int size); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_unix/include/chtbl.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- chtbl.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CHTBL_H 8 | #define CHTBL_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Define a structure for chained hash tables. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef struct CHTbl_ { 21 | 22 | int buckets; 23 | 24 | int (*h)(const void *key); 25 | int (*match)(const void *key1, const void *key2); 26 | void (*destroy)(void *data); 27 | 28 | int size; 29 | List *table; 30 | 31 | } CHTbl; 32 | 33 | /***************************************************************************** 34 | * * 35 | * --------------------------- Public Interface --------------------------- * 36 | * * 37 | *****************************************************************************/ 38 | 39 | int chtbl_init(CHTbl *htbl, int buckets, int (*h)(const void *key), int 40 | (*match)(const void *key1, const void *key2), void (*destroy)(void *data)); 41 | 42 | void chtbl_destroy(CHTbl *htbl); 43 | 44 | int chtbl_insert(CHTbl *htbl, const void *data); 45 | 46 | int chtbl_remove(CHTbl *htbl, void **data); 47 | 48 | int chtbl_lookup(const CHTbl *htbl, void **data); 49 | 50 | #define chtbl_size(htbl) ((htbl)->size) 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /examples_unix/include/clist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- clist.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef CLIST_H 8 | #define CLIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for circular list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct CListElmt_ { 19 | 20 | void *data; 21 | struct CListElmt_ *next; 22 | 23 | } CListElmt; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Define a structure for circular lists. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | typedef struct CList_ { 32 | 33 | int size; 34 | 35 | int (*match)(const void *key1, const void *key2); 36 | void (*destroy)(void *data); 37 | 38 | CListElmt *head; 39 | 40 | } CList; 41 | 42 | /***************************************************************************** 43 | * * 44 | * --------------------------- Public Interface --------------------------- * 45 | * * 46 | *****************************************************************************/ 47 | 48 | void clist_init(CList *list, void (*destroy)(void *data)); 49 | 50 | void clist_destroy(CList *list); 51 | 52 | int clist_ins_next(CList *list, CListElmt *element, const void *data); 53 | 54 | int clist_rem_next(CList *list, CListElmt *element, void **data); 55 | 56 | #define clist_size(list) ((list)->size) 57 | 58 | #define clist_head(list) ((list)->head) 59 | 60 | #define clist_data(element) ((element)->data) 61 | 62 | #define clist_next(element) ((element)->next) 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /examples_unix/include/cover.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- cover.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef COVER_H 8 | #define COVER_H 9 | 10 | #include "set.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for subsets identified by a key. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct KSet_ { 19 | 20 | void *key; 21 | Set set; 22 | 23 | } KSet; 24 | 25 | /***************************************************************************** 26 | * * 27 | * --------------------------- Public Interface --------------------------- * 28 | * * 29 | *****************************************************************************/ 30 | 31 | int cover(Set *members, Set *subsets, Set *covering); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /examples_unix/include/dfs.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- dfs.h --------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DFS_H 8 | #define DFS_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in depth-first search. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct DfsVertex_ { 20 | 21 | void *data; 22 | 23 | VertexColor color; 24 | 25 | } DfsVertex; 26 | 27 | /***************************************************************************** 28 | * * 29 | * --------------------------- Public Interface --------------------------- * 30 | * * 31 | *****************************************************************************/ 32 | 33 | int dfs(Graph *graph, List *ordered); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /examples_unix/include/directls.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ directls.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DIRECTLS_H 8 | #define DIRECTLS_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for directory entries. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct Directory_ { 19 | 20 | char name[MAXNAMLEN + 1]; 21 | 22 | } Directory; 23 | 24 | /***************************************************************************** 25 | * * 26 | * --------------------------- Public Interface --------------------------- * 27 | * * 28 | *****************************************************************************/ 29 | 30 | int directls(const char *path, Directory **dir); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /examples_unix/include/dlist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- dlist.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef DLIST_H 8 | #define DLIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for doubly-linked list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct DListElmt_ { 19 | 20 | void *data; 21 | struct DListElmt_ *prev; 22 | struct DListElmt_ *next; 23 | 24 | } DListElmt; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Define a structure for doubly-linked lists. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | typedef struct DList_ { 33 | 34 | int size; 35 | 36 | int (*match)(const void *key1, const void *key2); 37 | void (*destroy)(void *data); 38 | 39 | DListElmt *head; 40 | DListElmt *tail; 41 | 42 | } DList; 43 | 44 | /***************************************************************************** 45 | * * 46 | * --------------------------- Public Interface --------------------------- * 47 | * * 48 | *****************************************************************************/ 49 | 50 | void dlist_init(DList *list, void (*destroy)(void *data)); 51 | 52 | void dlist_destroy(DList *list); 53 | 54 | int dlist_ins_next(DList *list, DListElmt *element, const void *data); 55 | 56 | int dlist_ins_prev(DList *list, DListElmt *element, const void *data); 57 | 58 | int dlist_remove(DList *list, DListElmt *element, void **data); 59 | 60 | #define dlist_size(list) ((list)->size) 61 | 62 | #define dlist_head(list) ((list)->head) 63 | 64 | #define dlist_tail(list) ((list)->tail) 65 | 66 | #define dlist_is_head(element) ((element)->prev == NULL ? 1 : 0) 67 | 68 | #define dlist_is_tail(element) ((element)->next == NULL ? 1 : 0) 69 | 70 | #define dlist_data(element) ((element)->data) 71 | 72 | #define dlist_next(element) ((element)->next) 73 | 74 | #define dlist_prev(element) ((element)->prev) 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /examples_unix/include/encrypt.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- encrypt.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef ENCRYPT_H 8 | #define ENCRYPT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * In a secure implementation, Huge should be at least 400 decimal digits, * 13 | * instead of the 10 below (ULONG_MAX = 4294967295). * 14 | * * 15 | *****************************************************************************/ 16 | 17 | typedef unsigned long Huge; 18 | 19 | /***************************************************************************** 20 | * * 21 | * Define a structure for RSA public keys. * 22 | * * 23 | *****************************************************************************/ 24 | 25 | typedef struct RsaPubKey_ { 26 | 27 | Huge e; 28 | Huge n; 29 | 30 | } RsaPubKey; 31 | 32 | /***************************************************************************** 33 | * * 34 | * Define a structure for RSA private keys. * 35 | * * 36 | *****************************************************************************/ 37 | 38 | typedef struct RsaPriKey_ { 39 | 40 | Huge d; 41 | Huge n; 42 | 43 | } RsaPriKey; 44 | 45 | /***************************************************************************** 46 | * * 47 | * --------------------------- Public Interface --------------------------- * 48 | * * 49 | *****************************************************************************/ 50 | 51 | void des_encipher(const unsigned char *plaintext, unsigned char *ciphertext, 52 | const unsigned char *key); 53 | 54 | void des_decipher(const unsigned char *ciphertext, unsigned char *plaintext, 55 | const unsigned char *key); 56 | 57 | void rsa_encipher(Huge plaintext, Huge *ciphertext, RsaPubKey pubkey); 58 | 59 | void rsa_decipher(Huge ciphertext, Huge *plaintext, RsaPriKey prikey); 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /examples_unix/include/event.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- event.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef EVENT_H 8 | #define EVENT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define an event structure for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Event_ { 17 | 18 | int type; 19 | 20 | } Event; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_unix/include/events.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- events.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef EVENTS_H 8 | #define EVENTS_H 9 | 10 | #include "event.h" 11 | #include "queue.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int receive_event(Queue *events, const Event *event); 20 | 21 | int process_event(Queue *events, int (*dispatch)(Event *event)); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples_unix/include/fact.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- fact.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACT_H 8 | #define FACT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int fact(int n); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_unix/include/factor.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- factor.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACTOR_H 8 | #define FACTOR_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | void factor(int number, int n, int j); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_unix/include/facttail.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ facttail.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FACTTAIL_H 8 | #define FACTTAIL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int facttail(int n, int a); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /examples_unix/include/frames.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- frames.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef FRAMES_H 8 | #define FRAMES_H 9 | 10 | #include "list.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * --------------------------- Public Interface --------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int alloc_frame(List *frames); 19 | 20 | int free_frame(List *frames, int frame_number); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_unix/include/geodist.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- geodist.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef GEODIST_H 8 | #define GEODIST_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define the radius of the earth in nautical miles. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define EARTH_RADIUS 3440.065 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int geodist(double lat1, double lon1, double lat2, double lon2, double *d); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_unix/include/graphalg.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ graphalg.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef GRAPHALG_H 8 | #define GRAPHALG_H 9 | 10 | #include "graph.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * Define a structure for vertices in minimum spanning trees. * 16 | * * 17 | *****************************************************************************/ 18 | 19 | typedef struct MstVertex_ { 20 | 21 | void *data; 22 | double weight; 23 | 24 | VertexColor color; 25 | double key; 26 | 27 | struct MstVertex_ *parent; 28 | 29 | } MstVertex; 30 | 31 | /***************************************************************************** 32 | * * 33 | * Define a structure for vertices in shortest-path problems. * 34 | * * 35 | *****************************************************************************/ 36 | 37 | typedef struct PathVertex_ { 38 | 39 | void *data; 40 | double weight; 41 | 42 | VertexColor color; 43 | double d; 44 | 45 | struct PathVertex_ *parent; 46 | 47 | } PathVertex; 48 | 49 | /***************************************************************************** 50 | * * 51 | * Define a structure for vertices in traveling-salesman problems. * 52 | * * 53 | *****************************************************************************/ 54 | 55 | typedef struct TspVertex_ { 56 | 57 | void *data; 58 | 59 | double x, 60 | y; 61 | 62 | VertexColor color; 63 | 64 | } TspVertex; 65 | 66 | /***************************************************************************** 67 | * * 68 | * --------------------------- Public Interface --------------------------- * 69 | * * 70 | *****************************************************************************/ 71 | 72 | int mst(Graph *graph, const MstVertex *start, List *span, int (*match)(const 73 | void *key1, const void *key2)); 74 | 75 | int shortest(Graph *graph, const PathVertex *start, List *paths, int (*match) 76 | (const void *key1, const void *key2)); 77 | 78 | int tsp(List *vertices, const TspVertex *start, List *tour, int (*match) 79 | (const void *key1, const void *key2)); 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /examples_unix/include/hashpjw.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- hashpjw.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef HASHPJW_H 8 | #define HASHPJW_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a table size for demonstration purposes only. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define PRIME_TBLSIZ 1699 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int hashpjw(const void *key); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_unix/include/heap.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- heap.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef HEAP_H 8 | #define HEAP_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a structure for heaps. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Heap_ { 17 | 18 | int size; 19 | 20 | int (*compare)(const void *key1, const void *key2); 21 | void (*destroy)(void *data); 22 | 23 | void **tree; 24 | 25 | } Heap; 26 | 27 | /***************************************************************************** 28 | * * 29 | * --------------------------- Public Interface --------------------------- * 30 | * * 31 | *****************************************************************************/ 32 | 33 | void heap_init(Heap *heap, int (*compare)(const void *key1, const void *key2), 34 | void (*destroy)(void *data)); 35 | 36 | void heap_destroy(Heap *heap); 37 | 38 | int heap_insert(Heap *heap, const void *data); 39 | 40 | int heap_extract(Heap *heap, void **data); 41 | 42 | #define heap_size(heap) ((heap)->size) 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /examples_unix/include/lex.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * --------------------------------- lex.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef LEX_H 8 | #define LEX_H 9 | 10 | #include "chtbl.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define the token types recognized by the lexical analyzer. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef enum Token_ {lexit, error, digit, other} Token; 19 | 20 | /***************************************************************************** 21 | * * 22 | * --------------------------- Public Interface --------------------------- * 23 | * * 24 | *****************************************************************************/ 25 | 26 | Token lex(const char *istream, CHTbl *symtbl); 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /examples_unix/include/list.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- list.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef LIST_H 8 | #define LIST_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for linked list elements. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct ListElmt_ { 19 | 20 | void *data; 21 | struct ListElmt_ *next; 22 | 23 | } ListElmt; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Define a structure for linked lists. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | typedef struct List_ { 32 | 33 | int size; 34 | 35 | int (*match)(const void *key1, const void *key2); 36 | void (*destroy)(void *data); 37 | 38 | ListElmt *head; 39 | ListElmt *tail; 40 | 41 | } List; 42 | 43 | /***************************************************************************** 44 | * * 45 | * --------------------------- Public Interface --------------------------- * 46 | * * 47 | *****************************************************************************/ 48 | 49 | void list_init(List *list, void (*destroy)(void *data)); 50 | 51 | void list_destroy(List *list); 52 | 53 | int list_ins_next(List *list, ListElmt *element, const void *data); 54 | 55 | int list_rem_next(List *list, ListElmt *element, void **data); 56 | 57 | #define list_size(list) ((list)->size) 58 | 59 | #define list_head(list) ((list)->head) 60 | 61 | #define list_tail(list) ((list)->tail) 62 | 63 | #define list_is_head(list, element) ((element) == (list)->head ? 1 : 0) 64 | 65 | #define list_is_tail(element) ((element)->next == NULL ? 1 : 0) 66 | 67 | #define list_data(element) ((element)->data) 68 | 69 | #define list_next(element) ((element)->next) 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /examples_unix/include/nummeths.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ nummeths.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef NUMMETHS_H 8 | #define NUMMETHS_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int interpol(const double *x, const double *fx, int n, double *z, double *pz, 17 | int m); 18 | 19 | void lsqe(const double *x, const double *y, int n, double *b1, double *b0); 20 | 21 | int root(double (*f)(double x), double (*g)(double x), double *x, int *n, 22 | double delta); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /examples_unix/include/ohtbl.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- ohtbl.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef OHTBL_H 8 | #define OHTBL_H 9 | 10 | #include 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for open-addressed hash tables. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct OHTbl_ { 19 | 20 | int positions; 21 | void *vacated; 22 | 23 | int (*h1)(const void *key); 24 | int (*h2)(const void *key); 25 | int (*match)(const void *key1, const void *key2); 26 | void (*destroy)(void *data); 27 | 28 | int size; 29 | void **table; 30 | 31 | } OHTbl; 32 | 33 | /***************************************************************************** 34 | * * 35 | * --------------------------- Public Interface --------------------------- * 36 | * * 37 | *****************************************************************************/ 38 | 39 | int ohtbl_init(OHTbl *htbl, int positions, int (*h1)(const void *key), int 40 | (*h2)(const void *key), int (*match)(const void *key1, const void *key2), 41 | void (*destroy)(void *data)); 42 | 43 | void ohtbl_destroy(OHTbl *htbl); 44 | 45 | int ohtbl_insert(OHTbl *htbl, const void *data); 46 | 47 | int ohtbl_remove(OHTbl *htbl, void **data); 48 | 49 | int ohtbl_lookup(const OHTbl *htbl, void **data); 50 | 51 | #define ohtbl_size(htbl) ((htbl)->size) 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /examples_unix/include/page.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- page.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PAGE_H 8 | #define PAGE_H 9 | 10 | #include "clist.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Define a structure for information about pages. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef struct Page_ { 19 | 20 | int number; 21 | int reference; 22 | 23 | } Page; 24 | 25 | /***************************************************************************** 26 | * * 27 | * --------------------------- Public Interface --------------------------- * 28 | * * 29 | *****************************************************************************/ 30 | 31 | int replace_page(CListElmt **current); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /examples_unix/include/parcel.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- parcel.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PARCEL_H 8 | #define PARCEL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define a parcel structure for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | typedef struct Parcel_ { 17 | 18 | int priority; 19 | 20 | } Parcel; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_unix/include/parcels.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- parcels.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PARCELS_H 8 | #define PARCELS_H 9 | 10 | #include "parcel.h" 11 | #include "pqueue.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int get_parcel(PQueue *parcels, Parcel *parcel); 20 | 21 | int put_parcel(PQueue *parcels, const Parcel *parcel); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples_unix/include/pqueue.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- pqueue.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef PQUEUE_H 8 | #define PQUEUE_H 9 | 10 | #include "heap.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * Implement priority queues as heaps. * 15 | * * 16 | *****************************************************************************/ 17 | 18 | typedef Heap PQueue; 19 | 20 | /***************************************************************************** 21 | * * 22 | * --------------------------- Public Interface --------------------------- * 23 | * * 24 | *****************************************************************************/ 25 | 26 | #define pqueue_init heap_init 27 | 28 | #define pqueue_destroy heap_destroy 29 | 30 | #define pqueue_insert heap_insert 31 | 32 | #define pqueue_extract heap_extract 33 | 34 | #define pqueue_peek(pqueue) ((pqueue)->tree == NULL ? NULL : (pqueue)->tree[0]) 35 | 36 | #define pqueue_size heap_size 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /examples_unix/include/queue.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- queue.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef QUEUE_H 8 | #define QUEUE_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement queues as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Queue; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | #define queue_init list_init 29 | 30 | #define queue_destroy list_destroy 31 | 32 | int queue_enqueue(Queue *queue, const void *data); 33 | 34 | int queue_dequeue(Queue *queue, void **data); 35 | 36 | #define queue_peek(queue) ((queue)->head == NULL ? NULL : (queue)->head->data) 37 | 38 | #define queue_size list_size 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples_unix/include/route.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- route.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef ROUTE_H 8 | #define ROUTE_H 9 | 10 | #include "graphalg.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int route(List *paths, PathVertex *destination, PathVertex **next, int 20 | (*match)(const void *key1, const void *key2)); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /examples_unix/include/search.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- search.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SEARCH_H 8 | #define SEARCH_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int bisearch(void *sorted, const void *target, int size, int esize, int 17 | (*compare)(const void *key1, const void *key2)); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /examples_unix/include/set.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- set.h --------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SET_H 8 | #define SET_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement sets as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Set; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | void set_init(Set *set, int (*match)(const void *key1, const void *key2), 29 | void (*destroy)(void *data)); 30 | 31 | #define set_destroy list_destroy 32 | 33 | int set_insert(Set *set, const void *data); 34 | 35 | int set_remove(Set *set, void **data); 36 | 37 | int set_union(Set *setu, const Set *set1, const Set *set2); 38 | 39 | int set_intersection(Set *seti, const Set *set1, const Set *set2); 40 | 41 | int set_difference(Set *setd, const Set *set1, const Set *set2); 42 | 43 | int set_is_member(const Set *set, const void *data); 44 | 45 | int set_is_subset(const Set *set1, const Set *set2); 46 | 47 | int set_is_equal(const Set *set1, const Set *set2); 48 | 49 | #define set_size(set) ((set)->size) 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /examples_unix/include/sort.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- sort.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SORT_H 8 | #define SORT_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int issort(void *data, int size, int esize, int (*compare)(const void *key1, 17 | const void *key2)); 18 | 19 | int qksort(void *data, int size, int esize, int i, int k, int (*compare) 20 | (const void *key1, const void *key2)); 21 | 22 | int mgsort(void *data, int size, int esize, int i, int k, int (*compare) 23 | (const void *key1, const void *key2)); 24 | 25 | int ctsort(int *data, int size, int k); 26 | 27 | int rxsort(int *data, int size, int p, int k); 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples_unix/include/spell.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- spell.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SPELL_H 8 | #define SPELL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define the maximum size for words in the dictionary. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | #define SPELL_SIZE 31 17 | 18 | /***************************************************************************** 19 | * * 20 | * --------------------------- Public Interface --------------------------- * 21 | * * 22 | *****************************************************************************/ 23 | 24 | int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /examples_unix/include/stack.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- stack.h -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | #include 11 | 12 | #include "list.h" 13 | 14 | /***************************************************************************** 15 | * * 16 | * Implement stacks as linked lists. * 17 | * * 18 | *****************************************************************************/ 19 | 20 | typedef List Stack; 21 | 22 | /***************************************************************************** 23 | * * 24 | * --------------------------- Public Interface --------------------------- * 25 | * * 26 | *****************************************************************************/ 27 | 28 | #define stack_init list_init 29 | 30 | #define stack_destroy list_destroy 31 | 32 | int stack_push(Stack *stack, const void *data); 33 | 34 | int stack_pop(Stack *stack, void **data); 35 | 36 | #define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data) 37 | 38 | #define stack_size list_size 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /examples_unix/include/symbol.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- symbol.h ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef SYMBOL_H 8 | #define SYMBOL_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * Define next_token for demonstration purposes. * 13 | * * 14 | *****************************************************************************/ 15 | 16 | static char *next_token(const char *istream) { 17 | 18 | return NULL; 19 | 20 | } 21 | 22 | /***************************************************************************** 23 | * * 24 | * Define a symbol structure for demonstration purposes. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | typedef struct Symbol_ { 29 | 30 | char *lexeme; 31 | Token token; 32 | 33 | } Symbol; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /examples_unix/include/transfer.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ transfer.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef TRANSFER_H 8 | #define TRANSFER_H 9 | 10 | /***************************************************************************** 11 | * * 12 | * --------------------------- Public Interface --------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int send_comp(int s, const unsigned char *data, int size, int flags); 17 | 18 | int recv_comp(int s, unsigned char **data, int *size, int flags); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /examples_unix/include/traverse.h: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ traverse.h ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #ifndef TRAVERSE_H 8 | #define TRAVERSE_H 9 | 10 | #include "bitree.h" 11 | #include "list.h" 12 | 13 | /***************************************************************************** 14 | * * 15 | * --------------------------- Public Interface --------------------------- * 16 | * * 17 | *****************************************************************************/ 18 | 19 | int preorder(const BiTreeNode *node, List *list); 20 | 21 | int inorder(const BiTreeNode *node, List *list); 22 | 23 | int postorder(const BiTreeNode *node, List *list); 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /examples_unix/source/arclen.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- arclen.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "geometry.h" 10 | 11 | /***************************************************************************** 12 | * * 13 | * -------------------------------- arclen -------------------------------- * 14 | * * 15 | *****************************************************************************/ 16 | 17 | void arclen(SPoint p1, SPoint p2, double *length) { 18 | 19 | Point p1_rct, 20 | p2_rct; 21 | 22 | double alpha, 23 | dot; 24 | 25 | /***************************************************************************** 26 | * * 27 | * Convert the spherical coordinates to rectilinear coordinates. * 28 | * * 29 | *****************************************************************************/ 30 | 31 | p1_rct.x = p1.rho * sin(p1.phi) * cos(p1.theta); 32 | p1_rct.y = p1.rho * sin(p1.phi) * sin(p1.theta); 33 | p1_rct.z = p1.rho * cos(p1.phi); 34 | 35 | p2_rct.x = p2.rho * sin(p2.phi) * cos(p2.theta); 36 | p2_rct.y = p2.rho * sin(p2.phi) * sin(p2.theta); 37 | p2_rct.z = p2.rho * cos(p2.phi); 38 | 39 | /***************************************************************************** 40 | * * 41 | * Get the angle between the line segments from the origin to each point. * 42 | * * 43 | *****************************************************************************/ 44 | 45 | dot = (p1_rct.x * p2_rct.x) + (p1_rct.y * p2_rct.y) + (p1_rct.z * p2_rct.z); 46 | alpha = acos(dot / pow(p1.rho, 2.0)); 47 | 48 | /***************************************************************************** 49 | * * 50 | * Compute the length of the arc along the spherical surface. * 51 | * * 52 | *****************************************************************************/ 53 | 54 | *length = alpha * p1.rho; 55 | 56 | return; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /examples_unix/source/fact.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- fact.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "fact.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * --------------------------------- fact --------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int fact(int n) { 16 | 17 | /***************************************************************************** 18 | * * 19 | * Compute a factorial recursively. * 20 | * * 21 | *****************************************************************************/ 22 | 23 | if (n < 0) 24 | return 0; 25 | else if (n == 0) 26 | return 1; 27 | else if (n == 1) 28 | return 1; 29 | else 30 | return n * fact(n - 1); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /examples_unix/source/factor.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- factor.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | #include 9 | 10 | #include "factor.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * -------------------------------- factor -------------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | void factor(int number, int n, int j) { 19 | 20 | int i; 21 | 22 | /***************************************************************************** 23 | * * 24 | * 1 is neither prime nor composite. * 25 | * * 26 | *****************************************************************************/ 27 | 28 | if (n == 1) { 29 | 30 | printf("1 is a unit\n"); 31 | return; 32 | 33 | } 34 | 35 | /***************************************************************************** 36 | * * 37 | * Determine the prime factors of n. * 38 | * * 39 | *****************************************************************************/ 40 | 41 | i = j; 42 | 43 | while (i <= (int)(sqrt((double)n))) { 44 | 45 | if (n % i == 0) { 46 | 47 | /*********************************************************************** 48 | * * 49 | * We have found a prime factor of n. Print it and factor n / i. * 50 | * * 51 | ***********************************************************************/ 52 | 53 | fprintf(stdout, "%d\n", i); 54 | factor(number, (int)(n / i), i); 55 | return; 56 | 57 | } 58 | 59 | else { 60 | 61 | i++; 62 | 63 | } 64 | 65 | } 66 | 67 | /***************************************************************************** 68 | * * 69 | * If this point is reached, n is prime. * 70 | * * 71 | *****************************************************************************/ 72 | 73 | if (n == number) 74 | printf("%d is prime\n", number); 75 | else 76 | printf("%d\n", n); 77 | 78 | return; 79 | 80 | } 81 | -------------------------------------------------------------------------------- /examples_unix/source/facttail.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------ facttail.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "facttail.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * ------------------------------- facttail ------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int facttail(int n, int a) { 16 | 17 | /***************************************************************************** 18 | * * 19 | * Compute a factorial in a tail-recursive manner. * 20 | * * 21 | *****************************************************************************/ 22 | 23 | if (n < 0) 24 | return 0; 25 | else if (n == 0) 26 | return 1; 27 | else if (n == 1) 28 | return a; 29 | else 30 | return facttail(n - 1, n * a); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /examples_unix/source/geodist.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- geodist.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "geodist.h" 8 | #include "geometry.h" 9 | 10 | /***************************************************************************** 11 | * * 12 | * -------------------------------- geodist ------------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int geodist(double lat1, double lon1, double lat2, double lon2, double *d) { 17 | 18 | SPoint p1, 19 | p2; 20 | 21 | /***************************************************************************** 22 | * * 23 | * Validate the coordinates. * 24 | * * 25 | *****************************************************************************/ 26 | 27 | if (lat1 < -90.0 || lat1 > 90.0 || lat2 < -90.0 || lat2 > 90.0) 28 | return -1; 29 | 30 | if (lon1 < -180.0 || lon1 > 180.0 || lon2 < -180.0 || lon2 > 180.0) 31 | return -1; 32 | 33 | /***************************************************************************** 34 | * * 35 | * Convert each latitude and longitude to spherical coordinates in radians * 36 | * using the earth's radius for rho. * 37 | * * 38 | *****************************************************************************/ 39 | 40 | p1.rho = EARTH_RADIUS; 41 | p1.theta = -1.0 * DEGTORAD(lon1); 42 | p1.phi = (DEGTORAD(-1.0 * lat1)) + DEGTORAD(90.0); 43 | 44 | p2.rho = EARTH_RADIUS; 45 | p2.theta = -1.0 * DEGTORAD(lon2); 46 | p2.phi = (DEGTORAD(-1.0 * lat2)) + DEGTORAD(90.0); 47 | 48 | /***************************************************************************** 49 | * * 50 | * Compute the distance between the points. * 51 | * * 52 | *****************************************************************************/ 53 | 54 | arclen(p1, p2, d); 55 | 56 | return 0; 57 | 58 | } 59 | -------------------------------------------------------------------------------- /examples_unix/source/hashpjw.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- hashpjw.c ------------------------------ * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "hashpjw.h" 8 | 9 | /***************************************************************************** 10 | * * 11 | * -------------------------------- hashpjw ------------------------------- * 12 | * * 13 | *****************************************************************************/ 14 | 15 | int hashpjw(const void *key) { 16 | 17 | const char *ptr; 18 | 19 | int val; 20 | 21 | /***************************************************************************** 22 | * * 23 | * Hash the key by performing a number of bit operations on it. * 24 | * * 25 | *****************************************************************************/ 26 | 27 | val = 0; 28 | ptr = key; 29 | 30 | while (*ptr != '\0') { 31 | 32 | int tmp; 33 | 34 | val = (val << 4) + (*ptr); 35 | 36 | if (tmp = (val & 0xf0000000)) { 37 | 38 | val = val ^ (tmp >> 24); 39 | val = val ^ tmp; 40 | 41 | } 42 | 43 | ptr++; 44 | 45 | } 46 | 47 | /***************************************************************************** 48 | * * 49 | * In practice, replace PRIME_TBLSIZ with the actual table size. * 50 | * * 51 | *****************************************************************************/ 52 | 53 | return val % PRIME_TBLSIZ; 54 | 55 | } 56 | -------------------------------------------------------------------------------- /examples_unix/source/issort.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- issort.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | #include 9 | 10 | #include "sort.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * -------------------------------- issort -------------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int issort(void *data, int size, int esize, int (*compare)(const void *key1, 19 | const void *key2)) { 20 | 21 | char *a = data; 22 | 23 | void *key; 24 | 25 | int i, 26 | j; 27 | 28 | /***************************************************************************** 29 | * * 30 | * Allocate storage for the key element. * 31 | * * 32 | *****************************************************************************/ 33 | 34 | if ((key = (char *)malloc(esize)) == NULL) 35 | return -1; 36 | 37 | /***************************************************************************** 38 | * * 39 | * Repeatedly insert a key element among the sorted elements. * 40 | * * 41 | *****************************************************************************/ 42 | 43 | for (j = 1; j < size; j++) { 44 | 45 | memcpy(key, &a[j * esize], esize); 46 | i = j - 1; 47 | 48 | /************************************************************************** 49 | * * 50 | * Determine the position at which to insert the key element. * 51 | * * 52 | **************************************************************************/ 53 | 54 | while (i >= 0 && compare(&a[i * esize], key) > 0) { 55 | 56 | memcpy(&a[(i + 1) * esize], &a[i * esize], esize); 57 | i--; 58 | 59 | } 60 | 61 | memcpy(&a[(i + 1) * esize], key, esize); 62 | 63 | } 64 | 65 | /***************************************************************************** 66 | * * 67 | * Free the storage allocated for sorting. * 68 | * * 69 | *****************************************************************************/ 70 | 71 | free(key); 72 | 73 | return 0; 74 | 75 | } 76 | -------------------------------------------------------------------------------- /examples_unix/source/lsqe.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- lsqe.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "nummeths.h" 10 | 11 | /***************************************************************************** 12 | * * 13 | * --------------------------------- lsqe --------------------------------- * 14 | * * 15 | *****************************************************************************/ 16 | 17 | void lsqe(const double *x, const double *y, int n, double *b1, double *b0) { 18 | 19 | double sumx, 20 | sumy, 21 | sumx2, 22 | sumxy; 23 | 24 | int i; 25 | 26 | /***************************************************************************** 27 | * * 28 | * Compute the required summations. * 29 | * * 30 | *****************************************************************************/ 31 | 32 | sumx = 0.0; 33 | sumy = 0.0; 34 | sumx2 = 0.0; 35 | sumxy = 0.0; 36 | 37 | for (i = 0; i < n; i++) { 38 | 39 | sumx = sumx + x[i]; 40 | sumy = sumy + y[i]; 41 | sumx2 = sumx2 + pow(x[i], 2.0); 42 | sumxy = sumxy + (x[i] * y[i]); 43 | 44 | } 45 | 46 | /***************************************************************************** 47 | * * 48 | * Compute the least-squares estimators. * 49 | * * 50 | *****************************************************************************/ 51 | 52 | *b1 = (sumxy - ((sumx * sumy)/(double)n)) / (sumx2-(pow(sumx,2.0)/(double)n)); 53 | *b0 = (sumy - ((*b1) * sumx)) / (double)n; 54 | 55 | return; 56 | 57 | } 58 | -------------------------------------------------------------------------------- /examples_unix/source/page.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- page.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include "clist.h" 8 | #include "page.h" 9 | 10 | /***************************************************************************** 11 | * * 12 | * ----------------------------- replace_page ----------------------------- * 13 | * * 14 | *****************************************************************************/ 15 | 16 | int replace_page(CListElmt **current) { 17 | 18 | /***************************************************************************** 19 | * * 20 | * Circle through the list of pages until one is found to replace. * 21 | * * 22 | *****************************************************************************/ 23 | 24 | while (((Page *)(*current)->data)->reference != 0) { 25 | 26 | ((Page *)(*current)->data)->reference = 0; 27 | *current = clist_next(*current); 28 | 29 | } 30 | 31 | return ((Page *)(*current)->data)->number; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /examples_unix/source/queue.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- queue.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "list.h" 10 | #include "queue.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ----------------------------- queue_enqueue ---------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int queue_enqueue(Queue *queue, const void *data) { 19 | 20 | /***************************************************************************** 21 | * * 22 | * Enqueue the data. * 23 | * * 24 | *****************************************************************************/ 25 | 26 | return list_ins_next(queue, list_tail(queue), data); 27 | 28 | } 29 | 30 | /***************************************************************************** 31 | * * 32 | * ----------------------------- queue_dequeue ---------------------------- * 33 | * * 34 | *****************************************************************************/ 35 | 36 | int queue_dequeue(Queue *queue, void **data) { 37 | 38 | /***************************************************************************** 39 | * * 40 | * Dequeue the data. * 41 | * * 42 | *****************************************************************************/ 43 | 44 | return list_rem_next(queue, NULL, data); 45 | 46 | } 47 | -------------------------------------------------------------------------------- /examples_unix/source/spell.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * -------------------------------- spell.c ------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "search.h" 10 | #include "spell.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ------------------------------ compare_str ----------------------------- * 15 | * * 16 | *****************************************************************************/ 17 | 18 | static int compare_str(const void *str1, const void *str2) { 19 | 20 | int retval; 21 | 22 | if ((retval = strcmp((const char *)str1, (const char *)str2)) > 0) 23 | return 1; 24 | else if (retval < 0) 25 | return -1; 26 | else 27 | return 0; 28 | 29 | } 30 | 31 | /***************************************************************************** 32 | * * 33 | * --------------------------------- spell -------------------------------- * 34 | * * 35 | *****************************************************************************/ 36 | 37 | int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word) { 38 | 39 | /***************************************************************************** 40 | * * 41 | * Look up the word. * 42 | * * 43 | *****************************************************************************/ 44 | 45 | if (bisearch(dictionary, word, size, SPELL_SIZE, compare_str) >= 0) 46 | return 1; 47 | else 48 | return 0; 49 | 50 | } 51 | -------------------------------------------------------------------------------- /examples_unix/source/stack.c: -------------------------------------------------------------------------------- 1 | /***************************************************************************** 2 | * * 3 | * ------------------------------- stack.c -------------------------------- * 4 | * * 5 | *****************************************************************************/ 6 | 7 | #include 8 | 9 | #include "list.h" 10 | #include "stack.h" 11 | 12 | /***************************************************************************** 13 | * * 14 | * ------------------------------ stack_push ------------------------------ * 15 | * * 16 | *****************************************************************************/ 17 | 18 | int stack_push(Stack *stack, const void *data) { 19 | 20 | /***************************************************************************** 21 | * * 22 | * Push the data onto the stack. * 23 | * * 24 | *****************************************************************************/ 25 | 26 | return list_ins_next(stack, NULL, data); 27 | 28 | } 29 | 30 | /***************************************************************************** 31 | * * 32 | * ------------------------------ stack_pop ------------------------------- * 33 | * * 34 | *****************************************************************************/ 35 | 36 | int stack_pop(Stack *stack, void **data) { 37 | 38 | /***************************************************************************** 39 | * * 40 | * Pop the data off the stack. * 41 | * * 42 | *****************************************************************************/ 43 | 44 | return list_rem_next(stack, NULL, data); 45 | 46 | } 47 | --------------------------------------------------------------------------------