├── .gitattributes ├── .gitignore ├── HashTable ├── hashTable.cpp └── hashTable.hpp ├── HuffmanCode ├── huffmanCode.hpp └── huffmanTree.cpp ├── LICENSE ├── PID ├── PID.cpp └── PID.hpp ├── README.md └── circularQueue └── circularQueue.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | .vscode/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # StyleCop 66 | StyleCopReport.xml 67 | 68 | # Files built by Visual Studio 69 | *_i.c 70 | *_p.c 71 | *_h.h 72 | *.ilk 73 | *.meta 74 | *.obj 75 | *.iobj 76 | *.pch 77 | *.pdb 78 | *.ipdb 79 | *.pgc 80 | *.pgd 81 | *.rsp 82 | *.sbr 83 | *.tlb 84 | *.tli 85 | *.tlh 86 | *.tmp 87 | *.tmp_proj 88 | *_wpftmp.csproj 89 | *.log 90 | *.vspscc 91 | *.vssscc 92 | .builds 93 | *.pidb 94 | *.svclog 95 | *.scc 96 | 97 | # Chutzpah Test files 98 | _Chutzpah* 99 | 100 | # Visual C++ cache files 101 | ipch/ 102 | *.aps 103 | *.ncb 104 | *.opendb 105 | *.opensdf 106 | *.sdf 107 | *.cachefile 108 | *.VC.db 109 | *.VC.VC.opendb 110 | 111 | # Visual Studio profiler 112 | *.psess 113 | *.vsp 114 | *.vspx 115 | *.sap 116 | 117 | # Visual Studio Trace Files 118 | *.e2e 119 | 120 | # TFS 2012 Local Workspace 121 | $tf/ 122 | 123 | # Guidance Automation Toolkit 124 | *.gpState 125 | 126 | # ReSharper is a .NET coding add-in 127 | _ReSharper*/ 128 | *.[Rr]e[Ss]harper 129 | *.DotSettings.user 130 | 131 | # JustCode is a .NET coding add-in 132 | .JustCode 133 | 134 | # TeamCity is a build add-in 135 | _TeamCity* 136 | 137 | # DotCover is a Code Coverage Tool 138 | *.dotCover 139 | 140 | # AxoCover is a Code Coverage Tool 141 | .axoCover/* 142 | !.axoCover/settings.json 143 | 144 | # Visual Studio code coverage results 145 | *.coverage 146 | *.coveragexml 147 | 148 | # NCrunch 149 | _NCrunch_* 150 | .*crunch*.local.xml 151 | nCrunchTemp_* 152 | 153 | # MightyMoose 154 | *.mm.* 155 | AutoTest.Net/ 156 | 157 | # Web workbench (sass) 158 | .sass-cache/ 159 | 160 | # Installshield output folder 161 | [Ee]xpress/ 162 | 163 | # DocProject is a documentation generator add-in 164 | DocProject/buildhelp/ 165 | DocProject/Help/*.HxT 166 | DocProject/Help/*.HxC 167 | DocProject/Help/*.hhc 168 | DocProject/Help/*.hhk 169 | DocProject/Help/*.hhp 170 | DocProject/Help/Html2 171 | DocProject/Help/html 172 | 173 | # Click-Once directory 174 | publish/ 175 | 176 | # Publish Web Output 177 | *.[Pp]ublish.xml 178 | *.azurePubxml 179 | # Note: Comment the next line if you want to checkin your web deploy settings, 180 | # but database connection strings (with potential passwords) will be unencrypted 181 | *.pubxml 182 | *.publishproj 183 | 184 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 185 | # checkin your Azure Web App publish settings, but sensitive information contained 186 | # in these scripts will be unencrypted 187 | PublishScripts/ 188 | 189 | # NuGet Packages 190 | *.nupkg 191 | # NuGet Symbol Packages 192 | *.snupkg 193 | # The packages folder can be ignored because of Package Restore 194 | **/[Pp]ackages/* 195 | # except build/, which is used as an MSBuild target. 196 | !**/[Pp]ackages/build/ 197 | # Uncomment if necessary however generally it will be regenerated when needed 198 | #!**/[Pp]ackages/repositories.config 199 | # NuGet v3's project.json files produces more ignorable files 200 | *.nuget.props 201 | *.nuget.targets 202 | 203 | # Microsoft Azure Build Output 204 | csx/ 205 | *.build.csdef 206 | 207 | # Microsoft Azure Emulator 208 | ecf/ 209 | rcf/ 210 | 211 | # Windows Store app package directories and files 212 | AppPackages/ 213 | BundleArtifacts/ 214 | Package.StoreAssociation.xml 215 | _pkginfo.txt 216 | *.appx 217 | *.appxbundle 218 | *.appxupload 219 | 220 | # Visual Studio cache files 221 | # files ending in .cache can be ignored 222 | *.[Cc]ache 223 | # but keep track of directories ending in .cache 224 | !?*.[Cc]ache/ 225 | 226 | # Others 227 | ClientBin/ 228 | ~$* 229 | *~ 230 | *.dbmdl 231 | *.dbproj.schemaview 232 | *.jfm 233 | *.pfx 234 | *.publishsettings 235 | orleans.codegen.cs 236 | 237 | # Including strong name files can present a security risk 238 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 239 | #*.snk 240 | 241 | # Since there are multiple workflows, uncomment next line to ignore bower_components 242 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 243 | #bower_components/ 244 | 245 | # RIA/Silverlight projects 246 | Generated_Code/ 247 | 248 | # Backup & report files from converting an old project file 249 | # to a newer Visual Studio version. Backup files are not needed, 250 | # because we have git ;-) 251 | _UpgradeReport_Files/ 252 | Backup*/ 253 | UpgradeLog*.XML 254 | UpgradeLog*.htm 255 | ServiceFabricBackup/ 256 | *.rptproj.bak 257 | 258 | # SQL Server files 259 | *.mdf 260 | *.ldf 261 | *.ndf 262 | 263 | # Business Intelligence projects 264 | *.rdl.data 265 | *.bim.layout 266 | *.bim_*.settings 267 | *.rptproj.rsuser 268 | *- [Bb]ackup.rdl 269 | *- [Bb]ackup ([0-9]).rdl 270 | *- [Bb]ackup ([0-9][0-9]).rdl 271 | 272 | # Microsoft Fakes 273 | FakesAssemblies/ 274 | 275 | # GhostDoc plugin setting file 276 | *.GhostDoc.xml 277 | 278 | # Node.js Tools for Visual Studio 279 | .ntvs_analysis.dat 280 | node_modules/ 281 | 282 | # Visual Studio 6 build log 283 | *.plg 284 | 285 | # Visual Studio 6 workspace options file 286 | *.opt 287 | 288 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 289 | *.vbw 290 | 291 | # Visual Studio LightSwitch build output 292 | **/*.HTMLClient/GeneratedArtifacts 293 | **/*.DesktopClient/GeneratedArtifacts 294 | **/*.DesktopClient/ModelManifest.xml 295 | **/*.Server/GeneratedArtifacts 296 | **/*.Server/ModelManifest.xml 297 | _Pvt_Extensions 298 | 299 | # Paket dependency manager 300 | .paket/paket.exe 301 | paket-files/ 302 | 303 | # FAKE - F# Make 304 | .fake/ 305 | 306 | # CodeRush personal settings 307 | .cr/personal 308 | 309 | # Python Tools for Visual Studio (PTVS) 310 | __pycache__/ 311 | *.pyc 312 | 313 | # Cake - Uncomment if you are using it 314 | # tools/** 315 | # !tools/packages.config 316 | 317 | # Tabs Studio 318 | *.tss 319 | 320 | # Telerik's JustMock configuration file 321 | *.jmconfig 322 | 323 | # BizTalk build output 324 | *.btp.cs 325 | *.btm.cs 326 | *.odx.cs 327 | *.xsd.cs 328 | 329 | # OpenCover UI analysis results 330 | OpenCover/ 331 | 332 | # Azure Stream Analytics local run output 333 | ASALocalRun/ 334 | 335 | # MSBuild Binary and Structured Log 336 | *.binlog 337 | 338 | # NVidia Nsight GPU debugger configuration file 339 | *.nvuser 340 | 341 | # MFractors (Xamarin productivity tool) working folder 342 | .mfractor/ 343 | 344 | # Local History for Visual Studio 345 | .localhistory/ 346 | 347 | # BeatPulse healthcheck temp database 348 | healthchecksdb 349 | 350 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 351 | MigrationBackup/ 352 | 353 | # Ionide (cross platform F# VS Code tools) working folder 354 | .ionide/ 355 | -------------------------------------------------------------------------------- /HashTable/hashTable.cpp: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /HashTable/hashTable.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * hashTable.hpp - The C++ head file of 哈希树 3 | * NOTE: This file is based on C++11 4 | * 5 | * Copyright (c) 2020-, FOSH Project 6 | * 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Change Logs: 10 | * Date Author Notes mail 11 | * 2020-06-14 StudyHooligen first version 2373180028@qq.com 12 | */ 13 | #pragma once 14 | #include 15 | #include 16 | #include 17 | 18 | namespace hash { 19 | //采用除留余数法构造哈希表 20 | //冲突解决方案为:伪随机探测再散列法 21 | 22 | typedef struct hashTable 23 | { 24 | std::vector data; //哈希表中存储的数据 25 | 26 | int hashMod; //哈希函数除数 27 | std::vector hashRN; //哈希函数冲突时使用的随机数序列 28 | }hashTable,* hashTablePtr; 29 | 30 | /* 函数功能:产生哈希表 31 | * 参数1:建立的查找数据 32 | * 参数2:数据量 33 | * 参数3:用户给定的哈希除数摸 34 | * 返回值:建立好的哈希存储表指针 35 | */ 36 | hashTablePtr hashCreate(std::vector inputData, int size,int hashMod); 37 | 38 | /* 函数功能:在指定哈希表中查找数据 39 | * 参数1:建立好的哈希表指针 40 | * 参数2:查找的数据 41 | * 参数3:查找的数据结果存储地址 42 | * 返回值:查找结果成功(Bool:true)或者失败(false) 43 | */ 44 | bool hashSearch(const hashTablePtr sourceTable, const std::string targetID, std::string* resault); 45 | 46 | 47 | /**********************开始函数定义*******************************/ 48 | hashTablePtr hashCreate(std::vector inputData, int size, int hashMod) { 49 | //建立哈希表(动态内存) 50 | hashTablePtr hashT = new hashTable; 51 | hashT->hashMod = hashMod; 52 | hashT->data.resize(size*2); //最坏情况下,两倍内存空间 53 | 54 | srand(time(0)); //随机数发生器初始化 55 | 56 | for (int i = 0,key = 0,H_key; i < size; i++) { 57 | key = inputData[i].front() + inputData[i].back(); //提取关键字 58 | 59 | H_key = key % hashMod; //哈希函数 60 | 61 | if ((hashT->data)[H_key].empty()) { //不冲突 62 | (hashT->data)[H_key] = inputData[i]; //存入数据 63 | //std::cout << "insert(no conflict) :" << inputData[i] //调试用 64 | // << " to " << H_key << std::endl; 65 | } 66 | else { 67 | int j = 0; 68 | while (j < hashT->hashRN.size() && hashT->hashRN[j]) { 69 | H_key = (key + hashT->hashRN[j]) % hashMod; //哈希冲突函数 70 | if ((hashT->data)[H_key].empty()) { //不冲突 71 | (hashT->data)[H_key] = inputData[i]; //存入数据 72 | //std::cout << "insert(conflict) :" << inputData[i] //调试用 73 | // << " to " << H_key << " with random: " 74 | // << hashT->hashRN[j] << std::endl; 75 | j = -1; //退出循环标志位 76 | break; 77 | } 78 | else { 79 | //发生冲突 80 | j++; 81 | continue; 82 | } 83 | } 84 | if (j == -1) continue; 85 | else { 86 | while (1) 87 | { 88 | int RN = rand(); //生成随机数 89 | 90 | H_key = (key + RN) % hashMod; //哈希冲突函数 91 | 92 | if ((hashT->data)[H_key].empty()) { //不冲突 93 | (hashT->data)[H_key] = inputData[i]; //存入数据 94 | hashT->hashRN.push_back(RN); //该有效随机数存入随机数表 95 | //std::cout << "insert(conflict) :" << inputData[i] //调试用 96 | // << " to " << H_key << " with random(create): " 97 | // << RN << std::endl; 98 | break; 99 | } 100 | //如果冲突,则重新生成随机数,直到找到有效随机数为止 101 | } 102 | } 103 | } 104 | } 105 | return hashT; 106 | } 107 | 108 | bool hashSearch(const hashTablePtr sourceTable, const std::string targetID, std::string* resault) { 109 | int key = targetID.front() + targetID.back(); //提取关键值 110 | int H_key = key % sourceTable->hashMod; //哈希函数 111 | if (sourceTable->data[H_key] == targetID) { 112 | *resault = sourceTable->data[H_key]; //结果返回 113 | return true; //成功查找 114 | } 115 | else { 116 | for (auto i : sourceTable->hashRN) { 117 | H_key = (key + i) % sourceTable->hashMod; //哈希冲突函数 118 | if (sourceTable->data[H_key] == targetID) { 119 | *resault = sourceTable->data[H_key]; //结果返回 120 | return true; //查找成功 121 | } 122 | } 123 | return false; //查找失败 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /HuffmanCode/huffmanCode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * huffmanCode.hpp - The C++ head file of 哈夫曼树 3 | * NOTE: This file is based on C++11 4 | * 5 | * Copyright (c) 2020-, FOSH Project 6 | * 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Change Logs: 10 | * Date Author Notes mail 11 | * 2020-06-14 StudyHooligen first version 2373180028@qq.com 12 | */ 13 | #ifndef _HuffmanCode_H_ 14 | 15 | #define _HuffmanCode_H_ 16 | #include 17 | #include 18 | 19 | namespace huffman{ 20 | 21 | typedef struct huffmanTree 22 | { 23 | int weight; //节点权重 24 | char data; //节点数据 25 | struct huffmanTree* Parent; //父节点 26 | struct huffmanTree * Ln,* Rn; //左右子节点 27 | 28 | }huffmanTree,*huffmanTreePtr; 29 | 30 | typedef struct huffmanCodeSet 31 | { 32 | char data; //编码源字符 33 | std::string huffmanCode; //编码密码 34 | struct huffmanCodeSet* next; //下一个节点 35 | 36 | }huffmanCodeSet,* huffmanCodeSetPtr; 37 | 38 | typedef struct huffmanInitNode { 39 | char data; //字符数据 40 | int weight; //权重 41 | }huffmanInitNode,* huffmanInitNodePtr; 42 | 43 | /* 初始化(建立)哈夫曼树 44 | * 参数1:编码集元素个数 45 | * 参数2:编码集头指针 46 | * 参数3:生成的哈夫曼树存储文件位置 47 | * 输出: 生成于内存中的哈夫曼树根节点 48 | */ 49 | huffmanTreePtr HuffmanInit(int setSize, huffmanInitNodePtr setPtr, std::string location); 50 | 51 | /* 哈夫曼编码密码本生成 52 | * 参数1:哈夫曼树 53 | * 输出: 哈夫曼密码本 54 | */ 55 | huffmanCodeSetPtr HuffmanCodeSetInit(huffmanTreePtr sourceTree); 56 | 57 | /* 哈夫曼编码 58 | * 参数1:哈夫曼编码密码本 59 | * 参数2:待编码文件内容 60 | * 参数3:编码后文件内容 61 | * 输出: 编码成功(true)或失败(false) 62 | */ 63 | bool HuffmanEncoding(huffmanCodeSetPtr codeSet, 64 | std::string sourceContent, std::string& finishContent); 65 | 66 | 67 | /* 哈夫曼解码 68 | * 参数1:哈夫曼树头 69 | * 参数2:待解码文件位置 70 | * 参数3:解码后文件存储位置 71 | * 输出: 编码成功(true)或失败(false) 72 | */ 73 | bool HuffmanDecoding(huffmanTreePtr sourceTree, 74 | std::string sourceContent, std::string& finishContent); 75 | 76 | /* 哈夫曼编码打印 77 | * 参数1:编码后文件内容 78 | * 参数2:存储文件路径 79 | * 输出: 无 80 | */ 81 | void codeTxtPrint(std::string sourceContent,std::string saveLocation); 82 | 83 | /* 哈夫曼树打印 84 | * 参数1:哈夫曼树头 85 | * 输出: 无 86 | */ 87 | void HuffmanTreePrint(huffmanTreePtr sourceTree); 88 | 89 | //namespace end 90 | } 91 | 92 | #endif -------------------------------------------------------------------------------- /HuffmanCode/huffmanTree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * huffmanCode.cpp - The C++ head file of 哈夫曼树 3 | * NOTE: This file is based on C++11 4 | * 5 | * Copyright (c) 2020-, FOSH Project 6 | * 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Change Logs: 10 | * Date Author Notes mail 11 | * 2020-06-14 StudyHooligen first version 2373180028@qq.com 12 | */ 13 | #include "HuffmanCode.hpp" 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | namespace huffman { 20 | 21 | static bool huffmanInitNodeCompare(huffmanInitNode d1, huffmanInitNode d2){ 22 | return d1.weight < d2.weight; 23 | } 24 | 25 | huffmanTreePtr HuffmanInit(int setSize, huffmanInitNodePtr setPtr, std::string location){ 26 | //�����������ַ������� 27 | std::sort(setPtr, setPtr + setSize, huffmanInitNodeCompare); 28 | 29 | //��ӡ�������������ã� 30 | std::cout << std::endl << "after sort:" << std::endl; 31 | for (int i = 0; i < setSize; i++) { 32 | std::cout << i + 1 << "st.data :" << setPtr[i].data 33 | <<". weight:"<data = setPtr[0].data; 39 | root->weight = setPtr[0].weight; 40 | root->Parent = root->Ln = root->Rn = nullptr; 41 | huffmanTreePtr endPtr = root; 42 | for (int i = 1; i < setSize; i++) { 43 | endPtr->Parent = new huffmanTree; 44 | endPtr = endPtr->Parent; 45 | 46 | endPtr->data = setPtr[i].data; 47 | endPtr->weight = setPtr[i].weight; 48 | endPtr->Parent = endPtr->Rn = endPtr->Ln = nullptr; 49 | } 50 | 51 | //��ӡ������������������ã� 52 | std::cout << std::endl << "convert to Tree linkList:" << std::endl; 53 | for (huffmanTreePtr i = root; i != endPtr; i = i->Parent) 54 | std::cout << "data:" << i->data << ". weight:" << i->weight << std::endl; 55 | std::cout << "data:" << endPtr->data << ". weight:" << endPtr->weight << std::endl; 56 | 57 | //������δ�����Ľڵ㲢��ʱ 58 | while (root->Parent) { 59 | //ȥ����С������Ԫ�غ󣬽ڵ��ַ 60 | auto nextRoot = root->Parent->Parent; 61 | 62 | //����СȨ�ص������ڵ���ȡ�������γ��½ڵ� 63 | auto newNode = new huffmanTree; //�����ڵ� 64 | newNode->Ln = root; //��С�ķ�����Ҷ 65 | newNode->Rn = root->Parent; ///�ڶ�С�ķ�����Ҷ 66 | newNode->Parent = nextRoot; 67 | newNode->weight = root->weight + root->Parent->weight; //Ȩ��Ϊ��Ҷ�� 68 | newNode->data = 0; 69 | 70 | //ת��Ϊ��Ҷ�Ľڵ㣬��ָ���Ϊ�������½ڵ� 71 | root->Parent->Parent = newNode; 72 | root->Parent = newNode; 73 | 74 | //���û��δ����ڵ��ˣ�ֱ�ӷ��� 75 | if (!nextRoot) return newNode; 76 | 77 | if (newNode->weight < nextRoot->weight) { 78 | root = newNode; 79 | continue; //�Ѿ�Ĭ��Ϊ�ź�˳��״̬ 80 | } 81 | else { 82 | auto insertPtr = nextRoot;//����λ�� 83 | while (insertPtr->Parent && (insertPtr->Parent->weight < newNode->weight) ) 84 | { 85 | insertPtr = insertPtr->Parent; 86 | } 87 | //����ڵ� 88 | newNode->Parent = insertPtr->Parent; 89 | insertPtr->Parent = newNode; 90 | root = nextRoot; 91 | } 92 | 93 | } 94 | //������ɣ����ؽڵ� 95 | return root; 96 | } 97 | 98 | 99 | void HuffmanTreePrint(huffmanTreePtr sourceTree) { 100 | 101 | typedef struct printHuffmanTreeNodeLink 102 | { //��ȱ����������������ṹ 103 | huffmanTreePtr node; //���ڵ� 104 | struct printHuffmanTreeNodeLink * next; 105 | }nodeLink,*nodeLinkPtr; 106 | 107 | typedef struct printHuffmanTreeCountLink 108 | { //��ǰ��������ڵ�����������ṹ 109 | int count; //���� 110 | struct printHuffmanTreeCountLink* next; 111 | }countLink,* countLinkPtr; 112 | 113 | //��ʼ��������Ϊ����ͷ 114 | nodeLinkPtr nodeListHead = new nodeLink; 115 | nodeListHead->next = nullptr; 116 | nodeListHead->node = sourceTree; 117 | nodeLinkPtr nodeListTail = nodeListHead; 118 | 119 | //��ʼ����ǰ��ӡ�еĽڵ����Ϊ1 120 | countLinkPtr countList = new countLink; 121 | countList->count = 1; 122 | //ѭ����������һ�������ڵ�洢������һ��Ľڵ���� 123 | countList->next = new countLink; 124 | countList->next->next = countList; 125 | countList->next->count = 0; 126 | 127 | //ֻҪ��ǰ���ǿ�Ԫ������ 128 | while (countList->count) 129 | { 130 | auto lineCount = countList->count;//��Ҫ��ӡ�IJ�ڵ���� 131 | 132 | //����ڵ��ӡ 133 | while (lineCount-- && nodeListHead) { 134 | //��ӡ�ڵ����ݣ�Ȩ�� 135 | std::cout << "Data: " << nodeListHead->node->data 136 | << ",Weight: " << nodeListHead->node->weight 137 | << ". "; 138 | 139 | //����Ҷ��������ڣ������ӡ����β 140 | if (nodeListHead->node->Ln) { 141 | nodeListTail->next = new nodeLink; 142 | nodeListTail = nodeListTail->next; 143 | nodeListTail->node = nodeListHead->node->Ln; 144 | nodeListTail->next = nullptr; 145 | 146 | //������һ���ӡ���� 147 | countList->next->count++; 148 | } 149 | 150 | //����Ҷ��������ڣ������ӡ����β 151 | if (nodeListHead->node->Rn) { 152 | nodeListTail->next = new nodeLink; 153 | nodeListTail = nodeListTail->next; 154 | nodeListTail->node = nodeListHead->node->Rn; 155 | nodeListTail->next = nullptr; 156 | 157 | //������һ���ӡ���� 158 | countList->next->count++; 159 | } 160 | 161 | //ɾ���Ѵ�ӡ�Ĵ�ӡ�����ڵ� 162 | auto deleteNode = nodeListHead; 163 | nodeListHead = nodeListHead->next; 164 | delete deleteNode; 165 | deleteNode = nullptr; 166 | 167 | 168 | } 169 | std::cout << std::endl; 170 | 171 | //��ǰ��Ԫ�ش�ӡ�� 172 | countList->count = 0; 173 | countList = countList->next; 174 | 175 | } 176 | 177 | return; 178 | 179 | } 180 | 181 | huffmanCodeSetPtr HuffmanCodeSetInit(huffmanTreePtr sourceTree) { 182 | 183 | typedef struct codeSetHuffmanTreeNodeLink 184 | { //��ȱ����������������ṹ 185 | huffmanTreePtr node = nullptr; //���ڵ� 186 | std::string code = std::string(); //��ǰΪֹ�������� 187 | struct codeSetHuffmanTreeNodeLink* next = nullptr; 188 | }csNodeLink, * csNodeLinkPtr; 189 | 190 | //������������ 191 | csNodeLinkPtr codeSetHead = new csNodeLink; 192 | codeSetHead->code = std::string(); //��ʼ����Ϊ�� 193 | codeSetHead->node = sourceTree; //Huffman������Ϊ��һ�������ڵ� 194 | codeSetHead->next = nullptr; //ָ���� 195 | csNodeLinkPtr codeSetTail = codeSetHead; //����β 196 | 197 | //���������ָ�� 198 | huffmanCodeSet ResHead; 199 | huffmanCodeSetPtr resault = &ResHead; 200 | huffmanCodeSetPtr resTail = resault; 201 | 202 | while (codeSetHead) { //ֻҪ��û������ 203 | auto L = codeSetHead->node->Ln; 204 | auto R = codeSetHead->node->Rn; 205 | 206 | if (!R->Ln && !R->Rn) { //���ӽڵ���Ҷ�ڵ� 207 | //�������ַ����뱾 208 | resTail->next = new huffmanCodeSet; 209 | resTail->next->data = R->data; //�ַ� 210 | resTail->next->huffmanCode = codeSetHead->code + '1'; //���� 211 | resTail = resTail->next; //���뱾����βָ����� 212 | resTail->next = nullptr; //ָ�밲ȫ��ʼ�� 213 | } 214 | else { //��֦�ڵ� 215 | //���������������β 216 | codeSetTail->next = new csNodeLink; 217 | codeSetTail->next->code = codeSetHead->code + '1'; //���� 218 | codeSetTail->next->node = R; //�ڵ� 219 | codeSetTail = codeSetTail->next; //��������βָ����� 220 | codeSetTail->next = nullptr; //ָ�밲ȫ��ʼ�� 221 | } 222 | 223 | //ͬ��������ָ������ͬ���� 224 | if (!L->Ln && !L->Rn) { 225 | resTail->next = new huffmanCodeSet; 226 | resTail->next->data = L->data; 227 | resTail->next->huffmanCode = codeSetHead->code + '0'; 228 | resTail = resTail->next; 229 | resTail->next = nullptr; 230 | } 231 | else { 232 | codeSetTail->next = new csNodeLink; 233 | codeSetTail->next->code = codeSetHead->code + '0'; 234 | codeSetTail->next->node = L; 235 | codeSetTail = codeSetTail->next; 236 | codeSetTail->next = nullptr; 237 | } 238 | 239 | //ɾ����ǰ�Ѿ�ʹ����ı�������ͷ�ڵ㣨�ڴ���գ� 240 | auto deleteNode = codeSetHead; 241 | codeSetHead = codeSetHead->next; 242 | delete deleteNode; 243 | } 244 | 245 | 246 | return resault->next; 247 | } 248 | 249 | bool HuffmanEncoding(huffmanCodeSetPtr codeSet, 250 | std::string sourceContent, std::string& finishContent) { 251 | 252 | finishContent = std::string(); //����ı���ʼ�� 253 | 254 | for (auto data : sourceContent) { 255 | auto csPtr = codeSet; 256 | while (csPtr) 257 | { 258 | if (csPtr->data == data) { 259 | finishContent += csPtr->huffmanCode; 260 | break; 261 | } 262 | csPtr = csPtr->next; 263 | } 264 | if (!csPtr) return false; 265 | } 266 | 267 | return true; 268 | } 269 | 270 | bool HuffmanDecoding(huffmanTreePtr sourceTree, 271 | std::string sourceContent, std::string& finishContent) { 272 | auto nowPtr = sourceTree; 273 | finishContent = std::string(); 274 | 275 | for (auto nowChar : sourceContent) { 276 | if (nowChar == '0') { 277 | nowPtr = nowPtr->Ln; 278 | } 279 | if (nowChar == '1') { 280 | nowPtr = nowPtr->Rn; 281 | } 282 | 283 | if (!nowPtr) return false; 284 | 285 | if (!nowPtr->Ln && !nowPtr->Rn) { 286 | finishContent += nowPtr->data; 287 | nowPtr = sourceTree; 288 | continue; 289 | } 290 | 291 | } 292 | return true; 293 | } 294 | 295 | 296 | void codeTxtPrint(std::string sourceContent, std::string saveLocation) { 297 | std::fstream saveFile; 298 | saveFile.open(saveLocation,'w'); 299 | auto lineCount = 0; 300 | for (auto data : sourceContent) { 301 | std::cout << data; 302 | saveFile << data; 303 | lineCount++; 304 | if (lineCount == 50) { 305 | lineCount = 0; 306 | std::cout << std::endl; 307 | } 308 | } 309 | saveFile.close(); 310 | std::cout << std::endl; 311 | } 312 | 313 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /PID/PID.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * PID.cpp - The C++ source file of PID controller 3 | * NOTE: This file is based on C++11 4 | * 5 | * Copyright (c) 2020-, FOSH Project 6 | * 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Change Logs: 10 | * Date Author Notes mail 11 | * 2020-06-14 StudyHooligen first version 2373180028@qq.com 12 | */ 13 | #include "PID/PID.hpp" 14 | 15 | template 16 | PID::PID(T P,T I,T D,T outMax,T outMin) 17 | :K_p(P),K_i(I),K_d(D), 18 | max_(outMax),min_(outMin){ 19 | } 20 | 21 | template 22 | bool PID::PID_setTarget(T target){ 23 | target_ = target; 24 | return true; 25 | } 26 | 27 | template 28 | bool PID::PID_setParam(T P,T I,T D){ 29 | this->K_p = P; 30 | this->K_i = I; 31 | this->K_d = D; 32 | return true; 33 | } 34 | 35 | template 36 | bool PID::PID_setThreshold(T outMin,T outMax){ 37 | max_ = outMax; 38 | min_ = outMin; 39 | return true; 40 | } 41 | 42 | template 43 | T PID::PID_updateDelta(T status){ 44 | T errNow = target_ - status; //目前误差值 45 | 46 | T delta = //输出增量 47 | K_p * (errNow - error_Pre) + //比例P 48 | K_i * errNow + //积分I 49 | K_d * (errNow - 2*error_Pre + error_PPre); //微分D 50 | 51 | if(delta>max_) delta = max_; //输出限幅 52 | else if(delta 63 | T PID::PID_updateStatus(T status){ 64 | 65 | output_pre += this->PID_updateDelta(status); 66 | 67 | if(output_pre>max_) output_pre = max_; //输出限幅 68 | else if(output_pre 19 | class PID 20 | { 21 | 22 | private: 23 | /************ 内部参数 *************/ 24 | T K_p = 0; //比例增益 25 | T K_i = 0; //积分增益 26 | T K_d = 0; //微分增益 27 | 28 | T target_ = 0; //输出目标值 29 | 30 | T error_Pre = 0; //上一次误差 31 | T error_PPre = 0; //上上次误差 32 | 33 | #ifdef PID_use_integral 34 | T output_pre = 0; //上次输出值 35 | #endif 36 | 37 | T max_ = 65536; //PID输出最大值 38 | T min_ = 0; //PID输出最小值 39 | 40 | public: 41 | /*********** 模块接口 ***********/ 42 | typedef PID * PIDptr; //模块指针 43 | 44 | /* 概要:默认构造函数 45 | * 入参:无 46 | */ 47 | PID(); 48 | 49 | /* 概要:带参构造函数 50 | * 入参: 51 | * P: 比例系数 52 | * I:积分系数 53 | * D:微分系数 54 | */ 55 | PID(T P,T I,T D,T outMin,T outMax); 56 | 57 | /* 概要:默认析构函数 58 | * 入参:无 59 | */ 60 | ~PID(); 61 | 62 | /* 概要:改变PID实例参数 63 | * 入参: 64 | * target:目标值 65 | * 回参:参数设置成功或失败 66 | */ 67 | bool PID_setTarget(T target); 68 | 69 | /* 概要:改变PID实例参数 70 | * 入参: 71 | * P: 比例系数 72 | * I:积分系数 73 | * D:微分系数 74 | * 回参:参数设置成功或失败 75 | */ 76 | bool PID_setParam(T P,T I,T D); 77 | 78 | /* 概要:改变PID实例限幅值 79 | * 入参: 80 | * target:目标值 81 | * 回参:参数设置成功或失败 82 | */ 83 | bool PID_setThreshold(T outMin,T outMax); 84 | 85 | /* 概要:求取PID实例增量值 86 | * 入参: 87 | * status:目标状态测量值 88 | * 回参:增量值 89 | */ 90 | T PID_updateDelta(T status); 91 | 92 | /* 概要:求取PID实例输出值 93 | * 入参: 94 | * status:目标状态测量值 95 | * 回参:状态值 96 | */ 97 | T PID_updateStatus(T status); 98 | 99 | /* 概要:获取P参数 100 | * 入参:无 101 | * 回参:P参数 102 | */ 103 | T PID_getKp() 104 | { 105 | return K_p; 106 | } 107 | 108 | /* 概要:获取P参数 109 | * 入参:无 110 | * 回参:P参数 111 | */ 112 | T PID_getKi() 113 | { 114 | return K_i; 115 | } 116 | 117 | /* 概要:获取P参数 118 | * 入参:无 119 | * 回参:P参数 120 | */ 121 | T PID_getKd() 122 | { 123 | return K_d; 124 | } 125 | 126 | }; 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C/C++ Algorithm for embedded System 2 | 3 | 这是一个开源的嵌入式设备算法库,主要语言为C和C++ 4 | 5 | ## 库内资源 6 | 7 | - PID控制器 8 | - 哈夫曼编码器 9 | - 哈希表 10 | - 环形队列 11 | 12 | ## NOTICE 13 | 14 | 该库为FOSH计划的一部分,对于该库中的代码,欢迎使用,但是将代码用于违法项目,作者不负任何法律责任 15 | -------------------------------------------------------------------------------- /circularQueue/circularQueue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * circularQueue.h - The C head file of 环形队列 3 | * NOTE: This file is based on C++11 4 | * 5 | * Copyright (c) 2020-, FOSH Project 6 | * 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Change Logs: 10 | * Date Author Notes mail 11 | * 2020-06-14 StudyHooligen first version 2373180028@qq.com 12 | */ 13 | #ifndef circularQueue_H 14 | 15 | #define circularQueue_H 16 | 17 | #define uint16_t int 18 | #define uint32_t int 19 | 20 | typedef struct circularQueue 21 | { 22 | uint16_t readIndex; 23 | uint16_t writeIndex; 24 | uint16_t bufferSize; 25 | char* buffer; 26 | }circularQueue; 27 | 28 | void circularQueueInit(circularQueue* cq, char* bufferPtr, uint16_t bufferSize); 29 | 30 | void circularQueueAdd(circularQueue* cq, char* stringPtr, uint16_t stringSize); 31 | 32 | 33 | 34 | #endif // !circularQueue_H 35 | --------------------------------------------------------------------------------