Skip to content

Commit

Permalink
rename ListInsert() => ListInsertFront(); add ListDelete(); ListInser…
Browse files Browse the repository at this point in the history
…tEnd(); ListReverse()
  • Loading branch information
kjk committed Aug 11, 2024
1 parent ab93c6f commit ad251b2
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/HomePage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
#endif
#define ABOUT_LINE_SEP_SIZE 1

constexpr const char* builtIn = R"(
[
Name = Edna
URL = https://edna.arslexis.io
Info = note taking app for develelopers
]
)";

constexpr COLORREF kAboutBorderCol = RGB(0, 0, 0);

constexpr int kAboutLeftRightSpaceDx = 8;
Expand Down
2 changes: 1 addition & 1 deletion src/mui/Mui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ CachedFont* GetCachedFont(const WCHAR* name, float sizePt, FontStyle style) {
}

FontListItem* item = new FontListItem(name, sizePt, style, font, nullptr);
ListInsert(&gFontsCache, item);
ListInsertFront(&gFontsCache, item);
return &item->cf;
}

Expand Down
46 changes: 45 additions & 1 deletion src/utils/BaseUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,55 @@ int RoundUp(int n, int rounding);
char* RoundUp(char*, int rounding);

template <typename T>
void ListInsert(T** root, T* el) {
void ListDelete(T* root) {
T* next;
T* curr = root;
while (curr) {
next = curr->next;
delete curr;
curr = next;
}
}

template <typename T>
void ListInsertFront(T** root, T* el) {
el->next = *root;
*root = el;
}

template <typename T>
void ListInsertEnd(T** root, T* el) {
el->next = nullptr;
if (!*root) {
*root = el;
return;
}
T** prevPtr = root;
T** currPtr = root;
T* curr;
while (*currPtr) {
prevPtr = currPtr;
curr = *currPtr;
currPtr = &(curr->next);
}
T* prev = *prevPtr;
prev->next = el;
}

template <typename T>
void ListReverse(T** root) {
T* newRoot = nullptr;
T* next;
T* el = *root;
while (el) {
next = el->next;
el->next = newRoot;
newRoot = el;
el = next;
}
*root = newRoot;
}

template <typename T>
bool ListRemove(T** root, T* el) {
T** currPtr = root;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/FileWatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static WatchedDir* NewWatchedDir(const char* dirPath) {
wd->hDir = hDir;
wd->dirPath = str::Dup(dirPath);

ListInsert(&gWatchedDirs, wd);
ListInsertFront(&gWatchedDirs, wd);
return wd;
}

Expand Down Expand Up @@ -418,7 +418,7 @@ static WatchedFile* NewWatchedFile(const char* filePath, const Func0& onFileChan
wf->watchedDir = wd;
wf->isManualCheck = isManualCheck;

ListInsert(&gWatchedFiles, wf);
ListInsertFront(&gWatchedFiles, wf);

if (wf->isManualCheck) {
GetFileState(filePath, &wf->fileState);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ static HFONT RememberCreatedFont(HFONT font, const char* name, int size, u16 fla
cf->size = (u16)size;
cf->flags = flags;
cf->weightOffset = weightOffset;
ListInsert(&gFonts, cf);
ListInsertFront(&gFonts, cf);
int n = ListLen(gFonts);
name = name ? name : "";
/* logf("RememberCreatedFont: added font '%s', size: %d, flags: %x, weightOffset: %d\n", name, size, (int)flags,
Expand Down
45 changes: 45 additions & 0 deletions src/utils/tests/BaseUtil_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,52 @@ static int roundUpTestCases[] = {
0, 0, 1, 8, 2, 8, 3, 8, 4, 8, 5, 8, 6, 8, 7, 8, 8, 8, 9, 16,
};

struct ListNode {
struct ListNode* next = nullptr;
int n = 0;
ListNode() = default;
};

static void CheckListOrder(ListNode* root, int* seq) {
ListNode* el = root;
for (int n = *seq; n >= 0; n = *(++seq)) {
utassert(el->n == n);
el = el->next;
}
utassert(!el);
}

static void ListTest() {
int n = 5;

static int orderReverse[] = {5, 4, 3, 2, 1, -1};
static int orderNormal[] = {1, 2, 3, 4, 5, -1};
{
ListNode* root = nullptr;
for (int i = 1; i <= n; i++) {
auto node = new ListNode();
node->n = i;
ListInsertFront(&root, node);
}
CheckListOrder(root, orderReverse);
ListReverse(&root);
CheckListOrder(root, orderNormal);
ListDelete(root);
}
{
ListNode* root = nullptr;
for (int i = 1; i <= n; i++) {
auto node = new ListNode();
node->n = i;
ListInsertEnd(&root, node);
}
CheckListOrder(root, orderNormal);
ListDelete(root);
}
}

void BaseUtilTest() {
ListTest();
Func0Test();
Func1Test();

Expand Down

0 comments on commit ad251b2

Please sign in to comment.