๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1735

Re: Inversion-of-control and dependency-injection in C

#1735
From: BONITA MONTERO
Date: Thu, 19 Nov 2026 15:53
149 lines
5179 bytes
  To: COMP.LANG.C
From Newsgroup: comp.lang.c

Am 19.11.2026 um 10:30 schrieb fir:
> Bonita Montero pisze:
>> There are no complex data structures in C and it's very hard
>> to handle them as easy and as efficient as in C++. Imagine
>> you have a unordered_map<string_view, intin C++ and now you
>> should implement that in C. With C++ that's a one-liner, with
>> C that's a week of work.
> 
> 
> as this shot above you get it from ai - i never needed that so i dont
> want to discus it (have may ovn more algebraic attitude to 
containers)
> 
> writing it alosne is not a weeks of work arso adjusting it to some
> needs not so hard
> 
> this one cosed me like 2 * 3 seconds maybe here
> 
> ofc c has some unconvenience in syntax and with some additions the 
> containers in c could be more nice ofc (thats what im sometimes 
working on)
> 
> 
> #include <stdlib.h>
> #include <string.h>
> #include <stdio.h>
> 
> typedef struct {
>      const char *ptr;
>      int length;
> } StringView;
> 
> typedef struct {
>      StringView key;
>      int value;
>      unsigned hash;
>      int used;
> } StringIntMapEntry;
> 
> typedef struct {
>      StringIntMapEntry *data;
>      int size;
>      int capacity;
> } StringIntMap;
> 
> unsigned HashStringView(StringView s)
> {
>      unsigned h=2166136261u;
>      int i;
>      for(i=0;i<s.length;i++) {
>          h^=(unsigned char)s.ptr[i];
>          h*=16777619u;
>      }
>      return h;
> }
> 
> int StringViewEqual(StringView a,StringView b)
> {
>      return a.length==b.length && memcmp(a.ptr,b.ptr,a.length)==0;
> }
> 
> void StringIntMapInit(StringIntMap *m,int capacity)
> {
>      m->size=0;
>      m->capacity=capacity;
>      m- 
>  
>data=(StringIntMapEntry*)calloc(capacity,sizeof(StringIntMapEntry));
> }
> 
> void StringIntMapFree(StringIntMap *m)
> {
>      free(m->data);
>      m->data=NULL;
>      m->size=0;
>      m->capacity=0;
> }
> 
> void StringIntMapResize(StringIntMap *m,int capacity)
> {
>      StringIntMapEntry *old=m->data;
>      int old_capacity=m->capacity;
>      int i;
> 
>      m- 
>  
>data=(StringIntMapEntry*)calloc(capacity,sizeof(StringIntMapEntry));
>      m->capacity=capacity;
>      m->size=0;
> 
>      for(i=0;i<old_capacity;i++) {
>          if(old[i].used)
>              StringIntMapPut(m,old[i].key,old[i].value);
>      }
> 
>      free(old);
> }
> 
> void StringIntMapPut(StringIntMap *m,StringView key,int value)
> {
>      unsigned hash=HashStringView(key);
>      int index;
> 
>      if((m->size+1)*100>m->capacity*70)
>          StringIntMapResize(m,m->capacity*2);
> 
>      index=hash%m->capacity;
> 
>      while(m->data[index].used) {
>          if(m->data[index].hash==hash && StringViewEqual(m- 
>  >data[index].key,key)) {
>              m->data[index].value=value;
>              return;
>          }
>          index=(index+1)%m->capacity;
>      }
> 
>      m->data[index].used=1;
>      m->data[index].key=key;
>      m->data[index].value=value;
>      m->data[index].hash=hash;
>      m->size++;
> }
> 
> int *StringIntMapGet(StringIntMap *m,StringView key)
> {
>      unsigned hash=HashStringView(key);
>      int index=hash%m->capacity;
> 
>      while(m->data[index].used) {
>          if(m->data[index].hash==hash && StringViewEqual(m- 
>  >data[index].key,key))
>              return &m->data[index].value;
>          index=(index+1)%m->capacity;
>      }
> 
>      return NULL;
> }
Try to implement that with the full functionality of 
std::unordered_map.
You suck.

--- Synchronet 3.19b-Linux NewsLink 1.113
 รพ Synchronet รพ Mythical Kingdom
--- Synchronet 3.19b-Linux NewsLink 1.113
 *  Mythical Kingdom SBBS - Dayton OH - telnet://bbs.mythicalkingdom.com
Technical Details
Message-ID: <6B00B395.43246.usenet_complane@bbs.mythicalkingdom.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!mk.eternal-september.org!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!mk.eternal-september.org!.POSTED!not-for-mail
From: "BONITA MONTERO" <bonita.montero@LION.remove-eeq-this>
Newsgroups: comp.lang.c
Subject: Re: Inversion-of-control and dependency-injection in C
Date: Thu, 19 Nov 2026 15:53:00 +0000
Organization: A noiseless patient Spider
Lines: 149
Message-ID: <6B00B395.43246.usenet_complane@bbs.mythicalkingdom.com>
Content-Type: text/plain; charset=IBM437
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 20 Nov 2026 22:20:15 +0000 (UTC)
Injection-Info: mk.eternal-september.org;
	logging-data="3787638"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/HQSllNdWcEZV9O88uWvPbfUpCsbYcOQY="; posting-host="40b0ebdbc249f0e2cc641fd67dcd0032"
Cancel-Lock: sha1:Q+NyeuWLClS3mCyInTJkXSCfpcY= sha256:jJ5FwByjpaz6haeh24SvgaGzKYzerCFrGwyTD4pyfE8=
	sha1:26IaeQ+TFUvoMmsNePBH7Id16kg= sha256:S/mDeeZb72DgCNqDEQvUMCqwuNJKbmT/QHMa3N6EAx8=
X-FTN-CHRS: CP437 2
X-Comment-To: COMP.LANG.C
X-Gateway: bbs.mythicalkingdom.com [Synchronet 3.19b-Linux NewsLink 1.113]
X-FTN-PID: Synchronet 3.19b-Linux HEAD/6b10fdc41 Feb 18 2022 GCC 9.3.0